From 025e31607b336ca32d0c68deebbf26d898d3aca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 10 Jan 2020 22:27:03 -0500 Subject: [PATCH 1/7] fix: disallow private name in object member and TS type elements --- .../babel-parser/src/parser/expression.js | 23 +- packages/babel-parser/src/parser/statement.js | 2 +- packages/babel-parser/src/plugins/flow.js | 5 +- .../src/plugins/typescript/index.js | 2 +- .../invalid-destructuring-arguments/input.js | 4 + .../options.json | 3 + .../output.json | 324 +++++++++++++++++ .../invalid-destructuring/input.js | 6 + .../invalid-destructuring/options.json | 3 + .../invalid-destructuring/output.json | 344 ++++++++++++++++++ .../invalid-object-method/input.js | 3 + .../invalid-object-method/options.json | 3 + .../invalid-object-method/output.json | 226 ++++++++++++ .../invalid-ts-type-literal/input.js | 3 + .../invalid-ts-type-literal/options.json | 3 + .../invalid-ts-type-literal/output.json | 164 +++++++++ 16 files changed, 1107 insertions(+), 11 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring-arguments/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring-arguments/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring-arguments/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-object-method/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-object-method/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-object-method/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-ts-type-literal/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-ts-type-literal/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-ts-type-literal/output.json diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index e3b6eae99a0b..0b6f374a2f78 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -606,7 +606,7 @@ export default class ExpressionParser extends LValParser { ? this.parseExpression() : optional ? this.parseIdentifier(true) - : this.parseMaybePrivateName(); + : this.parseMaybePrivateName(true); node.computed = computed; if (node.property.type === "PrivateName") { @@ -1131,11 +1131,19 @@ export default class ExpressionParser extends LValParser { return this.finishNode(node, "BooleanLiteral"); } - parseMaybePrivateName(): N.PrivateName | N.Identifier { + parseMaybePrivateName( + isPrivateNameAllowed: boolean, + ): N.PrivateName | N.Identifier { const isPrivate = this.match(tt.hash); if (isPrivate) { this.expectOnePlugin(["classPrivateProperties", "classPrivateMethods"]); + if (!isPrivateNameAllowed) { + this.raise( + this.state.pos, + "Private name can only be used as name of class element (i.e. class C { #p = 42 #m() {} } )\n or property of member expression (i.e. this.#p).", + ); + } const node = this.startNode(); this.next(); this.assertNoSpace("Unexpected space between # and identifier"); @@ -1596,12 +1604,12 @@ export default class ExpressionParser extends LValParser { } const containsEsc = this.state.containsEsc; - this.parsePropertyName(prop); + this.parsePropertyName(prop, /* isPrivateNameAllowed */ false); if (!isPattern && !containsEsc && !isGenerator && this.isAsyncProp(prop)) { isAsync = true; isGenerator = this.eat(tt.star); - this.parsePropertyName(prop); + this.parsePropertyName(prop, /* isPrivateNameAllowed */ false); } else { isAsync = false; } @@ -1688,7 +1696,7 @@ export default class ExpressionParser extends LValParser { if (!containsEsc && this.isGetterOrSetterMethod(prop, isPattern)) { if (isGenerator || isAsync) this.unexpected(); prop.kind = prop.key.name; - this.parsePropertyName(prop); + this.parsePropertyName(prop, /* isPrivateNameAllowed */ false); this.parseMethod( prop, /* isGenerator */ false, @@ -1779,7 +1787,8 @@ export default class ExpressionParser extends LValParser { } parsePropertyName( - prop: N.ObjectOrClassMember | N.ClassMember | N.TsNamedTypeElementBase, + prop: N.ObjectOrClassMember | N.TsNamedTypeElementBase, + isPrivateNameAllowed: boolean, ): N.Expression | N.Identifier { if (this.eat(tt.bracketL)) { (prop: $FlowSubtype).computed = true; @@ -1792,7 +1801,7 @@ export default class ExpressionParser extends LValParser { (prop: $FlowFixMe).key = this.match(tt.num) || this.match(tt.string) || this.match(tt.bigint) ? this.parseExprAtom() - : this.parseMaybePrivateName(); + : this.parseMaybePrivateName(isPrivateNameAllowed); if (prop.key.type !== "PrivateName") { // ClassPrivateProperty is never computed, so we don't assign in that case. diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 7a5bc33c9541..85e177564676 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -1474,7 +1474,7 @@ export default class StatementParser extends ExpressionParser { } parseClassPropertyName(member: N.ClassMember): N.Expression | N.Identifier { - const key = this.parsePropertyName(member); + const key = this.parsePropertyName(member, /* isPrivateNameAllowed */ true); if ( !member.computed && diff --git a/packages/babel-parser/src/plugins/flow.js b/packages/babel-parser/src/plugins/flow.js index 72ed92b6c276..8e3a36fc6126 100644 --- a/packages/babel-parser/src/plugins/flow.js +++ b/packages/babel-parser/src/plugins/flow.js @@ -2265,10 +2265,11 @@ export default (superClass: Class): Class => } parsePropertyName( - node: N.ObjectOrClassMember | N.ClassMember | N.TsNamedTypeElementBase, + node: N.ObjectOrClassMember | N.TsNamedTypeElementBase, + isPrivateNameAllowed: boolean, ): N.Identifier { const variance = this.flowParseVariance(); - const key = super.parsePropertyName(node); + const key = super.parsePropertyName(node, isPrivateNameAllowed); // $FlowIgnore ("variance" not defined on TsNamedTypeElementBase) node.variance = variance; return key; diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 0c740b2c9bdd..b53dca72d61c 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -507,7 +507,7 @@ export default (superClass: Class): Class => return idx; } - this.parsePropertyName(node); + this.parsePropertyName(node, /* isPrivateNameAllowed */ false); return this.tsParsePropertyOrMethodSignature(node, readonly); } diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring-arguments/input.js b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring-arguments/input.js new file mode 100644 index 000000000000..efc69f1bc6eb --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring-arguments/input.js @@ -0,0 +1,4 @@ +class C { + #x = 1; + #p = ({ #x: x }) => {} +} diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring-arguments/options.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring-arguments/options.json new file mode 100644 index 000000000000..f26e916957c8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring-arguments/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["classPrivateProperties"] +} diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring-arguments/output.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring-arguments/output.json new file mode 100644 index 000000000000..1f3ff18a1ad3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring-arguments/output.json @@ -0,0 +1,324 @@ +{ + "type": "File", + "start": 0, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "errors": [ + "SyntaxError: Private name can only be used as name of class element (i.e. class C { #p = 42 #m() {} } )\n or property of member expression (i.e. this.#p). (3:11)" + ], + "program": { + "type": "Program", + "start": 0, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 46, + "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": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "ClassPrivateProperty", + "start": 12, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 9 + } + }, + "static": false, + "key": { + "type": "PrivateName", + "start": 12, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 4 + } + }, + "id": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 3 + }, + "end": { + "line": 2, + "column": 4 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "value": { + "type": "NumericLiteral", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + }, + { + "type": "ClassPrivateProperty", + "start": 22, + "end": 44, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 24 + } + }, + "static": false, + "key": { + "type": "PrivateName", + "start": 22, + "end": 24, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 4 + } + }, + "id": { + "type": "Identifier", + "start": 23, + "end": 24, + "loc": { + "start": { + "line": 3, + "column": 3 + }, + "end": { + "line": 3, + "column": 4 + }, + "identifierName": "p" + }, + "name": "p" + } + }, + "value": { + "type": "ArrowFunctionExpression", + "start": 27, + "end": 44, + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 24 + } + }, + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "ObjectPattern", + "start": 28, + "end": 37, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 17 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 30, + "end": 35, + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 15 + } + }, + "method": false, + "key": { + "type": "PrivateName", + "start": 30, + "end": 32, + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 12 + } + }, + "id": { + "type": "Identifier", + "start": 31, + "end": 32, + "loc": { + "start": { + "line": 3, + "column": 11 + }, + "end": { + "line": 3, + "column": 12 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "shorthand": false, + "value": { + "type": "Identifier", + "start": 34, + "end": 35, + "loc": { + "start": { + "line": 3, + "column": 14 + }, + "end": { + "line": 3, + "column": 15 + }, + "identifierName": "x" + }, + "name": "x" + } + } + ] + } + ], + "body": { + "type": "BlockStatement", + "start": 42, + "end": 44, + "loc": { + "start": { + "line": 3, + "column": 22 + }, + "end": { + "line": 3, + "column": 24 + } + }, + "body": [], + "directives": [] + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring/input.js b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring/input.js new file mode 100644 index 000000000000..980a785949d2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring/input.js @@ -0,0 +1,6 @@ +class C { + #x = 1; + m() { + const {#x: x} = this; + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring/options.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring/options.json new file mode 100644 index 000000000000..f26e916957c8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["classPrivateProperties"] +} diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring/output.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring/output.json new file mode 100644 index 000000000000..3f122605f601 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring/output.json @@ -0,0 +1,344 @@ +{ + "type": "File", + "start": 0, + "end": 59, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "errors": [ + "SyntaxError: Private name can only be used as name of class element (i.e. class C { #p = 42 #m() {} } )\n or property of member expression (i.e. this.#p). (4:12)" + ], + "program": { + "type": "Program", + "start": 0, + "end": 59, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 59, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 59, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "body": [ + { + "type": "ClassPrivateProperty", + "start": 12, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 9 + } + }, + "static": false, + "key": { + "type": "PrivateName", + "start": 12, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 4 + } + }, + "id": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 3 + }, + "end": { + "line": 2, + "column": 4 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "value": { + "type": "NumericLiteral", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + }, + { + "type": "ClassMethod", + "start": 22, + "end": 57, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 5, + "column": 3 + } + }, + "static": false, + "key": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 3 + }, + "identifierName": "m" + }, + "name": "m" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 26, + "end": 57, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 5, + "column": 3 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "start": 32, + "end": 53, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 25 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 38, + "end": 52, + "loc": { + "start": { + "line": 4, + "column": 10 + }, + "end": { + "line": 4, + "column": 24 + } + }, + "id": { + "type": "ObjectPattern", + "start": 38, + "end": 45, + "loc": { + "start": { + "line": 4, + "column": 10 + }, + "end": { + "line": 4, + "column": 17 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 39, + "end": 44, + "loc": { + "start": { + "line": 4, + "column": 11 + }, + "end": { + "line": 4, + "column": 16 + } + }, + "method": false, + "key": { + "type": "PrivateName", + "start": 39, + "end": 41, + "loc": { + "start": { + "line": 4, + "column": 11 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "id": { + "type": "Identifier", + "start": 40, + "end": 41, + "loc": { + "start": { + "line": 4, + "column": 12 + }, + "end": { + "line": 4, + "column": 13 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "shorthand": false, + "value": { + "type": "Identifier", + "start": 43, + "end": 44, + "loc": { + "start": { + "line": 4, + "column": 15 + }, + "end": { + "line": 4, + "column": 16 + }, + "identifierName": "x" + }, + "name": "x" + } + } + ] + }, + "init": { + "type": "ThisExpression", + "start": 48, + "end": 52, + "loc": { + "start": { + "line": 4, + "column": 20 + }, + "end": { + "line": 4, + "column": 24 + } + } + } + } + ], + "kind": "const" + } + ], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-object-method/input.js b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-object-method/input.js new file mode 100644 index 000000000000..234c9a49371e --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-object-method/input.js @@ -0,0 +1,3 @@ +class C { + #p = ({ #x: 42 }); +} diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-object-method/options.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-object-method/options.json new file mode 100644 index 000000000000..f26e916957c8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-object-method/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["classPrivateProperties"] +} diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-object-method/output.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-object-method/output.json new file mode 100644 index 000000000000..ca1e59d2e100 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-object-method/output.json @@ -0,0 +1,226 @@ +{ + "type": "File", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "errors": [ + "SyntaxError: Private name can only be used as name of class element (i.e. class C { #p = 42 #m() {} } )\n or property of member expression (i.e. this.#p). (2:11)" + ], + "program": { + "type": "Program", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassPrivateProperty", + "start": 12, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "static": false, + "key": { + "type": "PrivateName", + "start": 12, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 4 + } + }, + "id": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 3 + }, + "end": { + "line": 2, + "column": 4 + }, + "identifierName": "p" + }, + "name": "p" + } + }, + "value": { + "type": "ObjectExpression", + "start": 18, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 20, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "method": false, + "key": { + "type": "PrivateName", + "start": 20, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "id": { + "type": "Identifier", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 12 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "shorthand": false, + "value": { + "type": "NumericLiteral", + "start": 24, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "extra": { + "rawValue": 42, + "raw": "42" + }, + "value": 42 + } + } + ], + "extra": { + "parenthesized": true, + "parenStart": 17 + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-ts-type-literal/input.js b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-ts-type-literal/input.js new file mode 100644 index 000000000000..dd79bd88301a --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-ts-type-literal/input.js @@ -0,0 +1,3 @@ +interface I { + #p: string +} diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-ts-type-literal/options.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-ts-type-literal/options.json new file mode 100644 index 000000000000..cb835ce4233c --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-ts-type-literal/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["classPrivateProperties", "typescript"] +} diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-ts-type-literal/output.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-ts-type-literal/output.json new file mode 100644 index 000000000000..745fa8b29953 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-ts-type-literal/output.json @@ -0,0 +1,164 @@ +{ + "type": "File", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "errors": [ + "SyntaxError: Private name can only be used as name of class element (i.e. class C { #p = 42 #m() {} } )\n or property of member expression (i.e. this.#p). (2:3)" + ], + "program": { + "type": "Program", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSPropertySignature", + "start": 16, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "key": { + "type": "PrivateName", + "start": 16, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 4 + } + }, + "id": { + "type": "Identifier", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 3 + }, + "end": { + "line": 2, + "column": 4 + }, + "identifierName": "p" + }, + "name": "p" + } + }, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 18, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 20, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 12 + } + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file From 95abf5ee96d35e7ae9a5e60b3144c081e928908e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 10 Jan 2020 22:27:36 -0500 Subject: [PATCH 2/7] chore: update test262 whitelist --- scripts/parser-tests/test262/whitelist.txt | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/scripts/parser-tests/test262/whitelist.txt b/scripts/parser-tests/test262/whitelist.txt index d7a38c2104cf..8b137891791f 100644 --- a/scripts/parser-tests/test262/whitelist.txt +++ b/scripts/parser-tests/test262/whitelist.txt @@ -1,16 +1 @@ -language/expressions/class/elements/syntax/early-errors/grammar-private-field-on-object-destructuring.js(default) -language/expressions/class/elements/syntax/early-errors/grammar-private-field-on-object-destructuring.js(strict mode) -language/expressions/object/method-definition/private-name-early-error-async-gen-inside-class.js(default) -language/expressions/object/method-definition/private-name-early-error-async-gen-inside-class.js(strict mode) -language/expressions/object/method-definition/private-name-early-error-async-gen.js(default) -language/expressions/object/method-definition/private-name-early-error-async-gen.js(strict mode) -language/expressions/object/method-definition/private-name-early-error-gen-inside-class.js(default) -language/expressions/object/method-definition/private-name-early-error-gen-inside-class.js(strict mode) -language/expressions/object/method-definition/private-name-early-error-gen.js(default) -language/expressions/object/method-definition/private-name-early-error-gen.js(strict mode) -language/expressions/object/method-definition/private-name-early-error-method-inside-class.js(default) -language/expressions/object/method-definition/private-name-early-error-method-inside-class.js(strict mode) -language/expressions/object/method-definition/private-name-early-error-method.js(default) -language/expressions/object/method-definition/private-name-early-error-method.js(strict mode) -language/statements/class/elements/syntax/early-errors/grammar-private-field-on-object-destructuring.js(default) -language/statements/class/elements/syntax/early-errors/grammar-private-field-on-object-destructuring.js(strict mode) + From 7b46e2efeeaafdfdced474976583ccc3282805cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 10 Jan 2020 22:52:34 -0500 Subject: [PATCH 3/7] chore: make flow happy --- packages/babel-parser/src/parser/expression.js | 2 +- packages/babel-parser/src/plugins/flow.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 0b6f374a2f78..143f30fa26d0 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -1787,7 +1787,7 @@ export default class ExpressionParser extends LValParser { } parsePropertyName( - prop: N.ObjectOrClassMember | N.TsNamedTypeElementBase, + prop: N.ObjectOrClassMember | N.ClassMember | N.TsNamedTypeElementBase, isPrivateNameAllowed: boolean, ): N.Expression | N.Identifier { if (this.eat(tt.bracketL)) { diff --git a/packages/babel-parser/src/plugins/flow.js b/packages/babel-parser/src/plugins/flow.js index 8e3a36fc6126..42f864a2069d 100644 --- a/packages/babel-parser/src/plugins/flow.js +++ b/packages/babel-parser/src/plugins/flow.js @@ -2265,7 +2265,7 @@ export default (superClass: Class): Class => } parsePropertyName( - node: N.ObjectOrClassMember | N.TsNamedTypeElementBase, + node: N.ObjectOrClassMember | N.ClassMember | N.TsNamedTypeElementBase, isPrivateNameAllowed: boolean, ): N.Identifier { const variance = this.flowParseVariance(); From 0df20ba9227839e381426c40cb7e3a231178d4a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Sat, 11 Jan 2020 08:20:48 -0500 Subject: [PATCH 4/7] Update packages/babel-parser/src/parser/expression.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Nicolò Ribaudo --- packages/babel-parser/src/parser/expression.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 143f30fa26d0..174929c6b374 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -1141,7 +1141,7 @@ export default class ExpressionParser extends LValParser { if (!isPrivateNameAllowed) { this.raise( this.state.pos, - "Private name can only be used as name of class element (i.e. class C { #p = 42 #m() {} } )\n or property of member expression (i.e. this.#p).", + "Private name can only be used as name of class element (i.e. class C { #p = 42; #m() {} } )\n or property of member expression (i.e. this.#p).", ); } const node = this.startNode(); From e7619cebbd735760d1ccb3399d2f76092e5805f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Sat, 11 Jan 2020 09:46:46 -0500 Subject: [PATCH 5/7] chore: update test fixtures --- .../invalid-destructuring-arguments/output.json | 2 +- .../class-private-properties/invalid-destructuring/output.json | 2 +- .../class-private-properties/invalid-object-method/output.json | 2 +- .../invalid-ts-type-literal/output.json | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring-arguments/output.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring-arguments/output.json index 1f3ff18a1ad3..fa7d3d75cbb7 100644 --- a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring-arguments/output.json +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring-arguments/output.json @@ -13,7 +13,7 @@ } }, "errors": [ - "SyntaxError: Private name can only be used as name of class element (i.e. class C { #p = 42 #m() {} } )\n or property of member expression (i.e. this.#p). (3:11)" + "SyntaxError: Private name can only be used as name of class element (i.e. class C { #p = 42; #m() {} } )\n or property of member expression (i.e. this.#p). (3:11)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring/output.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring/output.json index 3f122605f601..e179ce4b9755 100644 --- a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring/output.json +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring/output.json @@ -13,7 +13,7 @@ } }, "errors": [ - "SyntaxError: Private name can only be used as name of class element (i.e. class C { #p = 42 #m() {} } )\n or property of member expression (i.e. this.#p). (4:12)" + "SyntaxError: Private name can only be used as name of class element (i.e. class C { #p = 42; #m() {} } )\n or property of member expression (i.e. this.#p). (4:12)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-object-method/output.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-object-method/output.json index ca1e59d2e100..7155638a512a 100644 --- a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-object-method/output.json +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-object-method/output.json @@ -13,7 +13,7 @@ } }, "errors": [ - "SyntaxError: Private name can only be used as name of class element (i.e. class C { #p = 42 #m() {} } )\n or property of member expression (i.e. this.#p). (2:11)" + "SyntaxError: Private name can only be used as name of class element (i.e. class C { #p = 42; #m() {} } )\n or property of member expression (i.e. this.#p). (2:11)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-ts-type-literal/output.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-ts-type-literal/output.json index 745fa8b29953..9e42a92de0a9 100644 --- a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-ts-type-literal/output.json +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-ts-type-literal/output.json @@ -13,7 +13,7 @@ } }, "errors": [ - "SyntaxError: Private name can only be used as name of class element (i.e. class C { #p = 42 #m() {} } )\n or property of member expression (i.e. this.#p). (2:3)" + "SyntaxError: Private name can only be used as name of class element (i.e. class C { #p = 42; #m() {} } )\n or property of member expression (i.e. this.#p). (2:3)" ], "program": { "type": "Program", From e31d211820affc4a0455129b003f9546596e6aae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Sat, 11 Jan 2020 10:12:28 -0500 Subject: [PATCH 6/7] Update packages/babel-parser/src/parser/expression.js Co-Authored-By: Brian Ng --- packages/babel-parser/src/parser/expression.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 174929c6b374..d88bf86266d0 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -1141,7 +1141,7 @@ export default class ExpressionParser extends LValParser { if (!isPrivateNameAllowed) { this.raise( this.state.pos, - "Private name can only be used as name of class element (i.e. class C { #p = 42; #m() {} } )\n or property of member expression (i.e. this.#p).", + "Private names can only be used as the name of a class element (i.e. class C { #p = 42; #m() {} } )\n or a property of member expression (i.e. this.#p).", ); } const node = this.startNode(); From a93879f626ae783d99dbf44e49dcb667ede9789a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Sat, 11 Jan 2020 10:13:22 -0500 Subject: [PATCH 7/7] chore: update test fixtures --- .../invalid-destructuring-arguments/output.json | 2 +- .../class-private-properties/invalid-destructuring/output.json | 2 +- .../class-private-properties/invalid-object-method/output.json | 2 +- .../invalid-ts-type-literal/output.json | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring-arguments/output.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring-arguments/output.json index fa7d3d75cbb7..d8c6109cf982 100644 --- a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring-arguments/output.json +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring-arguments/output.json @@ -13,7 +13,7 @@ } }, "errors": [ - "SyntaxError: Private name can only be used as name of class element (i.e. class C { #p = 42; #m() {} } )\n or property of member expression (i.e. this.#p). (3:11)" + "SyntaxError: Private names can only be used as the name of a class element (i.e. class C { #p = 42; #m() {} } )\n or a property of member expression (i.e. this.#p). (3:11)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring/output.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring/output.json index e179ce4b9755..73b7c2579453 100644 --- a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring/output.json +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-destructuring/output.json @@ -13,7 +13,7 @@ } }, "errors": [ - "SyntaxError: Private name can only be used as name of class element (i.e. class C { #p = 42; #m() {} } )\n or property of member expression (i.e. this.#p). (4:12)" + "SyntaxError: Private names can only be used as the name of a class element (i.e. class C { #p = 42; #m() {} } )\n or a property of member expression (i.e. this.#p). (4:12)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-object-method/output.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-object-method/output.json index 7155638a512a..7a5716ee676d 100644 --- a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-object-method/output.json +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-object-method/output.json @@ -13,7 +13,7 @@ } }, "errors": [ - "SyntaxError: Private name can only be used as name of class element (i.e. class C { #p = 42; #m() {} } )\n or property of member expression (i.e. this.#p). (2:11)" + "SyntaxError: Private names can only be used as the name of a class element (i.e. class C { #p = 42; #m() {} } )\n or a property of member expression (i.e. this.#p). (2:11)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-ts-type-literal/output.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-ts-type-literal/output.json index 9e42a92de0a9..3422be4603b9 100644 --- a/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-ts-type-literal/output.json +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/invalid-ts-type-literal/output.json @@ -13,7 +13,7 @@ } }, "errors": [ - "SyntaxError: Private name can only be used as name of class element (i.e. class C { #p = 42; #m() {} } )\n or property of member expression (i.e. this.#p). (2:3)" + "SyntaxError: Private names can only be used as the name of a class element (i.e. class C { #p = 42; #m() {} } )\n or a property of member expression (i.e. this.#p). (2:3)" ], "program": { "type": "Program",