I would like to extract the comments from a typescript source file, preferably with their line numbers. I tried doing it like this:
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);
}
In fact, when I printed all the nodes' text, I could see that the comments were discarded entirely. The input source code I used for testing is:
//test comment
declare namespace myLib {
//another comment
function makeGreeting(s: string): string;
let numberOfGreetings: number;
}
tsutils.forEachComment()
(just likets.getLeadingCommentRanges()
) \ see github.com/Microsoft/TypeScript/issues/21049 => github.com/ajafff/tsutils – Hydromancy