Skip to content

Commit 2e15505

Browse files
authored
Reject return statement in static block, even if allowReturnOutsideFunction is used
FIX: Fix an issue where the parser would allow `return` statements in `static` blocks when `allowReturnOutsideFunction` was enabled.
1 parent 064bec7 commit 2e15505

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

acorn/src/state.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ export class Parser {
115115
return (this.inModule && this.options.ecmaVersion >= 13) || this.options.allowAwaitOutsideFunction
116116
}
117117

118+
get allowReturn() {
119+
if (this.inFunction) return true
120+
if (this.options.allowReturnOutsideFunction && this.currentVarScope().flags & SCOPE_TOP) return true
121+
return false
122+
}
123+
118124
get allowSuper() {
119125
const {flags} = this.currentThisScope()
120126
return (flags & SCOPE_SUPER) > 0 || this.options.allowSuperOutsideMethod

acorn/src/statement.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ pp.parseIfStatement = function(node) {
356356
}
357357

358358
pp.parseReturnStatement = function(node) {
359-
if (!this.inFunction && !this.options.allowReturnOutsideFunction)
359+
if (!this.allowReturn)
360360
this.raise(this.start, "'return' outside of function")
361361
this.next()
362362

test/tests-class-features-2022.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6391,3 +6391,5 @@ testFail(`class C {
63916391
return #brand in #brand in obj;
63926392
}
63936393
}`, 'Private identifier can only be left side of binary expression (5:21)', { ecmaVersion: 13 })
6394+
6395+
testFail(`class X { static { return; } }`, "'return' outside of function (1:19)", {allowReturnOutsideFunction: true, ecmaVersion: 13});

0 commit comments

Comments
 (0)