自定义变压器不使用TS节点

发布于 2025-02-04 17:31:57 字数 3037 浏览 0 评论 0原文

我有一个我试图在ts节点的开发环境中运行的项目,但是,它不断破坏我的变压器:

import * as ts from "typescript";
import { convertSystem } from "./system";

const transformer: ts.TransformerFactory<ts.SourceFile> = (context) => {
    return (sourceFile) => {
        console.log(sourceFile.getChildrenCount()); // <== This errors with:
        // "Error: Debug Failure. Did not expect SourceFile to have an Identifier in its trivia"
        const visitor = (node: ts.Node): ts.Node => {
            // Normally I would do stuff here, but every node is of kind 305: "UnparsedSource"

            return ts.visitEachChild(node, visitor, context);
        };

        return ts.visitNode(sourceFile, visitor);
    };
};

export default (program: ts.Program) => transformer;

我应该在我的文件夹结构中指出一些奇数:

builder
  |_transfomer.ts
  |_transfomer_helpers
  | |_...
  |_package.json: I need this package.json to set "type":"module", or node will complain while running the transfomer

src
  |_index.ts: Entry point
  |_...

package.json (below) 
tsconfig.json (also below)

我的TS -config:

{
    "compilerOptions": {

        /* Language and Environment */
        "target": "ESNext" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,

        /* Modules */
        "module": "ESNext" /* Specify what module code is generated. */,

        "moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
       
        "outDir": "./build" /* Specify an output folder for all emitted files. */,
        
        "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
        
        /* Type Checking */
        "strict": true /* Enable all strict type-checking options. */,
        
        "skipLibCheck": true /* Skip type checking all .d.ts files. */,
        "plugins": [
            {
                "transform": "./builder/transformer.ts",
                "type": "raw"
            }
        ]
    },
    "ts-node": {
        "compiler": "ttypescript",
        "esm": true,
        "experimentalSpecifierResolution": "node",

        "compilerOptions": {
            "plugins": [
                {
                    "transform": "./builder/transformer.ts",
                    "after": true
                }
            ]
        }
    },

    "include": ["src"]
}

and package.json:

{
  "name": "Project",
  "version": "0.0.0",
  "description": "Proj description",
  "type": "module",
  "scripts": {
    "start": "ts-node src/test/test",
  },

  "dependencies": {
    "typescript": "^4.6.4"
  },
  "devDependencies": {
    "ts-node": "^10.8.1",
    "ttypescript": "^1.5.13"
  }
}

使用普通的ttypescript进行了工作,据我所知,在网上没有任何东西。

I have a project that I am trying to run in a dev environment with ts-node, however, it keeps breaking my transformer:

import * as ts from "typescript";
import { convertSystem } from "./system";

const transformer: ts.TransformerFactory<ts.SourceFile> = (context) => {
    return (sourceFile) => {
        console.log(sourceFile.getChildrenCount()); // <== This errors with:
        // "Error: Debug Failure. Did not expect SourceFile to have an Identifier in its trivia"
        const visitor = (node: ts.Node): ts.Node => {
            // Normally I would do stuff here, but every node is of kind 305: "UnparsedSource"

            return ts.visitEachChild(node, visitor, context);
        };

        return ts.visitNode(sourceFile, visitor);
    };
};

export default (program: ts.Program) => transformer;

There are a few oddities I should point out in my folder structure:

builder
  |_transfomer.ts
  |_transfomer_helpers
  | |_...
  |_package.json: I need this package.json to set "type":"module", or node will complain while running the transfomer

src
  |_index.ts: Entry point
  |_...

package.json (below) 
tsconfig.json (also below)

My ts-config:

{
    "compilerOptions": {

        /* Language and Environment */
        "target": "ESNext" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,

        /* Modules */
        "module": "ESNext" /* Specify what module code is generated. */,

        "moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
       
        "outDir": "./build" /* Specify an output folder for all emitted files. */,
        
        "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
        
        /* Type Checking */
        "strict": true /* Enable all strict type-checking options. */,
        
        "skipLibCheck": true /* Skip type checking all .d.ts files. */,
        "plugins": [
            {
                "transform": "./builder/transformer.ts",
                "type": "raw"
            }
        ]
    },
    "ts-node": {
        "compiler": "ttypescript",
        "esm": true,
        "experimentalSpecifierResolution": "node",

        "compilerOptions": {
            "plugins": [
                {
                    "transform": "./builder/transformer.ts",
                    "after": true
                }
            ]
        }
    },

    "include": ["src"]
}

And package.json:

{
  "name": "Project",
  "version": "0.0.0",
  "description": "Proj description",
  "type": "module",
  "scripts": {
    "start": "ts-node src/test/test",
  },

  "dependencies": {
    "typescript": "^4.6.4"
  },
  "devDependencies": {
    "ts-node": "^10.8.1",
    "ttypescript": "^1.5.13"
  }
}

This worked using normal ttypescript, and there has not been anything online so far as I can see.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文