小编Rab*_*bee的帖子

是否可以使用打字稿编译器 API 将注释作为 AST 中的节点获取?

我想从打字稿源文件中提取注释,最好是行号。我试着这样做:

var program = ts.createProgram(files, {
    target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS, removeComments: false
});
ts.forEachChild(sourceFile, visit);

function visit(node) {
    if (node.kind == ts.SyntaxKind.SingleLineCommentTrivia){
        //print something
    }
    ts.forEachChild(node, visit);
}
Run Code Online (Sandbox Code Playgroud)

事实上,当我打印所有节点的文本时,我可以看到注释被完全丢弃了。我用于测试的输入源代码是:

//test comment
declare namespace myLib {
    //another comment
    function makeGreeting(s: string): string;
    let numberOfGreetings: number;
}
Run Code Online (Sandbox Code Playgroud)

static-analysis abstract-syntax-tree typescript typescript-compiler-api

11
推荐指数
2
解决办法
2058
查看次数

有没有办法从打字稿编译器 API 获取节点的行号?

我知道我们可以使用获取节点的位置,node.pos但是有没有办法获取节点的行号?像node.lineNumber什么?

static-analysis abstract-syntax-tree typescript typescript-compiler-api

6
推荐指数
1
解决办法
935
查看次数