Microsoft/TypeScript

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

Open

#14,575 opened on Mar 10, 2017

View on GitHub
 (3 comments) (1 reaction) (0 assignees)TypeScript (48,455 stars) (6,726 forks)batch import
BugDomain: APIDomain: JSDocHelp Wanted

Description

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.

Contributor guide