Microsoft/TypeScript

JSDoc comment nodes are not traversed and their parents are sent even if they should not

Open

#14,575 创建于 2017年3月10日

在 GitHub 查看
 (3 评论) (1 反应) (0 负责人)TypeScript (48,455 star) (6,726 fork)batch import
BugDomain: APIDomain: JSDocHelp Wanted

描述

TypeScript Version: 2.2.1

Code

const ts = require('typescript');

const source = `
/**
 * Some fancy comment
 * 
 * @function sum
 * @param {number} a
 * @param {number} b
 * @returns {number} 
 */
function sum(a: number, b: number): number {
  return a + b;
}
`;

let sourceFile = ts.createSourceFile(
  'input.ts',
  source,
  ts.ScriptTarget.ESNext,
  false // <-- don't set parents
);

ts.forEachChild(sourceFile, function (node) {
  if (node.kind === ts.SyntaxKind.FunctionDeclaration) {
    console.assert(!!node.jsDoc);
    console.assert(node.jsDoc.length === 1);
    console.assert(node.jsDoc[0].parent === undefined);
  } else if (node.kind === ts.SyntaxKind.JSDocComment) {
    console.assert(false);
  }
});

Expected behavior:

  • Expected JSDocComment nodes parents not to be set.
  • Expected JSDocComment nodes to be traversed using forEachChild.

Actual behavior:

When explicitly passing false to createSourceFile for not setting parents in nodes, they are set anyway in all JSDocComment nodes. Also, JSDocComments (and therefore all their children) are not traversed when calling forEachChild. I don't know if that last part is expected behavior, but it is definitely weird that they are not traversed.

贡献者指南