typescripttransformer-decorator不是';不能完全从AST中移除
我正试图从某些源文件的AST中删除特定的装饰程序。typescripttransformer-decorator不是';不能完全从AST中移除,typescript,typescript-compiler-api,Typescript,Typescript Compiler Api,我正试图从某些源文件的AST中删除特定的装饰程序。 我能够查找Decorator节点s,但是发出的代码仍然包含对\uu decoration([…])函数的调用 以下是变压器及其访问者 export default function() { function createVisitor( ctx: ts.TransformationContext, sourceFile: ts.SourceFile ): (node: ts.Node) => ts.VisitRes
我能够查找Decorator
节点
s,但是发出的代码仍然包含对\uu decoration([…])
函数的调用
以下是变压器及其访问者
export default function() {
function createVisitor(
ctx: ts.TransformationContext,
sourceFile: ts.SourceFile
): (node: ts.Node) => ts.VisitResult<ts.Node> {
const visitor: ts.Visitor = (node: ts.Node): ts.VisitResult<any> => {
if (ts.isDecorator(node)) {
if (node.getText(sourceFile) === '@Stateful') {
return undefined;
}
}
return ts.visitEachChild(node, visitor, ctx);
};
return visitor;
}
return (ctx: ts.TransformationContext) => (sourceFile: ts.SourceFile) =>
sourceFile.fileName.endsWith('component.ts')
? ts.visitNode(sourceFile, createVisitor(ctx, sourceFile))
: sourceFile;
}
我通过返回未定义的来删除AST节点
但是,编译后的代码仍然使用\uu
函数,尽管是空数组
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
...
};
import { Stateful } from './test.decorator';
export class Example {
constructor() {
this.str = '';
}
getStr() {
return this.str;
}
}
Example = __decorate([], Example);
我错过了什么?真奇怪。。。当我在控制台中记录父节点时,它有一个显示一个项的decorators
属性,但是当我在数组上调用.length
或尝试索引到它时,它将没有项&未定义。看起来像个虫子。作为一种解决方法,您可能会幸运地将父级的装饰器属性设置为未定义。@DavidSherret嘿!谢谢我也试过了,同样的结果。我真的不知道。我感谢你对我的兴趣!这是一个完美的时机:)而且,我很高兴这个网站对你有用!我希望如此!“这是在中提到的,但我还没有看到它,”戴维谢雷特谢谢。我在路线图上加了一条评论真的很奇怪。。。当我在控制台中记录父节点时,它有一个显示一个项的decorators
属性,但是当我在数组上调用.length
或尝试索引到它时,它将没有项&未定义。看起来像个虫子。作为一种解决方法,您可能会幸运地将父级的装饰器属性设置为未定义。@DavidSherret嘿!谢谢我也试过了,同样的结果。我真的不知道。我感谢你对我的兴趣!这是一个完美的时机:)而且,我很高兴这个网站对你有用!我希望如此!“这是在中提到的,但我还没有看到它,”戴维谢雷特谢谢。我在路线图上添加了一条评论
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
...
};
import { Stateful } from './test.decorator';
export class Example {
constructor() {
this.str = '';
}
getStr() {
return this.str;
}
}
Example = __decorate([], Example);