diff --git a/ast/spec.md b/ast/spec.md index f8c675c8c1..c230951efe 100644 --- a/ast/spec.md +++ b/ast/spec.md @@ -1056,6 +1056,7 @@ interface ClassProperty <: Node { type: "ClassProperty"; key: Expression; value: Expression; + static: boolean; computed: boolean; } ``` @@ -1067,6 +1068,7 @@ interface ClassPrivateProperty <: Node { type: "ClassPrivateProperty"; key: Identifier; value: Expression; + static: boolean; } ``` diff --git a/src/parser/statement.js b/src/parser/statement.js index 53485072df..d2fd342ca5 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -922,15 +922,6 @@ export default class StatementParser extends ExpressionParser { const method: N.ClassMethod = memberAny; const prop: N.ClassProperty = memberAny; - if (this.hasPlugin("classPrivateProperties") && this.match(tt.hash)) { - // Private property - this.next(); - const privateProp: N.ClassPrivateProperty = memberAny; - privateProp.key = this.parseIdentifier(true); - classBody.body.push(this.parsePrivateClassProperty(privateProp)); - return; - } - let isStatic = false; if (this.match(tt.name) && this.state.value === "static") { const key = this.parseIdentifier(true); // eats 'static' @@ -960,6 +951,16 @@ export default class StatementParser extends ExpressionParser { isStatic = true; } + if (this.hasPlugin("classPrivateProperties") && this.match(tt.hash)) { + // Private property + this.next(); + const privateProp: N.ClassPrivateProperty = memberAny; + privateProp.key = this.parseIdentifier(true); + privateProp.static = isStatic; + classBody.body.push(this.parsePrivateClassProperty(privateProp)); + return; + } + this.parseClassMemberWithIsStatic(classBody, member, state, isStatic); } diff --git a/src/types.js b/src/types.js index 3af9b4ab0a..7e85d4c7f0 100644 --- a/src/types.js +++ b/src/types.js @@ -680,6 +680,7 @@ export type ClassPrivateProperty = NodeBase & { type: "ClassPrivateProperty", key: Identifier, value: ?Expression, // TODO: Not in spec that this is nullable. + static: boolean, }; export type OptClassDeclaration = ClassBase & diff --git a/test/fixtures/experimental/class-private-properties/asi-success/expected.json b/test/fixtures/experimental/class-private-properties/asi-success/expected.json index d9b6ffff9c..22a8ec7990 100644 --- a/test/fixtures/experimental/class-private-properties/asi-success/expected.json +++ b/test/fixtures/experimental/class-private-properties/asi-success/expected.json @@ -106,6 +106,7 @@ }, "name": "x" }, + "static": false, "value": null }, { @@ -139,6 +140,7 @@ }, "name": "y" }, + "static": false, "value": null } ] @@ -147,4 +149,4 @@ ], "directives": [] } -} \ No newline at end of file +} diff --git a/test/fixtures/experimental/class-private-properties/delete-non-private/expected.json b/test/fixtures/experimental/class-private-properties/delete-non-private/expected.json index bc00bb10ad..873050141d 100644 --- a/test/fixtures/experimental/class-private-properties/delete-non-private/expected.json +++ b/test/fixtures/experimental/class-private-properties/delete-non-private/expected.json @@ -106,6 +106,7 @@ }, "name": "x" }, + "static": false, "value": null }, { @@ -272,4 +273,4 @@ ], "directives": [] } -} \ No newline at end of file +} diff --git a/test/fixtures/experimental/class-private-properties/inline/expected.json b/test/fixtures/experimental/class-private-properties/inline/expected.json index 79e5aa820a..2ebb392b10 100644 --- a/test/fixtures/experimental/class-private-properties/inline/expected.json +++ b/test/fixtures/experimental/class-private-properties/inline/expected.json @@ -106,6 +106,7 @@ }, "name": "x" }, + "static": false, "value": null }, { @@ -139,6 +140,7 @@ }, "name": "y" }, + "static": false, "value": null } ] @@ -222,6 +224,7 @@ }, "name": "x" }, + "static": false, "value": { "type": "NumericLiteral", "start": 36, @@ -274,6 +277,7 @@ }, "name": "y" }, + "static": false, "value": { "type": "NumericLiteral", "start": 44, @@ -301,4 +305,4 @@ ], "directives": [] } -} \ No newline at end of file +} diff --git a/test/fixtures/experimental/class-private-properties/nested/expected.json b/test/fixtures/experimental/class-private-properties/nested/expected.json index 30bd320359..a43e99bcb9 100644 --- a/test/fixtures/experimental/class-private-properties/nested/expected.json +++ b/test/fixtures/experimental/class-private-properties/nested/expected.json @@ -106,6 +106,7 @@ }, "name": "x" }, + "static": false, "value": { "type": "NumericLiteral", "start": 23, @@ -158,6 +159,7 @@ }, "name": "y" }, + "static": false, "value": { "type": "NumericLiteral", "start": 35, @@ -677,6 +679,7 @@ }, "name": "x" }, + "static": false, "value": { "type": "NumericLiteral", "start": 151, @@ -729,6 +732,7 @@ }, "name": "y" }, + "static": false, "value": { "type": "NumericLiteral", "start": 171, @@ -3350,4 +3354,4 @@ ], "directives": [] } -} \ No newline at end of file +} diff --git a/test/fixtures/experimental/class-private-properties/pbn-success/expected.json b/test/fixtures/experimental/class-private-properties/pbn-success/expected.json index b8a4aa6558..a7b00beec5 100644 --- a/test/fixtures/experimental/class-private-properties/pbn-success/expected.json +++ b/test/fixtures/experimental/class-private-properties/pbn-success/expected.json @@ -106,6 +106,7 @@ }, "name": "x" }, + "static": false, "value": null }, { @@ -139,6 +140,7 @@ }, "name": "y" }, + "static": false, "value": null }, { @@ -1621,4 +1623,4 @@ ], "directives": [] } -} \ No newline at end of file +} diff --git a/test/fixtures/experimental/class-private-properties/static/actual.js b/test/fixtures/experimental/class-private-properties/static/actual.js new file mode 100644 index 0000000000..0b915f1208 --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/static/actual.js @@ -0,0 +1,4 @@ +class A { + static #x; + static #y = 1; +} diff --git a/test/fixtures/experimental/class-private-properties/static/expected.json b/test/fixtures/experimental/class-private-properties/static/expected.json new file mode 100644 index 0000000000..9d7c840f64 --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/static/expected.json @@ -0,0 +1,171 @@ +{ + "type": "File", + "start": 0, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "script", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "A" + }, + "name": "A" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "ClassPrivateProperty", + "start": 12, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "key": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "x" + }, + "name": "x" + }, + "static": true, + "value": null + }, + { + "type": "ClassPrivateProperty", + "start": 25, + "end": 39, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 16 + } + }, + "key": { + "type": "Identifier", + "start": 33, + "end": 34, + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 11 + }, + "identifierName": "y" + }, + "name": "y" + }, + "static": true, + "value": { + "type": "NumericLiteral", + "start": 37, + "end": 38, + "loc": { + "start": { + "line": 3, + "column": 14 + }, + "end": { + "line": 3, + "column": 15 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + ] + } + } + ], + "directives": [] + } +} diff --git a/test/fixtures/experimental/class-private-properties/static/options.json b/test/fixtures/experimental/class-private-properties/static/options.json new file mode 100644 index 0000000000..19c38d2996 --- /dev/null +++ b/test/fixtures/experimental/class-private-properties/static/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["classProperties", "classPrivateProperties"] +}