Skip to content

Commit

Permalink
Change allowAwaitOutsideFunction to be enabled by default when `ecm…
Browse files Browse the repository at this point in the history
…aVersion` >= 2022
  • Loading branch information
ota-meshi authored and marijnh committed May 31, 2021
1 parent 164bf8f commit 7405bd1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
5 changes: 3 additions & 2 deletions acorn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ required):
and also allows `import.meta` expressions to appear in scripts
(when `sourceType` is not `"module"`).

- **allowAwaitOutsideFunction**: By default, `await` expressions can
only appear inside `async` functions. Setting this option to
- **allowAwaitOutsideFunction**: If `false`, `await` expressions can
only appear inside `async` functions. Defaults to `true` for
`ecmaVersion` 2022 and later, `false` for lower versions. Setting this option to
`true` allows to have top-level `await` expressions. They are
still not allowed in non-`async` functions, though.

Expand Down
5 changes: 4 additions & 1 deletion acorn/src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ export const defaultOptions = {
// appearing at the top of the program, and an import.meta expression
// in a script isn't considered an error.
allowImportExportEverywhere: false,
// By default, await identifiers are allowed to appear at the top-level scope only if ecmaVersion >= 2022.
// When enabled, await identifiers are allowed to appear at the top-level scope,
// but they are still not allowed in non-async functions.
allowAwaitOutsideFunction: false,
allowAwaitOutsideFunction: null,
// When enabled, hashbang directive in the beginning of file
// is allowed and treated as a line comment.
allowHashBang: false,
Expand Down Expand Up @@ -114,6 +115,8 @@ export function getOptions(opts) {

if (options.allowReserved == null)
options.allowReserved = options.ecmaVersion < 5
if (options.allowAwaitOutsideFunction == null)
options.allowAwaitOutsideFunction = options.ecmaVersion >= 13

if (isArray(options.onToken)) {
let tokens = options.onToken
Expand Down
30 changes: 30 additions & 0 deletions test/tests-await-top-level.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,33 @@ testFail("function foo() {return await 1}", "Unexpected token (1:29)", {
allowAwaitOutsideFunction: true,
ecmaVersion: 8
})
// ES2022
test("await 1", {
"type": "Program",
"start": 0,
"end": 7,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 7,
"expression": {
"type": "AwaitExpression",
"start": 0,
"end": 7,
"argument": {
"type": "Literal",
"start": 6,
"end": 7,
"value": 1
}
}
}
]
}, {ecmaVersion: 13})
testFail("function foo() {return await 1}", "Unexpected token (1:29)", {ecmaVersion: 13})
testFail("await 1", "Unexpected token (1:6)", {
allowAwaitOutsideFunction: false,
ecmaVersion: 13
})
testFail("await 1", "Unexpected token (1:6)", {ecmaVersion: 12})

0 comments on commit 7405bd1

Please sign in to comment.