0

타이프 스크립트 소스 파일에서 줄 번호가있는 주석을 추출하고 싶습니다. 나는 이런 식으로 일을 시도 : 나는 모든 노드의 텍스트를 인쇄 할 때, 나는 의견이 완전히 삭제 된 것을 볼 수 있었다typescript 컴파일러 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); 
} 

. 나는 시험에 사용되는 입력 소스 코드는 다음과 같습니다

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

답변

1

그것은 노드로 의견을 얻을 수는 없습니다 만 소스 파일의 의견을 얻을 여전히 가능하다. 사용할 함수는 getLeadingCommentRanges(text: string, pos: number)입니다.

for(var sourceFile of program.getSourceFiles()){ 
     ts.forEachChild(sourceFile, visit); 
    } 

function visit(node: ts.Node){ 
    let comments = ts.getLeadingCommentRanges(sourceFile.getText(), node.getFullStart(); 
} 
:

나는 이런 식으로 사용