我想从打字稿源文件中提取注释,最好是行号。我试着这样做:
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
我知道我们可以使用获取节点的位置,node.pos但是有没有办法获取节点的行号?像node.lineNumber什么?
static-analysis abstract-syntax-tree typescript typescript-compiler-api