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
Create parser plugin "topLevelAwait" #10449
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7c95240
Create parser plugin "topLevelAwait"
nicolo-ribaudo 14aa7e4
Update test262 whitelist
nicolo-ribaudo 9a16a3e
Update ts typings
nicolo-ribaudo 94cfa86
Fix "sourceType: unambiguous" with TLA
nicolo-ribaudo be80170
Ambiguous tokens after await
nicolo-ribaudo fd83281
Update await %x(0)
nicolo-ribaudo b73f31c
typo [skip ci]
nicolo-ribaudo c3d892e
Typo [skip ci]
nicolo-ribaudo 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
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
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 |
---|---|---|
|
@@ -2208,6 +2208,7 @@ export default class ExpressionParser extends LValParser { | |
isAwaitAllowed(): boolean { | ||
if (this.scope.inFunction) return this.scope.inAsync; | ||
if (this.options.allowAwaitOutsideFunction) return true; | ||
if (this.hasPlugin("topLevelAwait")) return this.inModule; | ||
return false; | ||
} | ||
|
||
|
@@ -2234,9 +2235,33 @@ export default class ExpressionParser extends LValParser { | |
); | ||
} | ||
|
||
if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) { | ||
if ( | ||
this.hasPrecedingLineBreak() || | ||
// All the following expressions are ambiguous: | ||
// await + 0, await - 0, await ( 0 ), await [ 0 ], await / 0 /u, await `` | ||
this.match(tt.plusMin) || | ||
this.match(tt.parenL) || | ||
this.match(tt.bracketL) || | ||
this.match(tt.backQuote) || | ||
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. Awesome! I missed I come up with a new case: If one turns on await %a() |
||
// Sometimes the tokenizer generates tt.slash for regexps, and this is | ||
// handler by parseExprAtom | ||
this.match(tt.regexp) || | ||
this.match(tt.slash) || | ||
// This code could be parsed both as a modulo operator or as an intrinsic: | ||
// await %x(0) | ||
(this.hasPlugin("v8intrinsic") && this.match(tt.modulo)) | ||
) { | ||
this.ambiguousScriptDifferentAst = true; | ||
} else { | ||
this.sawUnambiguousESM = true; | ||
} | ||
} | ||
|
||
if (!this.state.soloAwait) { | ||
node.argument = this.parseMaybeUnary(); | ||
} | ||
|
||
return this.finishNode(node, "AwaitExpression"); | ||
} | ||
|
||
|
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
1 change: 1 addition & 0 deletions
1
packages/babel-parser/test/fixtures/experimental/top-level-await/export-default/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 @@ | ||
export default await 0; |
4 changes: 4 additions & 0 deletions
4
packages/babel-parser/test/fixtures/experimental/top-level-await/export-default/options.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,4 @@ | ||
{ | ||
"plugins": ["topLevelAwait"], | ||
"sourceType": "module" | ||
} |
85 changes: 85 additions & 0 deletions
85
packages/babel-parser/test/fixtures/experimental/top-level-await/export-default/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,85 @@ | ||
{ | ||
"type": "File", | ||
"start": 0, | ||
"end": 23, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 23 | ||
} | ||
}, | ||
"program": { | ||
"type": "Program", | ||
"start": 0, | ||
"end": 23, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 23 | ||
} | ||
}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "ExportDefaultDeclaration", | ||
"start": 0, | ||
"end": 23, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 23 | ||
} | ||
}, | ||
"declaration": { | ||
"type": "AwaitExpression", | ||
"start": 15, | ||
"end": 22, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 15 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 22 | ||
} | ||
}, | ||
"argument": { | ||
"type": "NumericLiteral", | ||
"start": 21, | ||
"end": 22, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 21 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 22 | ||
} | ||
}, | ||
"extra": { | ||
"rawValue": 0, | ||
"raw": "0" | ||
}, | ||
"value": 0 | ||
} | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-parser/test/fixtures/experimental/top-level-await/for-await-module/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 @@ | ||
for await (const a of b); |
4 changes: 4 additions & 0 deletions
4
...ges/babel-parser/test/fixtures/experimental/top-level-await/for-await-module/options.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,4 @@ | ||
{ | ||
"plugins": ["topLevelAwait"], | ||
"sourceType": "module" | ||
} |
134 changes: 134 additions & 0 deletions
134
...ages/babel-parser/test/fixtures/experimental/top-level-await/for-await-module/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,134 @@ | ||
{ | ||
"type": "File", | ||
"start": 0, | ||
"end": 25, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 25 | ||
} | ||
}, | ||
"program": { | ||
"type": "Program", | ||
"start": 0, | ||
"end": 25, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 25 | ||
} | ||
}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "ForOfStatement", | ||
"start": 0, | ||
"end": 25, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 25 | ||
} | ||
}, | ||
"await": true, | ||
"left": { | ||
"type": "VariableDeclaration", | ||
"start": 11, | ||
"end": 18, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 11 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 18 | ||
} | ||
}, | ||
"declarations": [ | ||
{ | ||
"type": "VariableDeclarator", | ||
"start": 17, | ||
"end": 18, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 17 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 18 | ||
} | ||
}, | ||
"id": { | ||
"type": "Identifier", | ||
"start": 17, | ||
"end": 18, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 17 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 18 | ||
}, | ||
"identifierName": "a" | ||
}, | ||
"name": "a" | ||
}, | ||
"init": null | ||
} | ||
], | ||
"kind": "const" | ||
}, | ||
"right": { | ||
"type": "Identifier", | ||
"start": 22, | ||
"end": 23, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 22 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 23 | ||
}, | ||
"identifierName": "b" | ||
}, | ||
"name": "b" | ||
}, | ||
"body": { | ||
"type": "EmptyStatement", | ||
"start": 24, | ||
"end": 25, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 24 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 25 | ||
} | ||
} | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-parser/test/fixtures/experimental/top-level-await/for-await-script/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 @@ | ||
for await (const a of b); |
5 changes: 5 additions & 0 deletions
5
...ges/babel-parser/test/fixtures/experimental/top-level-await/for-await-script/options.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,5 @@ | ||
{ | ||
"plugins": ["topLevelAwait"], | ||
"sourceType": "script", | ||
"throws": "Unexpected token, expected \"(\" (1:4)" | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-parser/test/fixtures/experimental/top-level-await/inside-arrow/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 @@ | ||
() => await 0; |
5 changes: 5 additions & 0 deletions
5
packages/babel-parser/test/fixtures/experimental/top-level-await/inside-arrow/options.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,5 @@ | ||
{ | ||
"plugins": ["topLevelAwait"], | ||
"sourceType": "module", | ||
"throws": "Can not use keyword 'await' outside an async function (1:6)" | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-parser/test/fixtures/experimental/top-level-await/inside-block/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,3 @@ | ||
if (true) { | ||
await 0; | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/babel-parser/test/fixtures/experimental/top-level-await/inside-block/options.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,4 @@ | ||
{ | ||
"plugins": ["topLevelAwait"], | ||
"sourceType": "module" | ||
} |
Oops, something went wrong.
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.
If top-level await is only valid in modules, should seeing one set
sawUnambiguousESM
? In other words, how should this behave with asourceType
ofunambiguous
? Currently, parsingawait 0
withunambiguous
yields an AST withsourceType
ofscript
, even though if you'd specifiedscript
it would not have parsed: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.
This is tricky because, for exmaple, this is both a valid script and a valid module:
I can set it to a module when
await
the next invalid token is on the same line.