Skip to content

Commit

Permalink
fix: for ESTree, attach comments when a directive is followed by a co…
Browse files Browse the repository at this point in the history
…mment, then EOF

If a source file contains 1) a directive, followed by 2) a comment, and nothing else, then `@babel/parser` attaches the comment to the `Directive` node as a trailing comment.

The ESTree plugin handles `Directive` nodes by creating an `ExpressionStatement` node, then transferring information from the `Directive` node to the `ExpressionStatement` node. Previously, at the time it transfers information, comments hadn't yet been attached to the `Directive` node. And even if they had been, the ESTree plugin didn't do anything with comments attached to the `Directive` node. As a result, the source file's comment wasn't attached to any node in the resulting AST.

With this change, the ESTree plugin generates an AST in which the comment is attached to the `ExpressionStatement` node as a trailing comment.
  • Loading branch information
hegemonic committed Sep 12, 2022
1 parent 4b7047d commit 14b764d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/babel-parser/src/plugins/estree.ts
Expand Up @@ -111,6 +111,9 @@ export default (superClass: typeof Parser) =>
// @ts-expect-error TS2339: Property 'raw' does not exist on type 'Undone '.
expression.raw = directiveLiteral.extra.raw;

stmt.leadingComments = directive.leadingComments?.slice();
stmt.trailingComments = directive.trailingComments?.slice();

stmt.expression = this.finishNodeAt(
expression,
"Literal",
Expand Down Expand Up @@ -175,6 +178,9 @@ export default (superClass: typeof Parser) =>
end,
afterBlockParse,
);
if (node.directives.length) {
this.processComment(node);
}

const directiveStatements = node.directives.map(d =>
this.directiveToStmt(d),
Expand Down
@@ -0,0 +1,3 @@
"use strict";

/** @external foo */
@@ -0,0 +1,37 @@
{
"type": "File",
"start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":20}},
"program": {
"type": "Program",
"start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":20}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"trailingComments": [
{
"type": "CommentBlock",
"value": "* @external foo ",
"start":15,"end":35,"loc":{"start":{"line":3,"column":0,"index":15},"end":{"line":3,"column":20,"index":35}}
}
],
"expression": {
"type": "Literal",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"value": "use strict",
"raw": "\"use strict\""
},
"directive": "use strict"
}
]
},
"comments": [
{
"type": "CommentBlock",
"value": "* @external foo ",
"start":15,"end":35,"loc":{"start":{"line":3,"column":0,"index":15},"end":{"line":3,"column":20,"index":35}}
}
]
}

0 comments on commit 14b764d

Please sign in to comment.