diff --git a/packages/babel-parser/src/plugins/estree.ts b/packages/babel-parser/src/plugins/estree.ts index 2a23979dfa33..89531c705b1b 100644 --- a/packages/babel-parser/src/plugins/estree.ts +++ b/packages/babel-parser/src/plugins/estree.ts @@ -95,35 +95,25 @@ export default (superClass: typeof Parser) => return this.estreeParseLiteral(value); } + // Cast a Directive to an ExpressionStatement. Mutates the input Directive. directiveToStmt(directive: N.Directive): N.ExpressionStatement { - const directiveLiteral = directive.value; + const expression = directive.value as any as N.EstreeLiteral; + delete directive.value; - const stmt = this.startNodeAt( - directive.start, - directive.loc.start, - ); - const expression = this.startNodeAt( - directiveLiteral.start, - directiveLiteral.loc.start, - ); + expression.type = "Literal"; + // @ts-expect-error N.EstreeLiteral.raw is not defined. + expression.raw = expression.extra.raw; + expression.value = expression.extra.expressionValue; - expression.value = directiveLiteral.extra.expressionValue; - // @ts-expect-error TS2339: Property 'raw' does not exist on type 'Undone '. - expression.raw = directiveLiteral.extra.raw; + const stmt = directive as any as N.ExpressionStatement; + stmt.type = "ExpressionStatement"; + stmt.expression = expression; + // @ts-expect-error N.ExpressionStatement.directive is not defined + stmt.directive = expression.extra.rawValue; - stmt.expression = this.finishNodeAt( - expression, - "Literal", - directiveLiteral.loc.end, - ); - // @ts-expect-error N.Directive.value is not defined - stmt.directive = directiveLiteral.extra.raw.slice(1, -1); - - return this.finishNodeAt( - stmt, - "ExpressionStatement", - directive.loc.end, - ) as N.ExpressionStatement; + delete expression.extra; + + return stmt; } // ================================== diff --git a/packages/babel-parser/test/fixtures/estree/directives/interleaved-comments/input.js b/packages/babel-parser/test/fixtures/estree/directives/interleaved-comments/input.js new file mode 100644 index 000000000000..24c853e6bc11 --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/directives/interleaved-comments/input.js @@ -0,0 +1,4 @@ +// 1; +"use strict"; +// 2; +"use strict"; diff --git a/packages/babel-parser/test/fixtures/estree/directives/interleaved-comments/output.json b/packages/babel-parser/test/fixtures/estree/directives/interleaved-comments/output.json new file mode 100644 index 000000000000..b71ae9db581d --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/directives/interleaved-comments/output.json @@ -0,0 +1,67 @@ +{ + "type": "File", + "start":0,"end":39,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":13}}, + "program": { + "type": "Program", + "start":0,"end":39,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":13}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":6,"end":19,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":13}}, + "trailingComments": [ + { + "type": "CommentLine", + "value": " 2;", + "start":20,"end":25,"loc":{"start":{"line":3,"column":0,"index":20},"end":{"line":3,"column":5,"index":25}} + } + ], + "leadingComments": [ + { + "type": "CommentLine", + "value": " 1;", + "start":0,"end":5,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":5,"index":5}} + } + ], + "directive": "use strict", + "expression": { + "type": "Literal", + "start":6,"end":18,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":12}}, + "value": "use strict", + "raw": "\"use strict\"" + } + }, + { + "type": "ExpressionStatement", + "start":26,"end":39,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":13}}, + "leadingComments": [ + { + "type": "CommentLine", + "value": " 2;", + "start":20,"end":25,"loc":{"start":{"line":3,"column":0,"index":20},"end":{"line":3,"column":5,"index":25}} + } + ], + "directive": "use strict", + "expression": { + "type": "Literal", + "start":26,"end":38,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":12}}, + "value": "use strict", + "raw": "\"use strict\"" + } + } + ] + }, + "comments": [ + { + "type": "CommentLine", + "value": " 1;", + "start":0,"end":5,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":5,"index":5}} + }, + { + "type": "CommentLine", + "value": " 2;", + "start":20,"end":25,"loc":{"start":{"line":3,"column":0,"index":20},"end":{"line":3,"column":5,"index":25}} + } + ] +} 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}} + } + ] +}