Skip to content

Commit

Permalink
Fix parsing of class elements named accessor without semicolon termin…
Browse files Browse the repository at this point in the history
…ator (#428)
  • Loading branch information
lahma authored Dec 30, 2023
1 parent 422b76e commit c3899b1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Esprima/JavaScriptParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4874,7 +4874,7 @@ private ClassElement ParseClassElement(bool hasSuperClass, ref bool hasConstruct
token = _lookahead;
key = ParseObjectPropertyKey();

if (!QualifiedPropertyName(_lookahead))
if (!QualifiedPropertyName(_lookahead) || _hasLineTerminator)
{
goto ParseValue;
}
Expand Down
39 changes: 39 additions & 0 deletions test/Esprima.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,45 @@ class X {
Assert.Equal(expected, json);
}

[Fact]
public void CanParseClassElementsWithNewLinesInsteadOfSemicolon()
{
// field-definition-accessor-no-line-terminator.js
var parser = new JavaScriptParser();
var program = parser.ParseScript("""
var C = class {
accessor
$;
static accessor
$;
}
""");

var declaration = (VariableDeclaration) Assert.Single(program.Body);
var variableDeclarator = Assert.Single(declaration.Declarations);
var classExpression = Assert.IsType<ClassExpression>(variableDeclarator.Init);

var classElements = classExpression.Body.Body;
Assert.Equal(4, classElements.Count);

var first = Assert.IsType<PropertyDefinition>(classElements[0]);
Assert.Equal("accessor", ((Identifier) first.Key).Name);
Assert.Null(first.Value);

var second = Assert.IsType<PropertyDefinition>(classElements[1]);
Assert.Equal("$", ((Identifier) second.Key).Name);
Assert.Null(second.Value);

var third = Assert.IsType<PropertyDefinition>(classElements[2]);
Assert.Equal("accessor", ((Identifier) third.Key).Name);
Assert.True(third.Static);
Assert.Null(third.Value);

var fourth = Assert.IsType<PropertyDefinition>(classElements[3]);
Assert.Equal("$", ((Identifier) fourth.Key).Name);
Assert.Null(fourth.Value);
}

[Theory]
[InlineData("script", true)]
[InlineData("module", false)]
Expand Down

0 comments on commit c3899b1

Please sign in to comment.