Skip to content

Commit 7405bd1

Browse files
ota-meshimarijnh
authored andcommitted
Change allowAwaitOutsideFunction to be enabled by default when ecmaVersion >= 2022
1 parent 164bf8f commit 7405bd1

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

acorn/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ required):
9494
and also allows `import.meta` expressions to appear in scripts
9595
(when `sourceType` is not `"module"`).
9696

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

acorn/src/options.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ export const defaultOptions = {
3636
// appearing at the top of the program, and an import.meta expression
3737
// in a script isn't considered an error.
3838
allowImportExportEverywhere: false,
39+
// By default, await identifiers are allowed to appear at the top-level scope only if ecmaVersion >= 2022.
3940
// When enabled, await identifiers are allowed to appear at the top-level scope,
4041
// but they are still not allowed in non-async functions.
41-
allowAwaitOutsideFunction: false,
42+
allowAwaitOutsideFunction: null,
4243
// When enabled, hashbang directive in the beginning of file
4344
// is allowed and treated as a line comment.
4445
allowHashBang: false,
@@ -114,6 +115,8 @@ export function getOptions(opts) {
114115

115116
if (options.allowReserved == null)
116117
options.allowReserved = options.ecmaVersion < 5
118+
if (options.allowAwaitOutsideFunction == null)
119+
options.allowAwaitOutsideFunction = options.ecmaVersion >= 13
117120

118121
if (isArray(options.onToken)) {
119122
let tokens = options.onToken

test/tests-await-top-level.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,33 @@ testFail("function foo() {return await 1}", "Unexpected token (1:29)", {
3636
allowAwaitOutsideFunction: true,
3737
ecmaVersion: 8
3838
})
39+
// ES2022
40+
test("await 1", {
41+
"type": "Program",
42+
"start": 0,
43+
"end": 7,
44+
"body": [
45+
{
46+
"type": "ExpressionStatement",
47+
"start": 0,
48+
"end": 7,
49+
"expression": {
50+
"type": "AwaitExpression",
51+
"start": 0,
52+
"end": 7,
53+
"argument": {
54+
"type": "Literal",
55+
"start": 6,
56+
"end": 7,
57+
"value": 1
58+
}
59+
}
60+
}
61+
]
62+
}, {ecmaVersion: 13})
63+
testFail("function foo() {return await 1}", "Unexpected token (1:29)", {ecmaVersion: 13})
64+
testFail("await 1", "Unexpected token (1:6)", {
65+
allowAwaitOutsideFunction: false,
66+
ecmaVersion: 13
67+
})
68+
testFail("await 1", "Unexpected token (1:6)", {ecmaVersion: 12})

0 commit comments

Comments
 (0)