From b256e512d74bfa0d35464dc6dfae54ef49a5e792 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 6 Jul 2021 17:03:59 -0400 Subject: [PATCH] fix: handle comments when parsing async function/do --- .../babel-parser/src/parser/expression.js | 12 ++- .../basic/async-do-expression/input.js | 1 + .../basic/async-do-expression/options.json | 3 + .../basic/async-do-expression/output.json | 90 +++++++++++++++++++ .../comments/basic/async-function/input.js | 1 + .../comments/basic/async-function/output.json | 57 ++++++++++++ 6 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/comments/basic/async-do-expression/input.js create mode 100644 packages/babel-parser/test/fixtures/comments/basic/async-do-expression/options.json create mode 100644 packages/babel-parser/test/fixtures/comments/basic/async-do-expression/output.json create mode 100644 packages/babel-parser/test/fixtures/comments/basic/async-function/input.js create mode 100644 packages/babel-parser/test/fixtures/comments/basic/async-function/output.json diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 4737782a677c..debf32c190f4 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -1008,6 +1008,7 @@ export default class ExpressionParser extends LValParser { if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) { if (this.match(tt._function)) { + this.resetPreviousNodeTrailingComments(id); this.next(); return this.parseFunction( this.startNodeAtNode(id), @@ -1019,13 +1020,19 @@ export default class ExpressionParser extends LValParser { // arrow function. (Peeking ahead for "=" lets us avoid a more // expensive full-token lookahead on this common path.) if (this.lookaheadCharCode() === charCodes.equalsTo) { - return this.parseAsyncArrowUnaryFunction(id); + // although `id` is not used in async arrow unary function, + // we don't need to reset `async`'s trailing comments because + // it will be attached to the upcoming async arrow binding identifier + return this.parseAsyncArrowUnaryFunction( + this.startNodeAtNode(id), + ); } else { // Otherwise, treat "async" as an identifier and let calling code // deal with the current tt.name token. return id; } } else if (this.match(tt._do)) { + this.resetPreviousNodeTrailingComments(id); return this.parseDo(true); } } @@ -1198,8 +1205,7 @@ export default class ExpressionParser extends LValParser { } // async [no LineTerminator here] AsyncArrowBindingIdentifier[?Yield] [no LineTerminator here] => AsyncConciseBody[?In] - parseAsyncArrowUnaryFunction(id: N.Expression): N.ArrowFunctionExpression { - const node = this.startNodeAtNode(id); + parseAsyncArrowUnaryFunction(node: N.Node): N.ArrowFunctionExpression { // We don't need to push a new ParameterDeclarationScope here since we are sure // 1) it is an async arrow, 2) no biding pattern is allowed in params this.prodParam.enter(functionFlags(true, this.prodParam.hasYield)); diff --git a/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/input.js b/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/input.js new file mode 100644 index 000000000000..b61e768397a7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/input.js @@ -0,0 +1 @@ +/* 1 */async/* 2 */do/* 3 */{/* 4 */}/* 5 */ diff --git a/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/options.json b/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/options.json new file mode 100644 index 000000000000..94fd7ea36118 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["asyncDoExpressions", "doExpressions"] +} diff --git a/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/output.json b/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/output.json new file mode 100644 index 000000000000..c82c2cfdbacc --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/output.json @@ -0,0 +1,90 @@ +{ + "type": "File", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}}, + "program": { + "type": "Program", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":7,"end":37,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":37}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 5 ", + "start":37,"end":44,"loc":{"start":{"line":1,"column":37},"end":{"line":1,"column":44}} + } + ], + "expression": { + "type": "DoExpression", + "start":19,"end":37,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":37}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 2 ", + "start":12,"end":19,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":19}} + } + ], + "async": true, + "body": { + "type": "BlockStatement", + "start":28,"end":37,"loc":{"start":{"line":1,"column":28},"end":{"line":1,"column":37}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 3 ", + "start":21,"end":28,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":28}} + } + ], + "innerComments": [ + { + "type": "CommentBlock", + "value": " 4 ", + "start":29,"end":36,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":36}} + } + ], + "body": [], + "directives": [] + } + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}} + }, + { + "type": "CommentBlock", + "value": " 2 ", + "start":12,"end":19,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":19}} + }, + { + "type": "CommentBlock", + "value": " 3 ", + "start":21,"end":28,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":28}} + }, + { + "type": "CommentBlock", + "value": " 4 ", + "start":29,"end":36,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":36}} + }, + { + "type": "CommentBlock", + "value": " 5 ", + "start":37,"end":44,"loc":{"start":{"line":1,"column":37},"end":{"line":1,"column":44}} + } + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/comments/basic/async-function/input.js b/packages/babel-parser/test/fixtures/comments/basic/async-function/input.js new file mode 100644 index 000000000000..561c4d2562b1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/async-function/input.js @@ -0,0 +1 @@ +/* 1 */async/* 2 */function f () {} diff --git a/packages/babel-parser/test/fixtures/comments/basic/async-function/output.json b/packages/babel-parser/test/fixtures/comments/basic/async-function/output.json new file mode 100644 index 000000000000..1cc9422626cb --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/async-function/output.json @@ -0,0 +1,57 @@ +{ + "type": "File", + "start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":35}}, + "program": { + "type": "Program", + "start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":35}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start":7,"end":35,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":35}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}} + } + ], + "innerComments": [ + { + "type": "CommentBlock", + "value": " 2 ", + "start":12,"end":19,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":19}} + } + ], + "id": { + "type": "Identifier", + "start":28,"end":29,"loc":{"start":{"line":1,"column":28},"end":{"line":1,"column":29},"identifierName":"f"}, + "name": "f" + }, + "generator": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start":33,"end":35,"loc":{"start":{"line":1,"column":33},"end":{"line":1,"column":35}}, + "body": [], + "directives": [] + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}} + }, + { + "type": "CommentBlock", + "value": " 2 ", + "start":12,"end":19,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":19}} + } + ] +}