Skip to content

Commit

Permalink
[javascript] Class elements (fixes antlr#3474, fixes antlr#3451)
Browse files Browse the repository at this point in the history
  • Loading branch information
ris58h committed Jun 13, 2023
1 parent 354f608 commit 7cddfe8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
34 changes: 27 additions & 7 deletions javascript/javascript/JavaScriptParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,29 @@ classTail
;

classElement
: (Static | {this.n("static")}? identifier | Async)* (methodDefinition | assignable '=' objectLiteral ';')
: (Static | {this.n("static")}? identifier)? methodDefinition
| (Static | {this.n("static")}? identifier)? fieldDefinition
| (Static | {this.n("static")}? identifier) block
| emptyStatement_
| '#'? propertyName '=' singleExpression
;

methodDefinition
: '*'? '#'? propertyName '(' formalParameterList? ')' functionBody
| '*'? '#'? getter '(' ')' functionBody
| '*'? '#'? setter '(' formalParameterList? ')' functionBody
: (Async {this.notLineTerminator()}?)? '*'? classElementName '(' formalParameterList? ')' functionBody
| '*'? getter '(' ')' functionBody
| '*'? setter '(' formalParameterList? ')' functionBody
;

fieldDefinition
: classElementName initializer?
;

classElementName
: propertyName
| privateIdentifier
;

privateIdentifier
: '#' identifierName
;

formalParameterList
Expand Down Expand Up @@ -386,6 +400,12 @@ singleExpression
| '(' expressionSequence ')' # ParenthesizedExpression
;

initializer
// TODO: must be `= AssignmentExpression` and we have such label alredy but it doesn't respect the specification.
// See https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#prod-Initializer
: '=' singleExpression
;

assignable
: identifier
| arrayLiteral
Expand Down Expand Up @@ -461,11 +481,11 @@ bigintLiteral
;

getter
: {this.n("get")}? identifier propertyName
: {this.n("get")}? identifier classElementName
;

setter
: {this.n("set")}? identifier propertyName
: {this.n("set")}? identifier classElementName
;

identifierName
Expand Down
3 changes: 1 addition & 2 deletions javascript/javascript/examples/AsyncAwait.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ async function f(){}
class C {
async method(){}
static async method1(){}
async static #method2(){}
static async #method2(){}
async *gen(){}
async get v(){return 1};
}
async ()=>{};

Expand Down
13 changes: 13 additions & 0 deletions javascript/javascript/examples/ClassFields.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const PREFIX = "prefix";

class ClassWithField {
field;
fieldWithInitializer = "instance field";
[`${PREFIX}Field`] = "prefixed field";
}

const instance = new ClassWithField();
console.log(Object.hasOwn(instance, "field")); // true
console.log(instance.field); // undefined
console.log(instance.fieldWithInitializer); // "instance field"
console.log(instance.prefixField); // "prefixed field"
12 changes: 12 additions & 0 deletions javascript/javascript/examples/ClassStatic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class ClassWithStaticInitializationBlock {
static staticProperty1 = 'Property 1';
static staticProperty2;
static {
this.staticProperty2 = 'Property 2';
}
}

console.log(ClassWithStaticInitializationBlock.staticProperty1);
// Expected output: "Property 1"
console.log(ClassWithStaticInitializationBlock.staticProperty2);
// Expected output: "Property 2"

0 comments on commit 7cddfe8

Please sign in to comment.