Skip to content

Commit cf5588a

Browse files
committed
Reject return statement in static block, even if AllowReturnOutsideFunction is used
1 parent 75a15bf commit cf5588a

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/Acornima/Parser.State.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,14 @@ private bool CanAwait
272272
}
273273
}
274274

275+
private bool AllowReturn
276+
{
277+
// https://github.com/acornjs/acorn/blob/2e1550534005a8d02030df99e20f06c0392ac28b/acorn/src/state.js > `get allowReturn`
278+
279+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
280+
get => InFunction || (_options.AllowReturnOutsideFunction && (CurrentVarScope._flags & ScopeFlags.Top) != 0);
281+
}
282+
275283
private bool AllowSuper
276284
{
277285
// https://github.com/acornjs/acorn/blob/8.11.3/acorn/src/state.js > `get allowSuper`

src/Acornima/Parser.Statement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ private ReturnStatement ParseReturnStatement(in Marker startMarker)
743743
{
744744
// https://github.com/acornjs/acorn/blob/8.11.3/acorn/src/statement.js > `pp.parseReturnStatement = function`
745745

746-
if (!_options._allowReturnOutsideFunction && !InFunction)
746+
if (!AllowReturn)
747747
{
748748
// Raise(_tokenizer._start, "'return' outside of function"); // original acornjs error reporting
749749
Raise(_tokenizer._start, IllegalReturn);

test/Acornima.Tests/ParserTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,6 +2278,18 @@ public void ShouldDisallowImportKeywordInYieldExpression(string sourceType)
22782278
Assert.Throws<SyntaxErrorException>(() => parseAction(parser, code));
22792279
}
22802280

2281+
[Fact]
2282+
public void ShouldDisallowReturnInClassStaticBlock()
2283+
{
2284+
var parser = new Parser(new ParserOptions
2285+
{
2286+
AllowReturnOutsideFunction = true,
2287+
});
2288+
2289+
var ex = Assert.Throws<SyntaxErrorException>(() => parser.ParseScript("class X { static { return; } }"));
2290+
Assert.Equal("Illegal return statement", ex.Description);
2291+
}
2292+
22812293
[Theory]
22822294
[InlineData("as")]
22832295
[InlineData("do")]

0 commit comments

Comments
 (0)