New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Correctly disambiguate / after async fuctions #10475
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
// [opp]: http://en.wikipedia.org/wiki/Operator-precedence_parser | ||
|
||
import { types as tt, type TokenType } from "../tokenizer/types"; | ||
import { types as tc } from "../tokenizer/context"; | ||
import * as N from "../types"; | ||
import LValParser from "./lval"; | ||
import { | ||
|
@@ -949,6 +950,18 @@ export default class ExpressionParser extends LValParser { | |
this.match(tt._function) && | ||
!this.canInsertSemicolon() | ||
) { | ||
if (this.state.context.pop() !== tc.functionStatement) { | ||
// Since "async" is an identifier and normally identifiers | ||
// can't be followed by expression, the tokenizer assumes | ||
// that "function" starts a statement. | ||
// Fixing it in the tokenizer would mean traking not only the | ||
nicolo-ribaudo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// previous token ("async"), but also the one before to know | ||
// if its beforeExpr value. | ||
nicolo-ribaudo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// It's easier and more efficient to adjust the context here. | ||
throw new Error("Internal error"); | ||
} | ||
this.state.context.push(tc.functionExpression); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be more efficient to check the last element of |
||
|
||
this.next(); | ||
return this.parseFunction(node, undefined, true); | ||
} else if ( | ||
|
2 changes: 2 additions & 0 deletions
2
...el-parser/test/fixtures/es2017/async-functions/context-division-after-expression/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
void async function fn() {} | ||
/foo/g |
185 changes: 185 additions & 0 deletions
185
...parser/test/fixtures/es2017/async-functions/context-division-after-expression/output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
{ | ||
"type": "File", | ||
"start": 0, | ||
"end": 34, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"column": 6 | ||
} | ||
}, | ||
"program": { | ||
"type": "Program", | ||
"start": 0, | ||
"end": 34, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"column": 6 | ||
} | ||
}, | ||
"sourceType": "script", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "ExpressionStatement", | ||
"start": 0, | ||
"end": 34, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"column": 6 | ||
} | ||
}, | ||
"expression": { | ||
"type": "BinaryExpression", | ||
"start": 0, | ||
"end": 34, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"column": 6 | ||
} | ||
}, | ||
"left": { | ||
"type": "BinaryExpression", | ||
"start": 0, | ||
"end": 32, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"column": 4 | ||
} | ||
}, | ||
"left": { | ||
"type": "UnaryExpression", | ||
"start": 0, | ||
"end": 27, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 27 | ||
} | ||
}, | ||
"operator": "void", | ||
"prefix": true, | ||
"argument": { | ||
"type": "FunctionExpression", | ||
"start": 5, | ||
"end": 27, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 5 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 27 | ||
} | ||
}, | ||
"id": { | ||
"type": "Identifier", | ||
"start": 20, | ||
"end": 22, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 20 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 22 | ||
}, | ||
"identifierName": "fn" | ||
}, | ||
"name": "fn" | ||
}, | ||
"generator": false, | ||
"async": true, | ||
"params": [], | ||
"body": { | ||
"type": "BlockStatement", | ||
"start": 25, | ||
"end": 27, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 25 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 27 | ||
} | ||
}, | ||
"body": [], | ||
"directives": [] | ||
} | ||
} | ||
}, | ||
"operator": "/", | ||
"right": { | ||
"type": "Identifier", | ||
"start": 29, | ||
"end": 32, | ||
"loc": { | ||
"start": { | ||
"line": 2, | ||
"column": 1 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"column": 4 | ||
}, | ||
"identifierName": "foo" | ||
}, | ||
"name": "foo" | ||
} | ||
}, | ||
"operator": "/", | ||
"right": { | ||
"type": "Identifier", | ||
"start": 33, | ||
"end": 34, | ||
"loc": { | ||
"start": { | ||
"line": 2, | ||
"column": 5 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"column": 6 | ||
}, | ||
"identifierName": "g" | ||
}, | ||
"name": "g" | ||
} | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
.../babel-parser/test/fixtures/es2017/async-functions/context-regex-after-statement/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
async function fn() {} | ||
/foo/g |
122 changes: 122 additions & 0 deletions
122
...bel-parser/test/fixtures/es2017/async-functions/context-regex-after-statement/output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
{ | ||
"type": "File", | ||
"start": 0, | ||
"end": 29, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"column": 6 | ||
} | ||
}, | ||
"program": { | ||
"type": "Program", | ||
"start": 0, | ||
"end": 29, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"column": 6 | ||
} | ||
}, | ||
"sourceType": "script", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "FunctionDeclaration", | ||
"start": 0, | ||
"end": 22, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 22 | ||
} | ||
}, | ||
"id": { | ||
"type": "Identifier", | ||
"start": 15, | ||
"end": 17, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 15 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 17 | ||
}, | ||
"identifierName": "fn" | ||
}, | ||
"name": "fn" | ||
}, | ||
"generator": false, | ||
"async": true, | ||
"params": [], | ||
"body": { | ||
"type": "BlockStatement", | ||
"start": 20, | ||
"end": 22, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 20 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 22 | ||
} | ||
}, | ||
"body": [], | ||
"directives": [] | ||
} | ||
}, | ||
{ | ||
"type": "ExpressionStatement", | ||
"start": 23, | ||
"end": 29, | ||
"loc": { | ||
"start": { | ||
"line": 2, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"column": 6 | ||
} | ||
}, | ||
"expression": { | ||
"type": "RegExpLiteral", | ||
"start": 23, | ||
"end": 29, | ||
"loc": { | ||
"start": { | ||
"line": 2, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"column": 6 | ||
} | ||
}, | ||
"extra": { | ||
"raw": "/foo/g" | ||
}, | ||
"pattern": "foo", | ||
"flags": "g" | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: prefer
ct
to be consistent with naming intokenizer
.