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

Rab*_*bee 11 static-analysis abstract-syntax-tree typescript typescript-compiler-api

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

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)

Dim*_*ima 13

如果您使用 jsDoc 风格的注释,例如

export const myConst = {
  /**
   * My property description
   */
  myProp: 'test prop',
};

Run Code Online (Sandbox Code Playgroud)

可以在树遍历过程中检索评论内容。

例如extractWithComment将返回

{
  name: 'myProp',
  comment: 'My property description',
  type: 'string'
}

Run Code Online (Sandbox Code Playgroud)
import * as ts from 'typescript';

export function extractWithComment(fileNames: string[], options: ts.CompilerOptions): void {
  const program = ts.createProgram(fileNames, options);
  const checker: ts.TypeChecker = program.getTypeChecker();

  for (const sourceFile of program.getSourceFiles()) {
    if (!sourceFile.isDeclarationFile) {
      ts.forEachChild(sourceFile, visit);
    }
  }

  function visit(node: ts.Node) {
    const count = node.getChildCount()

    if (count > 0) {
      ts.forEachChild(node, visit);
    }

    if (ts.isPropertyAssignment(node) && node.name) {
      const symbol = checker.getSymbolAtLocation(node.name);
      if (symbol) {
        return serializeSymbol(symbol)
      }
    }
  }

  function serializeSymbol(symbol: ts.Symbol) {
    return {
      name: symbol.getName(),
      comment: ts.displayPartsToString(symbol.getDocumentationComment(checker)),
      type: checker.typeToString(
        checker.getTypeOfSymbolAtLocation(symbol, symbol.valueDeclaration!)
      ),
    };
  }
}
Run Code Online (Sandbox Code Playgroud)


Rab*_*bee 12

无法将注释作为节点获取,但仍然可以从源文件中获取注释。要使用的函数是getLeadingCommentRanges(text: string, pos: number)

我是这样用的:

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

function visit(node: ts.Node){
    const commentRanges = ts.getLeadingCommentRanges(
        sourceFile.getFullText(), 
        node.getFullStart());
    if (commentRange?.length)
        const commentStrings:string[] = 
          commentRanges.map(r=>sourceFile.getFullText().slice(r.pos,r.end))
}
Run Code Online (Sandbox Code Playgroud)

注意:sourceFile.getFullText()不会跳过前导注释,并且sourceFile.getText()会跳过前导注释。使用.getText在上述情况下使用是很明显错误的公共源极