Skip to content

Commit

Permalink
fix: handle comments when parsing async function/do
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jul 6, 2021
1 parent c8ac98a commit b256e51
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -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),
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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));
Expand Down
@@ -0,0 +1 @@
/* 1 */async/* 2 */do/* 3 */{/* 4 */}/* 5 */
@@ -0,0 +1,3 @@
{
"plugins": ["asyncDoExpressions", "doExpressions"]
}
@@ -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}}
}
]
}
@@ -0,0 +1 @@
/* 1 */async/* 2 */function f () {}
@@ -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}}
}
]
}

0 comments on commit b256e51

Please sign in to comment.