Skip to content

Commit

Permalink
feat: Don't parse expression if a line terminator is after return
Browse files Browse the repository at this point in the history
The spec says that there if there is a line terminator after a return
that means an expression shouldn't be parsed.
  • Loading branch information
PrestonLTaylor committed Apr 17, 2024
1 parent f2170b9 commit a3a60e6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions JSS.Lib.UnitTests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,24 @@ public void Parse_ReturnsReturnStatement_WithNoExpression_WhenProvidingReturn_Wi
returnStatement!.ReturnExpression.Should().BeNull();
}

[Test]
public void Parse_ReturnsReturnStatement_WithNoExpression_WhenProvidingReturn_WithNewLineThenExpression()
{
// Arrange
var parser = new Parser("return\n1");

// Act
var parsedProgram = ParseScript(parser);
var rootNodes = parsedProgram.ScriptCode;

// Assert
rootNodes.Should().HaveCount(2);

var returnStatement = rootNodes[0] as ReturnStatement;
returnStatement.Should().NotBeNull();
returnStatement!.ReturnExpression.Should().BeNull();
}

[TestCaseSource(nameof(expressionToExpectedTypeTestCases))]
public void Parse_ReturnsReturnStatement_WhenProvidingReturn(KeyValuePair<string, Type> expressionToExpectedType)
{
Expand Down
4 changes: 2 additions & 2 deletions JSS.Lib/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1705,8 +1705,8 @@ private ReturnStatement ParseReturnStatement()
{
_consumer.ConsumeTokenOfType(TokenType.Return);

// FIXME: return [no LineTerminator here] Expression[+In, ?Yield, ?Await] ;
// Don't parse an expression if there is a line terminator after the return
if (_consumer.IsLineTerminator()) return new ReturnStatement(null);

TryParseExpression(out IExpression? returnExpression);
return new ReturnStatement(returnExpression);
}
Expand Down

0 comments on commit a3a60e6

Please sign in to comment.