diff --git a/packages/babel-parser/src/plugins/estree.ts b/packages/babel-parser/src/plugins/estree.ts index 2a23979dfa33..e97af9aabbca 100644 --- a/packages/babel-parser/src/plugins/estree.ts +++ b/packages/babel-parser/src/plugins/estree.ts @@ -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", @@ -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), diff --git a/packages/babel-parser/test/fixtures/estree/directives/trailing-comment-eof/input.js b/packages/babel-parser/test/fixtures/estree/directives/trailing-comment-eof/input.js new file mode 100644 index 000000000000..ec942871d7de --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/directives/trailing-comment-eof/input.js @@ -0,0 +1,3 @@ +"use strict"; + +/** @external foo */ diff --git a/packages/babel-parser/test/fixtures/estree/directives/trailing-comment-eof/output.json b/packages/babel-parser/test/fixtures/estree/directives/trailing-comment-eof/output.json new file mode 100644 index 000000000000..4c9909c37f59 --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/directives/trailing-comment-eof/output.json @@ -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}} + } + ] +}