Skip to content

Commit

Permalink
Fix comments attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed May 17, 2023
1 parent 893442d commit 15a4308
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
34 changes: 34 additions & 0 deletions packages/babel-parser/src/parser/comments.ts
Expand Up @@ -246,6 +246,40 @@ export default class CommentsParser extends BaseParser {
}
}

/* eslint-disable no-irregular-whitespace */
/**
* Reset previous node leading comments. Used in import phase modifiers
* parsing. We parse `module` in `import module foo from ...` as an
* identifier but may reinterpret it into a phase modifier later. In this
* case the identifier is not part of the AST and we should sync the
* knowledge to commentStacks
*
* For example, when parsing
* ```
* import /* 1 *​/ module a from "a";
* ```
* the comment whitespace `/* 1 *​/` has trailing node Identifier(module). When
* we see that `module` is not a default import binding, we mark `/* 1 *​/` as
* inner comments of the ImportDeclaration. So `/* 1 *​/` should be detached from
* the Identifier node.
*
* @param node the last finished AST node _before_ current token
*/
/* eslint-enable no-irregular-whitespace */
resetPreviousNodeLeadingComments(node: Node) {
const { commentStack } = this.state;
const { length } = commentStack;
if (length === 0) return;

for (let i = length - 1; i >= 0; i--) {
const commentWS = commentStack[i];
if (commentWS.trailingNode === node) {
commentWS.trailingNode = null;
}
if (commentWS.end < node.start) break;
}
}

/**
* Attach a node to the comment whitespaces right before/after
* the given range.
Expand Down
1 change: 1 addition & 0 deletions packages/babel-parser/src/parser/statement.ts
Expand Up @@ -2958,6 +2958,7 @@ export default abstract class StatementParser extends ExpressionParser {
type !== tt.comma;

if (isImportPhase) {
this.resetPreviousNodeLeadingComments(phaseIdentifier);
this.applyImportPhase(
node as Undone<N.ImportDeclaration>,
isExport,
Expand Down
@@ -0,0 +1 @@
/* 0 */ import /* 1 */ module /* 2 */ Foo /* 3 */ from /* 4 */ "foo" /* 5 */;
@@ -0,0 +1,112 @@
{
"type": "File",
"start":0,"end":77,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":77,"index":77}},
"program": {
"type": "Program",
"start":0,"end":77,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":77,"index":77}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":8,"end":77,"loc":{"start":{"line":1,"column":8,"index":8},"end":{"line":1,"column":77,"index":77}},
"module": true,
"specifiers": [
{
"type": "ImportDefaultSpecifier",
"start":38,"end":41,"loc":{"start":{"line":1,"column":38,"index":38},"end":{"line":1,"column":41,"index":41}},
"local": {
"type": "Identifier",
"start":38,"end":41,"loc":{"start":{"line":1,"column":38,"index":38},"end":{"line":1,"column":41,"index":41},"identifierName":"Foo"},
"name": "Foo"
},
"trailingComments": [
{
"type": "CommentBlock",
"value": " 3 ",
"start":42,"end":49,"loc":{"start":{"line":1,"column":42,"index":42},"end":{"line":1,"column":49,"index":49}}
}
],
"leadingComments": [
{
"type": "CommentBlock",
"value": " 2 ",
"start":30,"end":37,"loc":{"start":{"line":1,"column":30,"index":30},"end":{"line":1,"column":37,"index":37}}
}
]
}
],
"source": {
"type": "StringLiteral",
"start":63,"end":68,"loc":{"start":{"line":1,"column":63,"index":63},"end":{"line":1,"column":68,"index":68}},
"extra": {
"rawValue": "foo",
"raw": "\"foo\""
},
"value": "foo",
"trailingComments": [
{
"type": "CommentBlock",
"value": " 5 ",
"start":69,"end":76,"loc":{"start":{"line":1,"column":69,"index":69},"end":{"line":1,"column":76,"index":76}}
}
],
"leadingComments": [
{
"type": "CommentBlock",
"value": " 4 ",
"start":55,"end":62,"loc":{"start":{"line":1,"column":55,"index":55},"end":{"line":1,"column":62,"index":62}}
}
]
},
"innerComments": [
{
"type": "CommentBlock",
"value": " 1 ",
"start":15,"end":22,"loc":{"start":{"line":1,"column":15,"index":15},"end":{"line":1,"column":22,"index":22}}
}
],
"leadingComments": [
{
"type": "CommentBlock",
"value": " 0 ",
"start":0,"end":7,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":7,"index":7}}
}
]
}
],
"directives": []
},
"comments": [
{
"type": "CommentBlock",
"value": " 0 ",
"start":0,"end":7,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":7,"index":7}}
},
{
"type": "CommentBlock",
"value": " 1 ",
"start":15,"end":22,"loc":{"start":{"line":1,"column":15,"index":15},"end":{"line":1,"column":22,"index":22}}
},
{
"type": "CommentBlock",
"value": " 2 ",
"start":30,"end":37,"loc":{"start":{"line":1,"column":30,"index":30},"end":{"line":1,"column":37,"index":37}}
},
{
"type": "CommentBlock",
"value": " 3 ",
"start":42,"end":49,"loc":{"start":{"line":1,"column":42,"index":42},"end":{"line":1,"column":49,"index":49}}
},
{
"type": "CommentBlock",
"value": " 4 ",
"start":55,"end":62,"loc":{"start":{"line":1,"column":55,"index":55},"end":{"line":1,"column":62,"index":62}}
},
{
"type": "CommentBlock",
"value": " 5 ",
"start":69,"end":76,"loc":{"start":{"line":1,"column":69,"index":69},"end":{"line":1,"column":76,"index":76}}
}
]
}

0 comments on commit 15a4308

Please sign in to comment.