Skip to content

Commit

Permalink
Make sure await in class field initializers is checked properly
Browse files Browse the repository at this point in the history
FIX: Fix an issue where `await` expressions in class field initializers were
inappropriately allowed.

Closes #1334
  • Loading branch information
marijnh committed Dec 14, 2024
1 parent 3c6a5a9 commit b6a98df
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
3 changes: 2 additions & 1 deletion acorn/src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ export class Parser {
get inAsync() { return (this.currentVarScope().flags & SCOPE_ASYNC) > 0 && !this.currentVarScope().inClassFieldInit }

get canAwait() {
if (this.currentThisScope().inClassFieldInit) return false
for (let i = this.scopeStack.length - 1; i >= 0; i--) {
let scope = this.scopeStack[i]
if (scope.inClassFieldInit || scope.flags & SCOPE_CLASS_STATIC_BLOCK) return false
if (scope.flags & SCOPE_CLASS_STATIC_BLOCK) return false
if (scope.flags & SCOPE_FUNCTION) return (scope.flags & SCOPE_ASYNC) > 0
}
return (this.inModule && this.options.ecmaVersion >= 13) || this.options.allowAwaitOutsideFunction
Expand Down
2 changes: 2 additions & 0 deletions test/tests-asyncawait.js
Original file line number Diff line number Diff line change
Expand Up @@ -3540,3 +3540,5 @@ testFail("async() => await (5) ** 6", "Unexpected token (1:21)", {ecmaVersion: 8
testFail("4 + async() => 2", "Unexpected token (1:12)", {ecmaVersion: 8, loose: false})

testFail("async function𝐬 f() {}", "Unexpected token (1:17)", {ecmaVersion: 8})

testFail("async () => class { x = await 1 }", "Cannot use 'await' as identifier inside an async function (1:24)", {ecmaVersion: 2024})

0 comments on commit b6a98df

Please sign in to comment.