Skip to content

Commit

Permalink
bugfix privatet static class members, fixes sebastienros#354
Browse files Browse the repository at this point in the history
  • Loading branch information
jogibear9988 committed Dec 28, 2022
1 parent 1cec365 commit c551456
Show file tree
Hide file tree
Showing 2 changed files with 19 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 @@ -4723,7 +4723,7 @@ private ClassElement ParseClassElement(ref bool hasConstructor)
token = _lookahead;
}
}
key = ParseObjectPropertyKey();
key = ParseObjectPropertyKey(isPrivate);
}
else if (Match("{"))
{
Expand Down
18 changes: 18 additions & 0 deletions test/Esprima.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -598,4 +598,22 @@ class aa {

Assert.Equal(expected, json);
}

[Fact]
public void CanParsePrivateStaticClassMembers()
{
var parser = new JavaScriptParser();
var program = parser.ParseModule(@"
class X {
static #privateField = 'super';
static #getPrivateField() {
return X.#privateField;
}
}");

var json = program.ToJsonString();
var expected = "{\"type\":\"Program\",\"body\":[{\"type\":\"ClassDeclaration\",\"id\":{\"type\":\"Identifier\",\"name\":\"X\"},\"superClass\":null,\"body\":{\"type\":\"ClassBody\",\"body\":[{\"type\":\"PropertyDefinition\",\"key\":{\"type\":\"PrivateIdentifier\",\"name\":\"privateField\"},\"computed\":false,\"value\":{\"type\":\"Literal\",\"value\":\"super\",\"raw\":\"'super'\"},\"kind\":\"property\",\"static\":true},{\"type\":\"MethodDefinition\",\"key\":{\"type\":\"PrivateIdentifier\",\"name\":\"getPrivateField\"},\"computed\":false,\"value\":{\"type\":\"FunctionExpression\",\"id\":null,\"params\":[],\"body\":{\"type\":\"BlockStatement\",\"body\":[{\"type\":\"ReturnStatement\",\"argument\":{\"type\":\"MemberExpression\",\"computed\":false,\"object\":{\"type\":\"Identifier\",\"name\":\"X\"},\"property\":{\"type\":\"PrivateIdentifier\",\"name\":\"privateField\"},\"optional\":false}}]},\"generator\":false,\"expression\":false,\"strict\":true,\"async\":false},\"kind\":\"method\",\"static\":true}]}}],\"sourceType\":\"module\"}";

Assert.Equal(expected, json);
}
}

0 comments on commit c551456

Please sign in to comment.