Skip to content

JavaScript AST inconsistent about trivia vs JSDoc #9975

Closed
@evmar

Description

@evmar

The parsed JavaScript AST sometimes includes JSDoc in its trivia, and sometimes it doesn't. This seems to be a regression from TS 1.9ish to current tip of tree.

If you have an input file like

/** @a */
var a = 3;
/** @b */
function b() { }

and you parse this with TypeScript ts.createSourceFile and reserialize it out via a recursive loop like:

function dump(node: ts.Node) {
  let pos = node.getFullStart();
  for (const child of node.getChildren()) {
    dumpRange(pos, child.getFullStart());
    dump(child);
    pos = child.getEnd();
  }
  dumpRange(pos, node.getEnd());

  function dumpRange(start: number, end: number) {
    process.stdout.write(node.getSourceFile().text.slice(start, end));
  }
}

The output is:

/** @a *//** @a */
var a = 3;
/** @b */
function b() { }

You'll see that one of the comments was duplicated, and the other wasn't.

I think this is because in the FunctionDeclaration case, the trivia of the FunctionDeclaration node stops at the JSDocComment, but in the VariableStatement case, the trivia of the VarKeyword still includes the jsdoc despite it also having a JSDocComment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions