Skip to content

Commit b6a98df

Browse files
committed
Make sure await in class field initializers is checked properly
FIX: Fix an issue where `await` expressions in class field initializers were inappropriately allowed. Closes #1334
1 parent 3c6a5a9 commit b6a98df

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

acorn/src/state.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ export class Parser {
104104
get inAsync() { return (this.currentVarScope().flags & SCOPE_ASYNC) > 0 && !this.currentVarScope().inClassFieldInit }
105105

106106
get canAwait() {
107+
if (this.currentThisScope().inClassFieldInit) return false
107108
for (let i = this.scopeStack.length - 1; i >= 0; i--) {
108109
let scope = this.scopeStack[i]
109-
if (scope.inClassFieldInit || scope.flags & SCOPE_CLASS_STATIC_BLOCK) return false
110+
if (scope.flags & SCOPE_CLASS_STATIC_BLOCK) return false
110111
if (scope.flags & SCOPE_FUNCTION) return (scope.flags & SCOPE_ASYNC) > 0
111112
}
112113
return (this.inModule && this.options.ecmaVersion >= 13) || this.options.allowAwaitOutsideFunction

test/tests-asyncawait.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3540,3 +3540,5 @@ testFail("async() => await (5) ** 6", "Unexpected token (1:21)", {ecmaVersion: 8
35403540
testFail("4 + async() => 2", "Unexpected token (1:12)", {ecmaVersion: 8, loose: false})
35413541

35423542
testFail("async function𝐬 f() {}", "Unexpected token (1:17)", {ecmaVersion: 8})
3543+
3544+
testFail("async () => class { x = await 1 }", "Cannot use 'await' as identifier inside an async function (1:24)", {ecmaVersion: 2024})

0 commit comments

Comments
 (0)