diff --git a/packages/babel-generator/test/fixtures/types/PrivateName/input.js b/packages/babel-generator/test/fixtures/types/PrivateName/input.js new file mode 100644 index 000000000000..9ef0a7906993 --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/PrivateName/input.js @@ -0,0 +1,7 @@ +class C { + #x; + m() { + #x in (#x in this); + var { #x: x } = this; + } +} diff --git a/packages/babel-generator/test/fixtures/types/PrivateName/options.json b/packages/babel-generator/test/fixtures/types/PrivateName/options.json new file mode 100644 index 000000000000..2271198464b8 --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/PrivateName/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["destructuringPrivate"] +} diff --git a/packages/babel-generator/test/fixtures/types/PrivateName/output.js b/packages/babel-generator/test/fixtures/types/PrivateName/output.js new file mode 100644 index 000000000000..e37a9fb1e7f1 --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/PrivateName/output.js @@ -0,0 +1,11 @@ +class C { + #x; + + m() { + #x in (#x in this); + var { + #x: x + } = this; + } + +} \ No newline at end of file diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index cab67f8f80cf..fd68c7c107ac 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -78,6 +78,7 @@ import { cloneIdentifier } from "./node"; /*:: import type { SourceType } from "../options"; +declare var invariant; */ const invalidHackPipeBodies = new Map([ @@ -329,6 +330,14 @@ export default class ExpressionParser extends LValParser { ) { refExpressionErrors.shorthandAssignLoc = null; // reset because shorthand default was used correctly } + if ( + refExpressionErrors.privateKeyLoc != null && + // $FlowIgnore[incompatible-type] We know this exists, so it can't be undefined. + indexes.get(refExpressionErrors.privateKeyLoc) >= startPos + ) { + this.checkDestructuringPrivate(refExpressionErrors); + refExpressionErrors.privateKeyLoc = null; // reset because `({ #x: x })` is an assignable pattern + } } else { node.left = left; } @@ -843,13 +852,14 @@ export default class ExpressionParser extends LValParser { let node = this.startNodeAt(startPos, startLoc); node.callee = base; + const { maybeAsyncArrow, optionalChainMember } = state; - if (state.maybeAsyncArrow) { + if (maybeAsyncArrow) { this.expressionScope.enter(newAsyncArrowScope()); refExpressionErrors = new ExpressionErrors(); } - if (state.optionalChainMember) { + if (optionalChainMember) { node.optional = optional; } @@ -864,10 +874,12 @@ export default class ExpressionParser extends LValParser { refExpressionErrors, ); } - this.finishCallExpression(node, state.optionalChainMember); + this.finishCallExpression(node, optionalChainMember); - if (state.maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) { + if (maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) { + /*:: invariant(refExpressionErrors != null) */ state.stop = true; + this.checkDestructuringPrivate(refExpressionErrors); this.expressionScope.validateAsPattern(); this.expressionScope.exit(); node = this.parseAsyncArrowFromCallExpression( @@ -875,7 +887,7 @@ export default class ExpressionParser extends LValParser { node, ); } else { - if (state.maybeAsyncArrow) { + if (maybeAsyncArrow) { this.checkExpressionErrors(refExpressionErrors, true); this.expressionScope.exit(); } @@ -1738,6 +1750,7 @@ export default class ExpressionParser extends LValParser { this.shouldParseArrow(exprList) && (arrowNode = this.parseArrow(arrowNode)) ) { + this.checkDestructuringPrivate(refExpressionErrors); this.expressionScope.validateAsPattern(); this.expressionScope.exit(); this.parseArrowExpression(arrowNode, exprList, false); @@ -2040,7 +2053,7 @@ export default class ExpressionParser extends LValParser { let isGenerator = this.eat(tt.star); this.parsePropertyNamePrefixOperator(prop); const containsEsc = this.state.containsEsc; - const key = this.parsePropertyName(prop); + const key = this.parsePropertyName(prop, refExpressionErrors); if (!isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) { const keyName = key.name; @@ -2245,8 +2258,12 @@ export default class ExpressionParser extends LValParser { return node; } + // https://tc39.es/ecma262/#prod-PropertyName + // when refExpressionErrors presents, it will parse private name + // and record the position of the first private name parsePropertyName( prop: N.ObjectOrClassMember | N.ClassMember | N.TsNamedTypeElementBase, + refExpressionErrors?: ?ExpressionErrors, ): N.Expression | N.Identifier { if (this.eat(tt.bracketL)) { (prop: $FlowSubtype).computed = true; @@ -2275,10 +2292,16 @@ export default class ExpressionParser extends LValParser { break; case tt.privateName: { // the class private key has been handled in parseClassElementName - this.raise(Errors.UnexpectedPrivateField, { - // FIXME: explain - at: createPositionWithColumnOffset(this.state.startLoc, 1), - }); + const privateKeyLoc = this.state.startLoc; + if (refExpressionErrors != null) { + if (refExpressionErrors.privateKeyLoc === null) { + refExpressionErrors.privateKeyLoc = privateKeyLoc; + } + } else { + this.raise(Errors.UnexpectedPrivateField, { + at: privateKeyLoc, + }); + } key = this.parsePrivateName(); break; } diff --git a/packages/babel-parser/src/parser/lval.js b/packages/babel-parser/src/parser/lval.js index 68ffc6418c5e..28a99666dc6f 100644 --- a/packages/babel-parser/src/parser/lval.js +++ b/packages/babel-parser/src/parser/lval.js @@ -16,6 +16,7 @@ import type { /*:: ObjectMember, */ /*:: TsNamedTypeElementBase, */ /*:: Identifier, */ + /*:: PrivateName, */ /*:: ObjectExpression, */ /*:: ObjectPattern, */ } from "../types"; @@ -63,6 +64,7 @@ export default class LValParser extends NodeUtils { +parsePropertyName: ( prop: ObjectOrClassMember | ClassMember | TsNamedTypeElementBase, ) => Expression | Identifier; + +parsePrivateName: () => PrivateName */ // Forward-declaration: defined in statement.js /*:: @@ -143,9 +145,14 @@ export default class LValParser extends NodeUtils { } break; - case "ObjectProperty": - this.toAssignable(node.value, isLHS); + case "ObjectProperty": { + const { key, value } = node; + if (this.isPrivateName(key)) { + this.classScope.usePrivateName(this.getPrivateNameSV(key), key.start); + } + this.toAssignable(value, isLHS); break; + } case "SpreadElement": { this.checkToRestConversion(node); @@ -427,6 +434,10 @@ export default class LValParser extends NodeUtils { const { type, start: startPos, startLoc } = this.state; if (type === tt.ellipsis) { return this.parseBindingRestProperty(prop); + } else if (type === tt.privateName) { + this.expectPlugin("destructuringPrivate", startLoc); + this.classScope.usePrivateName(this.state.value, startPos); + prop.key = this.parsePrivateName(); } else { this.parsePropertyName(prop); } diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index b04bc4b69e75..20677204baf1 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -726,6 +726,7 @@ export default class StatementParser extends ExpressionParser { } } if (isForOf || this.match(tt._in)) { + this.checkDestructuringPrivate(refExpressionErrors); this.toAssignable(init, /* isLHS */ true); const description = isForOf ? "for-of statement" : "for-in statement"; this.checkLVal(init, description); diff --git a/packages/babel-parser/src/parser/util.js b/packages/babel-parser/src/parser/util.js index 3da813b6e86d..b36cec1590c4 100644 --- a/packages/babel-parser/src/parser/util.js +++ b/packages/babel-parser/src/parser/util.js @@ -285,11 +285,18 @@ export default class UtilParser extends Tokenizer { andThrow: boolean, ) { if (!refExpressionErrors) return false; - const { shorthandAssignLoc, doubleProtoLoc, optionalParametersLoc } = - refExpressionErrors; + const { + shorthandAssignLoc, + doubleProtoLoc, + privateKeyLoc, + optionalParametersLoc, + } = refExpressionErrors; const hasErrors = - !!shorthandAssignLoc || !!doubleProtoLoc || !!optionalParametersLoc; + !!shorthandAssignLoc || + !!doubleProtoLoc || + !!optionalParametersLoc || + !!privateKeyLoc; if (!andThrow) { return hasErrors; @@ -305,6 +312,10 @@ export default class UtilParser extends Tokenizer { this.raise(Errors.DuplicateProto, { at: doubleProtoLoc }); } + if (privateKeyLoc != null) { + this.raise(Errors.UnexpectedPrivateField, { at: privateKeyLoc }); + } + if (optionalParametersLoc != null) { this.unexpected(optionalParametersLoc); } @@ -417,6 +428,13 @@ export default class UtilParser extends Tokenizer { this.scope.enter(SCOPE_PROGRAM); this.prodParam.enter(paramFlags); } + + checkDestructuringPrivate(refExpressionErrors: ExpressionErrors) { + const { privateKeyLoc } = refExpressionErrors; + if (privateKeyLoc !== null) { + this.expectPlugin("destructuringPrivate", privateKeyLoc); + } + } } /** @@ -428,11 +446,13 @@ export default class UtilParser extends Tokenizer { * * - **shorthandAssignLoc**: track initializer `=` position * - **doubleProtoLoc**: track the duplicate `__proto__` key position + * - **privateKey**: track private key `#p` position * - **optionalParametersLoc**: track the optional paramter (`?`). * It's only used by typescript and flow plugins */ export class ExpressionErrors { shorthandAssignLoc: ?Position = null; doubleProtoLoc: ?Position = null; + privateKeyLoc: ?Position = null; optionalParametersLoc: ?Position = null; } diff --git a/packages/babel-parser/src/plugins/estree.js b/packages/babel-parser/src/plugins/estree.js index 471066293a70..dacb6823f556 100644 --- a/packages/babel-parser/src/plugins/estree.js +++ b/packages/babel-parser/src/plugins/estree.js @@ -330,8 +330,11 @@ export default (superClass: Class): Class => toAssignable(node: N.Node, isLHS: boolean = false): N.Node { if (node != null && this.isObjectProperty(node)) { - this.toAssignable(node.value, isLHS); - + const { key, value } = node; + if (this.isPrivateName(key)) { + this.classScope.usePrivateName(this.getPrivateNameSV(key), key.start); + } + this.toAssignable(value, isLHS); return node; } diff --git a/packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-object-method/output.json b/packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-object-method/output.json index a7446d847fd7..94acff77ae7e 100644 --- a/packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-object-method/output.json +++ b/packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-object-method/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, "errors": [ - "SyntaxError: Unexpected private name. (2:11)" + "SyntaxError: Unexpected private name. (2:10)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-object-property-with-assignment/input.js b/packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-object-property-with-assignment/input.js new file mode 100644 index 000000000000..f355e57e2918 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-object-property-with-assignment/input.js @@ -0,0 +1,7 @@ +(class { + #x; + m = { + #x: x, + y: y = m + } +}) diff --git a/packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-object-property-with-assignment/output.json b/packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-object-property-with-assignment/output.json new file mode 100644 index 000000000000..9558f0c81d18 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-object-property-with-assignment/output.json @@ -0,0 +1,115 @@ +{ + "type": "File", + "start":0,"end":53,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":2}}, + "errors": [ + "SyntaxError: Unexpected private name. (4:4)" + ], + "program": { + "type": "Program", + "start":0,"end":53,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":2}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":53,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":2}}, + "expression": { + "type": "ClassExpression", + "start":1,"end":52,"loc":{"start":{"line":1,"column":1},"end":{"line":7,"column":1}}, + "id": null, + "superClass": null, + "body": { + "type": "ClassBody", + "start":7,"end":52,"loc":{"start":{"line":1,"column":7},"end":{"line":7,"column":1}}, + "body": [ + { + "type": "ClassPrivateProperty", + "start":11,"end":14,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":5}}, + "static": false, + "key": { + "type": "PrivateName", + "start":11,"end":13,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":4}}, + "id": { + "type": "Identifier", + "start":12,"end":13,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":4},"identifierName":"x"}, + "name": "x" + } + }, + "value": null + }, + { + "type": "ClassProperty", + "start":17,"end":50,"loc":{"start":{"line":3,"column":2},"end":{"line":6,"column":3}}, + "static": false, + "key": { + "type": "Identifier", + "start":17,"end":18,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":3},"identifierName":"m"}, + "name": "m" + }, + "computed": false, + "value": { + "type": "ObjectExpression", + "start":21,"end":50,"loc":{"start":{"line":3,"column":6},"end":{"line":6,"column":3}}, + "properties": [ + { + "type": "ObjectProperty", + "start":27,"end":32,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":9}}, + "method": false, + "key": { + "type": "PrivateName", + "start":27,"end":29,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":6}}, + "id": { + "type": "Identifier", + "start":28,"end":29,"loc":{"start":{"line":4,"column":5},"end":{"line":4,"column":6},"identifierName":"x"}, + "name": "x" + } + }, + "shorthand": false, + "value": { + "type": "Identifier", + "start":31,"end":32,"loc":{"start":{"line":4,"column":8},"end":{"line":4,"column":9},"identifierName":"x"}, + "name": "x" + } + }, + { + "type": "ObjectProperty", + "start":38,"end":46,"loc":{"start":{"line":5,"column":4},"end":{"line":5,"column":12}}, + "method": false, + "key": { + "type": "Identifier", + "start":38,"end":39,"loc":{"start":{"line":5,"column":4},"end":{"line":5,"column":5},"identifierName":"y"}, + "name": "y" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "AssignmentExpression", + "start":41,"end":46,"loc":{"start":{"line":5,"column":7},"end":{"line":5,"column":12}}, + "operator": "=", + "left": { + "type": "Identifier", + "start":41,"end":42,"loc":{"start":{"line":5,"column":7},"end":{"line":5,"column":8},"identifierName":"y"}, + "name": "y" + }, + "right": { + "type": "Identifier", + "start":45,"end":46,"loc":{"start":{"line":5,"column":11},"end":{"line":5,"column":12},"identifierName":"m"}, + "name": "m" + } + } + } + ] + } + } + ] + }, + "extra": { + "parenthesized": true, + "parenStart": 0 + } + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-object-property/output.json b/packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-object-property/output.json index f3622c7378cb..1c5f0dfe3831 100644 --- a/packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-object-property/output.json +++ b/packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-object-property/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, "errors": [ - "SyntaxError: Unexpected private name. (2:11)" + "SyntaxError: Unexpected private name. (2:10)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-ts-type-literal/output.json b/packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-ts-type-literal/output.json index c53dd5371ec0..0709c7f3e934 100644 --- a/packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-ts-type-literal/output.json +++ b/packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-ts-type-literal/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, "errors": [ - "SyntaxError: Unexpected private name. (2:3)" + "SyntaxError: Unexpected private name. (2:2)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-destructuring-arguments/input.js b/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-arrow-params/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-destructuring-arguments/input.js rename to packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-arrow-params/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-arrow-params/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-arrow-params/options.json new file mode 100644 index 000000000000..4900ead27c65 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-arrow-params/options.json @@ -0,0 +1,3 @@ +{ + "throws": "This experimental syntax requires enabling the parser plugin: \"destructuringPrivate\". (3:10)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-assignment/input.js b/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-assignment/input.js new file mode 100644 index 000000000000..94276f376591 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-assignment/input.js @@ -0,0 +1,7 @@ +class C { + #x = 1; + m() { + let x; + ({ #x: x } = this); + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-assignment/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-assignment/options.json new file mode 100644 index 000000000000..7b8402241fec --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-assignment/options.json @@ -0,0 +1,3 @@ +{ + "throws": "This experimental syntax requires enabling the parser plugin: \"destructuringPrivate\". (5:7)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-async-arrow-params/input.js b/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-async-arrow-params/input.js new file mode 100644 index 000000000000..935b7e1f032c --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-async-arrow-params/input.js @@ -0,0 +1,4 @@ +class C { + #x = 1; + #p = async ({ #x: x }) => {} +} diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-async-arrow-params/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-async-arrow-params/options.json new file mode 100644 index 000000000000..e40feacc4fb2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-async-arrow-params/options.json @@ -0,0 +1,3 @@ +{ + "throws": "This experimental syntax requires enabling the parser plugin: \"destructuringPrivate\". (3:16)" +} diff --git a/packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-destructuring/input.js b/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-bindings/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/es2022/class-private-properties/invalid-destructuring/input.js rename to packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-bindings/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-bindings/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-bindings/options.json new file mode 100644 index 000000000000..d52a78ea5442 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-bindings/options.json @@ -0,0 +1,3 @@ +{ + "throws": "This experimental syntax requires enabling the parser plugin: \"destructuringPrivate\". (4:11)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-for-lhs/input.js b/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-for-lhs/input.js new file mode 100644 index 000000000000..b9bc462870ac --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-for-lhs/input.js @@ -0,0 +1,7 @@ +class C { + #x = 1; + m() { + let x; + for ({#x: x} of [this]); + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-for-lhs/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-for-lhs/options.json new file mode 100644 index 000000000000..0d1bb731bf4f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/destructuring-private-for-lhs/options.json @@ -0,0 +1,3 @@ +{ + "throws": "This experimental syntax requires enabling the parser plugin: \"destructuringPrivate\". (5:10)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-async-call-arguments/input.js b/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-async-call-arguments/input.js new file mode 100644 index 000000000000..ade58eccfb28 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-async-call-arguments/input.js @@ -0,0 +1,4 @@ +class C { + #x = 1; + #p = async ({ #x: x }) +} diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-async-call-arguments/output.json b/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-async-call-arguments/output.json new file mode 100644 index 000000000000..5f6d3cab03d0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-async-call-arguments/output.json @@ -0,0 +1,106 @@ +{ + "type": "File", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}}, + "errors": [ + "SyntaxError: Unexpected private name. (3:16)" + ], + "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": "CallExpression", + "start":27,"end":44,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":24}}, + "callee": { + "type": "Identifier", + "start":27,"end":32,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":12},"identifierName":"async"}, + "name": "async" + }, + "arguments": [ + { + "type": "ObjectExpression", + "start":34,"end":43,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":23}}, + "properties": [ + { + "type": "ObjectProperty", + "start":36,"end":41,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":21}}, + "method": false, + "key": { + "type": "PrivateName", + "start":36,"end":38,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":18}}, + "id": { + "type": "Identifier", + "start":37,"end":38,"loc":{"start":{"line":3,"column":17},"end":{"line":3,"column":18},"identifierName":"x"}, + "name": "x" + } + }, + "shorthand": false, + "value": { + "type": "Identifier", + "start":40,"end":41,"loc":{"start":{"line":3,"column":20},"end":{"line":3,"column":21},"identifierName":"x"}, + "name": "x" + } + } + ] + } + ] + } + } + ] + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-object-literal/input.js b/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-object-literal/input.js new file mode 100644 index 000000000000..f355e57e2918 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-object-literal/input.js @@ -0,0 +1,7 @@ +(class { + #x; + m = { + #x: x, + y: y = m + } +}) diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-object-literal/output.json b/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-object-literal/output.json new file mode 100644 index 000000000000..9558f0c81d18 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-object-literal/output.json @@ -0,0 +1,115 @@ +{ + "type": "File", + "start":0,"end":53,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":2}}, + "errors": [ + "SyntaxError: Unexpected private name. (4:4)" + ], + "program": { + "type": "Program", + "start":0,"end":53,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":2}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":53,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":2}}, + "expression": { + "type": "ClassExpression", + "start":1,"end":52,"loc":{"start":{"line":1,"column":1},"end":{"line":7,"column":1}}, + "id": null, + "superClass": null, + "body": { + "type": "ClassBody", + "start":7,"end":52,"loc":{"start":{"line":1,"column":7},"end":{"line":7,"column":1}}, + "body": [ + { + "type": "ClassPrivateProperty", + "start":11,"end":14,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":5}}, + "static": false, + "key": { + "type": "PrivateName", + "start":11,"end":13,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":4}}, + "id": { + "type": "Identifier", + "start":12,"end":13,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":4},"identifierName":"x"}, + "name": "x" + } + }, + "value": null + }, + { + "type": "ClassProperty", + "start":17,"end":50,"loc":{"start":{"line":3,"column":2},"end":{"line":6,"column":3}}, + "static": false, + "key": { + "type": "Identifier", + "start":17,"end":18,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":3},"identifierName":"m"}, + "name": "m" + }, + "computed": false, + "value": { + "type": "ObjectExpression", + "start":21,"end":50,"loc":{"start":{"line":3,"column":6},"end":{"line":6,"column":3}}, + "properties": [ + { + "type": "ObjectProperty", + "start":27,"end":32,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":9}}, + "method": false, + "key": { + "type": "PrivateName", + "start":27,"end":29,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":6}}, + "id": { + "type": "Identifier", + "start":28,"end":29,"loc":{"start":{"line":4,"column":5},"end":{"line":4,"column":6},"identifierName":"x"}, + "name": "x" + } + }, + "shorthand": false, + "value": { + "type": "Identifier", + "start":31,"end":32,"loc":{"start":{"line":4,"column":8},"end":{"line":4,"column":9},"identifierName":"x"}, + "name": "x" + } + }, + { + "type": "ObjectProperty", + "start":38,"end":46,"loc":{"start":{"line":5,"column":4},"end":{"line":5,"column":12}}, + "method": false, + "key": { + "type": "Identifier", + "start":38,"end":39,"loc":{"start":{"line":5,"column":4},"end":{"line":5,"column":5},"identifierName":"y"}, + "name": "y" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "AssignmentExpression", + "start":41,"end":46,"loc":{"start":{"line":5,"column":7},"end":{"line":5,"column":12}}, + "operator": "=", + "left": { + "type": "Identifier", + "start":41,"end":42,"loc":{"start":{"line":5,"column":7},"end":{"line":5,"column":8},"identifierName":"y"}, + "name": "y" + }, + "right": { + "type": "Identifier", + "start":45,"end":46,"loc":{"start":{"line":5,"column":11},"end":{"line":5,"column":12},"identifierName":"m"}, + "name": "m" + } + } + } + ] + } + } + ] + }, + "extra": { + "parenthesized": true, + "parenStart": 0 + } + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-parenthesized-expression/input.js b/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-parenthesized-expression/input.js new file mode 100644 index 000000000000..73041553fce7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-parenthesized-expression/input.js @@ -0,0 +1,4 @@ +class C { + #x = 1; + #p = ({ #x: x }) +} diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-parenthesized-expression/output.json b/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-parenthesized-expression/output.json new file mode 100644 index 000000000000..6b8edad71062 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-parenthesized-expression/output.json @@ -0,0 +1,99 @@ +{ + "type": "File", + "start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}}, + "errors": [ + "SyntaxError: Unexpected private name. (3:10)" + ], + "program": { + "type": "Program", + "start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":40,"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":40,"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":38,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":18}}, + "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": "ObjectExpression", + "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" + } + } + ], + "extra": { + "parenthesized": true, + "parenStart": 27 + } + } + } + ] + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-shorthand/input.js b/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-shorthand/input.js new file mode 100644 index 000000000000..38527cd2e83e --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-shorthand/input.js @@ -0,0 +1,4 @@ +class C { + #x = 1; + #p = ({ #x }) => {} +} diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-shorthand/options.json b/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-shorthand/options.json new file mode 100644 index 000000000000..7dcf4212d594 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-shorthand/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token (3:13)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-undefined-private-name/input.js b/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-undefined-private-name/input.js new file mode 100644 index 000000000000..a6893a8f3e94 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-undefined-private-name/input.js @@ -0,0 +1,9 @@ +class C { + m() { + ({#a: a}) => {}; + async ({#b: b}) => {}; + var {#c: c} = {}; + for ({#d: d} in {}); + ({ #e: c } = {}); + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-undefined-private-name/output.json b/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-undefined-private-name/output.json new file mode 100644 index 000000000000..08ef2ace4aba --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/invalid-undefined-private-name/output.json @@ -0,0 +1,274 @@ +{ + "type": "File", + "start":0,"end":140,"loc":{"start":{"line":1,"column":0},"end":{"line":9,"column":1}}, + "errors": [ + "SyntaxError: Private name #a is not defined. (undefined:undefined)", + "SyntaxError: Private name #b is not defined. (undefined:undefined)", + "SyntaxError: Private name #c is not defined. (undefined:undefined)", + "SyntaxError: Private name #d is not defined. (undefined:undefined)", + "SyntaxError: Private name #e is not defined. (undefined:undefined)" + ], + "program": { + "type": "Program", + "start":0,"end":140,"loc":{"start":{"line":1,"column":0},"end":{"line":9,"column":1}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":140,"loc":{"start":{"line":1,"column":0},"end":{"line":9,"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":140,"loc":{"start":{"line":1,"column":8},"end":{"line":9,"column":1}}, + "body": [ + { + "type": "ClassMethod", + "start":12,"end":138,"loc":{"start":{"line":2,"column":2},"end":{"line":8,"column":3}}, + "static": false, + "key": { + "type": "Identifier", + "start":12,"end":13,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":3},"identifierName":"m"}, + "name": "m" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":16,"end":138,"loc":{"start":{"line":2,"column":6},"end":{"line":8,"column":3}}, + "body": [ + { + "type": "ExpressionStatement", + "start":22,"end":38,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":20}}, + "expression": { + "type": "ArrowFunctionExpression", + "start":22,"end":37,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":19}}, + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "ObjectPattern", + "start":23,"end":30,"loc":{"start":{"line":3,"column":5},"end":{"line":3,"column":12}}, + "properties": [ + { + "type": "ObjectProperty", + "start":24,"end":29,"loc":{"start":{"line":3,"column":6},"end":{"line":3,"column":11}}, + "method": false, + "key": { + "type": "PrivateName", + "start":24,"end":26,"loc":{"start":{"line":3,"column":6},"end":{"line":3,"column":8}}, + "id": { + "type": "Identifier", + "start":25,"end":26,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":8},"identifierName":"a"}, + "name": "a" + } + }, + "shorthand": false, + "value": { + "type": "Identifier", + "start":28,"end":29,"loc":{"start":{"line":3,"column":10},"end":{"line":3,"column":11},"identifierName":"a"}, + "name": "a" + } + } + ] + } + ], + "body": { + "type": "BlockStatement", + "start":35,"end":37,"loc":{"start":{"line":3,"column":17},"end":{"line":3,"column":19}}, + "body": [], + "directives": [] + } + } + }, + { + "type": "ExpressionStatement", + "start":43,"end":65,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":26}}, + "expression": { + "type": "ArrowFunctionExpression", + "start":43,"end":64,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":25}}, + "id": null, + "generator": false, + "async": true, + "params": [ + { + "type": "ObjectPattern", + "start":50,"end":57,"loc":{"start":{"line":4,"column":11},"end":{"line":4,"column":18}}, + "properties": [ + { + "type": "ObjectProperty", + "start":51,"end":56,"loc":{"start":{"line":4,"column":12},"end":{"line":4,"column":17}}, + "method": false, + "key": { + "type": "PrivateName", + "start":51,"end":53,"loc":{"start":{"line":4,"column":12},"end":{"line":4,"column":14}}, + "id": { + "type": "Identifier", + "start":52,"end":53,"loc":{"start":{"line":4,"column":13},"end":{"line":4,"column":14},"identifierName":"b"}, + "name": "b" + } + }, + "shorthand": false, + "value": { + "type": "Identifier", + "start":55,"end":56,"loc":{"start":{"line":4,"column":16},"end":{"line":4,"column":17},"identifierName":"b"}, + "name": "b" + } + } + ] + } + ], + "body": { + "type": "BlockStatement", + "start":62,"end":64,"loc":{"start":{"line":4,"column":23},"end":{"line":4,"column":25}}, + "body": [], + "directives": [] + } + } + }, + { + "type": "VariableDeclaration", + "start":70,"end":87,"loc":{"start":{"line":5,"column":4},"end":{"line":5,"column":21}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":74,"end":86,"loc":{"start":{"line":5,"column":8},"end":{"line":5,"column":20}}, + "id": { + "type": "ObjectPattern", + "start":74,"end":81,"loc":{"start":{"line":5,"column":8},"end":{"line":5,"column":15}}, + "properties": [ + { + "type": "ObjectProperty", + "start":75,"end":80,"loc":{"start":{"line":5,"column":9},"end":{"line":5,"column":14}}, + "key": { + "type": "PrivateName", + "start":75,"end":77,"loc":{"start":{"line":5,"column":9},"end":{"line":5,"column":11}}, + "id": { + "type": "Identifier", + "start":76,"end":77,"loc":{"start":{"line":5,"column":10},"end":{"line":5,"column":11},"identifierName":"c"}, + "name": "c" + } + }, + "method": false, + "shorthand": false, + "value": { + "type": "Identifier", + "start":79,"end":80,"loc":{"start":{"line":5,"column":13},"end":{"line":5,"column":14},"identifierName":"c"}, + "name": "c" + } + } + ] + }, + "init": { + "type": "ObjectExpression", + "start":84,"end":86,"loc":{"start":{"line":5,"column":18},"end":{"line":5,"column":20}}, + "properties": [] + } + } + ], + "kind": "var" + }, + { + "type": "ForInStatement", + "start":92,"end":112,"loc":{"start":{"line":6,"column":4},"end":{"line":6,"column":24}}, + "left": { + "type": "ObjectPattern", + "start":97,"end":104,"loc":{"start":{"line":6,"column":9},"end":{"line":6,"column":16}}, + "properties": [ + { + "type": "ObjectProperty", + "start":98,"end":103,"loc":{"start":{"line":6,"column":10},"end":{"line":6,"column":15}}, + "method": false, + "key": { + "type": "PrivateName", + "start":98,"end":100,"loc":{"start":{"line":6,"column":10},"end":{"line":6,"column":12}}, + "id": { + "type": "Identifier", + "start":99,"end":100,"loc":{"start":{"line":6,"column":11},"end":{"line":6,"column":12},"identifierName":"d"}, + "name": "d" + } + }, + "shorthand": false, + "value": { + "type": "Identifier", + "start":102,"end":103,"loc":{"start":{"line":6,"column":14},"end":{"line":6,"column":15},"identifierName":"d"}, + "name": "d" + } + } + ] + }, + "right": { + "type": "ObjectExpression", + "start":108,"end":110,"loc":{"start":{"line":6,"column":20},"end":{"line":6,"column":22}}, + "properties": [] + }, + "body": { + "type": "EmptyStatement", + "start":111,"end":112,"loc":{"start":{"line":6,"column":23},"end":{"line":6,"column":24}} + } + }, + { + "type": "ExpressionStatement", + "start":117,"end":134,"loc":{"start":{"line":7,"column":4},"end":{"line":7,"column":21}}, + "expression": { + "type": "AssignmentExpression", + "start":118,"end":132,"loc":{"start":{"line":7,"column":5},"end":{"line":7,"column":19}}, + "operator": "=", + "left": { + "type": "ObjectPattern", + "start":118,"end":127,"loc":{"start":{"line":7,"column":5},"end":{"line":7,"column":14}}, + "properties": [ + { + "type": "ObjectProperty", + "start":120,"end":125,"loc":{"start":{"line":7,"column":7},"end":{"line":7,"column":12}}, + "method": false, + "key": { + "type": "PrivateName", + "start":120,"end":122,"loc":{"start":{"line":7,"column":7},"end":{"line":7,"column":9}}, + "id": { + "type": "Identifier", + "start":121,"end":122,"loc":{"start":{"line":7,"column":8},"end":{"line":7,"column":9},"identifierName":"e"}, + "name": "e" + } + }, + "shorthand": false, + "value": { + "type": "Identifier", + "start":124,"end":125,"loc":{"start":{"line":7,"column":11},"end":{"line":7,"column":12},"identifierName":"c"}, + "name": "c" + } + } + ] + }, + "right": { + "type": "ObjectExpression", + "start":130,"end":132,"loc":{"start":{"line":7,"column":17},"end":{"line":7,"column":19}}, + "properties": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 117 + } + } + } + ], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/options.json b/packages/babel-parser/test/fixtures/experimental/destructuring-private/options.json new file mode 100644 index 000000000000..2271198464b8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["destructuringPrivate"] +} diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-arrow-params/input.js b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-arrow-params/input.js new file mode 100644 index 000000000000..efc69f1bc6eb --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-arrow-params/input.js @@ -0,0 +1,4 @@ +class C { + #x = 1; + #p = ({ #x: x }) => {} +} diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-arrow-params/output.json b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-arrow-params/output.json new file mode 100644 index 000000000000..d515972382b5 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-arrow-params/output.json @@ -0,0 +1,107 @@ +{ + "type": "File", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}}, + "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/destructuring-private/valid-assignment/input.js b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-assignment/input.js new file mode 100644 index 000000000000..94276f376591 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-assignment/input.js @@ -0,0 +1,7 @@ +class C { + #x = 1; + m() { + let x; + ({ #x: x } = this); + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-assignment/output.json b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-assignment/output.json new file mode 100644 index 000000000000..e7b1a096e392 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-assignment/output.json @@ -0,0 +1,135 @@ +{ + "type": "File", + "start":0,"end":68,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}}, + "program": { + "type": "Program", + "start":0,"end":68,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":68,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"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":68,"loc":{"start":{"line":1,"column":8},"end":{"line":7,"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":66,"loc":{"start":{"line":3,"column":2},"end":{"line":6,"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":66,"loc":{"start":{"line":3,"column":6},"end":{"line":6,"column":3}}, + "body": [ + { + "type": "VariableDeclaration", + "start":32,"end":38,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":10}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":36,"end":37,"loc":{"start":{"line":4,"column":8},"end":{"line":4,"column":9}}, + "id": { + "type": "Identifier", + "start":36,"end":37,"loc":{"start":{"line":4,"column":8},"end":{"line":4,"column":9},"identifierName":"x"}, + "name": "x" + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "ExpressionStatement", + "start":43,"end":62,"loc":{"start":{"line":5,"column":4},"end":{"line":5,"column":23}}, + "expression": { + "type": "AssignmentExpression", + "start":44,"end":60,"loc":{"start":{"line":5,"column":5},"end":{"line":5,"column":21}}, + "operator": "=", + "left": { + "type": "ObjectPattern", + "start":44,"end":53,"loc":{"start":{"line":5,"column":5},"end":{"line":5,"column":14}}, + "properties": [ + { + "type": "ObjectProperty", + "start":46,"end":51,"loc":{"start":{"line":5,"column":7},"end":{"line":5,"column":12}}, + "method": false, + "key": { + "type": "PrivateName", + "start":46,"end":48,"loc":{"start":{"line":5,"column":7},"end":{"line":5,"column":9}}, + "id": { + "type": "Identifier", + "start":47,"end":48,"loc":{"start":{"line":5,"column":8},"end":{"line":5,"column":9},"identifierName":"x"}, + "name": "x" + } + }, + "shorthand": false, + "value": { + "type": "Identifier", + "start":50,"end":51,"loc":{"start":{"line":5,"column":11},"end":{"line":5,"column":12},"identifierName":"x"}, + "name": "x" + } + } + ] + }, + "right": { + "type": "ThisExpression", + "start":56,"end":60,"loc":{"start":{"line":5,"column":17},"end":{"line":5,"column":21}} + }, + "extra": { + "parenthesized": true, + "parenStart": 43 + } + } + } + ], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-async-arrow-params/input.js b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-async-arrow-params/input.js new file mode 100644 index 000000000000..935b7e1f032c --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-async-arrow-params/input.js @@ -0,0 +1,4 @@ +class C { + #x = 1; + #p = async ({ #x: x }) => {} +} diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-async-arrow-params/output.json b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-async-arrow-params/output.json new file mode 100644 index 000000000000..09f15f228979 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-async-arrow-params/output.json @@ -0,0 +1,107 @@ +{ + "type": "File", + "start":0,"end":52,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}}, + "program": { + "type": "Program", + "start":0,"end":52,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":52,"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":52,"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":50,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":30}}, + "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":50,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":30}}, + "id": null, + "generator": false, + "async": true, + "params": [ + { + "type": "ObjectPattern", + "start":34,"end":43,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":23}}, + "properties": [ + { + "type": "ObjectProperty", + "start":36,"end":41,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":21}}, + "method": false, + "key": { + "type": "PrivateName", + "start":36,"end":38,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":18}}, + "id": { + "type": "Identifier", + "start":37,"end":38,"loc":{"start":{"line":3,"column":17},"end":{"line":3,"column":18},"identifierName":"x"}, + "name": "x" + } + }, + "shorthand": false, + "value": { + "type": "Identifier", + "start":40,"end":41,"loc":{"start":{"line":3,"column":20},"end":{"line":3,"column":21},"identifierName":"x"}, + "name": "x" + } + } + ] + } + ], + "body": { + "type": "BlockStatement", + "start":48,"end":50,"loc":{"start":{"line":3,"column":28},"end":{"line":3,"column":30}}, + "body": [], + "directives": [] + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-bindings/input.js b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-bindings/input.js new file mode 100644 index 000000000000..980a785949d2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-bindings/input.js @@ -0,0 +1,6 @@ +class C { + #x = 1; + m() { + const {#x: x} = this; + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-bindings/output.json b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-bindings/output.json new file mode 100644 index 000000000000..fe2e8c5826fc --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-bindings/output.json @@ -0,0 +1,116 @@ +{ + "type": "File", + "start":0,"end":59,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}}, + "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}}, + "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" + } + }, + "method": false, + "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/destructuring-private/valid-for-lhs/input.js b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-for-lhs/input.js new file mode 100644 index 000000000000..b9bc462870ac --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-for-lhs/input.js @@ -0,0 +1,7 @@ +class C { + #x = 1; + m() { + let x; + for ({#x: x} of [this]); + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-for-lhs/output.json b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-for-lhs/output.json new file mode 100644 index 000000000000..40d3eba1c9bf --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-for-lhs/output.json @@ -0,0 +1,137 @@ +{ + "type": "File", + "start":0,"end":73,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}}, + "program": { + "type": "Program", + "start":0,"end":73,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":73,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"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":73,"loc":{"start":{"line":1,"column":8},"end":{"line":7,"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":71,"loc":{"start":{"line":3,"column":2},"end":{"line":6,"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":71,"loc":{"start":{"line":3,"column":6},"end":{"line":6,"column":3}}, + "body": [ + { + "type": "VariableDeclaration", + "start":32,"end":38,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":10}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":36,"end":37,"loc":{"start":{"line":4,"column":8},"end":{"line":4,"column":9}}, + "id": { + "type": "Identifier", + "start":36,"end":37,"loc":{"start":{"line":4,"column":8},"end":{"line":4,"column":9},"identifierName":"x"}, + "name": "x" + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "ForOfStatement", + "start":43,"end":67,"loc":{"start":{"line":5,"column":4},"end":{"line":5,"column":28}}, + "await": false, + "left": { + "type": "ObjectPattern", + "start":48,"end":55,"loc":{"start":{"line":5,"column":9},"end":{"line":5,"column":16}}, + "properties": [ + { + "type": "ObjectProperty", + "start":49,"end":54,"loc":{"start":{"line":5,"column":10},"end":{"line":5,"column":15}}, + "method": false, + "key": { + "type": "PrivateName", + "start":49,"end":51,"loc":{"start":{"line":5,"column":10},"end":{"line":5,"column":12}}, + "id": { + "type": "Identifier", + "start":50,"end":51,"loc":{"start":{"line":5,"column":11},"end":{"line":5,"column":12},"identifierName":"x"}, + "name": "x" + } + }, + "shorthand": false, + "value": { + "type": "Identifier", + "start":53,"end":54,"loc":{"start":{"line":5,"column":14},"end":{"line":5,"column":15},"identifierName":"x"}, + "name": "x" + } + } + ] + }, + "right": { + "type": "ArrayExpression", + "start":59,"end":65,"loc":{"start":{"line":5,"column":20},"end":{"line":5,"column":26}}, + "elements": [ + { + "type": "ThisExpression", + "start":60,"end":64,"loc":{"start":{"line":5,"column":21},"end":{"line":5,"column":25}} + } + ] + }, + "body": { + "type": "EmptyStatement", + "start":66,"end":67,"loc":{"start":{"line":5,"column":27},"end":{"line":5,"column":28}} + } + } + ], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-multiple-bindings/input.js b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-multiple-bindings/input.js new file mode 100644 index 000000000000..e8752078a586 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-multiple-bindings/input.js @@ -0,0 +1,6 @@ +class C { + #x = 1; + m() { + const {#x: x1, #x: x2 = x1 } = this; + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-multiple-bindings/output.json b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-multiple-bindings/output.json new file mode 100644 index 000000000000..6d5240b442df --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-multiple-bindings/output.json @@ -0,0 +1,145 @@ +{ + "type": "File", + "start":0,"end":74,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}}, + "program": { + "type": "Program", + "start":0,"end":74,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":74,"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":74,"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":72,"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":72,"loc":{"start":{"line":3,"column":6},"end":{"line":5,"column":3}}, + "body": [ + { + "type": "VariableDeclaration", + "start":32,"end":68,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":40}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":38,"end":67,"loc":{"start":{"line":4,"column":10},"end":{"line":4,"column":39}}, + "id": { + "type": "ObjectPattern", + "start":38,"end":60,"loc":{"start":{"line":4,"column":10},"end":{"line":4,"column":32}}, + "properties": [ + { + "type": "ObjectProperty", + "start":39,"end":45,"loc":{"start":{"line":4,"column":11},"end":{"line":4,"column":17}}, + "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" + } + }, + "method": false, + "shorthand": false, + "value": { + "type": "Identifier", + "start":43,"end":45,"loc":{"start":{"line":4,"column":15},"end":{"line":4,"column":17},"identifierName":"x1"}, + "name": "x1" + } + }, + { + "type": "ObjectProperty", + "start":47,"end":58,"loc":{"start":{"line":4,"column":19},"end":{"line":4,"column":30}}, + "key": { + "type": "PrivateName", + "start":47,"end":49,"loc":{"start":{"line":4,"column":19},"end":{"line":4,"column":21}}, + "id": { + "type": "Identifier", + "start":48,"end":49,"loc":{"start":{"line":4,"column":20},"end":{"line":4,"column":21},"identifierName":"x"}, + "name": "x" + } + }, + "method": false, + "shorthand": false, + "value": { + "type": "AssignmentPattern", + "start":51,"end":58,"loc":{"start":{"line":4,"column":23},"end":{"line":4,"column":30}}, + "left": { + "type": "Identifier", + "start":51,"end":53,"loc":{"start":{"line":4,"column":23},"end":{"line":4,"column":25},"identifierName":"x2"}, + "name": "x2" + }, + "right": { + "type": "Identifier", + "start":56,"end":58,"loc":{"start":{"line":4,"column":28},"end":{"line":4,"column":30},"identifierName":"x1"}, + "name": "x1" + } + } + } + ] + }, + "init": { + "type": "ThisExpression", + "start":63,"end":67,"loc":{"start":{"line":4,"column":35},"end":{"line":4,"column":39}} + } + } + ], + "kind": "const" + } + ], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-nested-bindings/input.js b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-nested-bindings/input.js new file mode 100644 index 000000000000..d9160ba8b33b --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-nested-bindings/input.js @@ -0,0 +1,6 @@ +class C { + #x = 1; + m() { + const {x: { #x: [x] }, y: [...{ #x: y }]} = this; + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-nested-bindings/output.json b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-nested-bindings/output.json new file mode 100644 index 000000000000..94f13da02605 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/destructuring-private/valid-nested-bindings/output.json @@ -0,0 +1,188 @@ +{ + "type": "File", + "start":0,"end":87,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}}, + "program": { + "type": "Program", + "start":0,"end":87,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":87,"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":87,"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":85,"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":85,"loc":{"start":{"line":3,"column":6},"end":{"line":5,"column":3}}, + "body": [ + { + "type": "VariableDeclaration", + "start":32,"end":81,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":53}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":38,"end":80,"loc":{"start":{"line":4,"column":10},"end":{"line":4,"column":52}}, + "id": { + "type": "ObjectPattern", + "start":38,"end":73,"loc":{"start":{"line":4,"column":10},"end":{"line":4,"column":45}}, + "properties": [ + { + "type": "ObjectProperty", + "start":39,"end":53,"loc":{"start":{"line":4,"column":11},"end":{"line":4,"column":25}}, + "key": { + "type": "Identifier", + "start":39,"end":40,"loc":{"start":{"line":4,"column":11},"end":{"line":4,"column":12},"identifierName":"x"}, + "name": "x" + }, + "computed": false, + "method": false, + "shorthand": false, + "value": { + "type": "ObjectPattern", + "start":42,"end":53,"loc":{"start":{"line":4,"column":14},"end":{"line":4,"column":25}}, + "properties": [ + { + "type": "ObjectProperty", + "start":44,"end":51,"loc":{"start":{"line":4,"column":16},"end":{"line":4,"column":23}}, + "key": { + "type": "PrivateName", + "start":44,"end":46,"loc":{"start":{"line":4,"column":16},"end":{"line":4,"column":18}}, + "id": { + "type": "Identifier", + "start":45,"end":46,"loc":{"start":{"line":4,"column":17},"end":{"line":4,"column":18},"identifierName":"x"}, + "name": "x" + } + }, + "method": false, + "shorthand": false, + "value": { + "type": "ArrayPattern", + "start":48,"end":51,"loc":{"start":{"line":4,"column":20},"end":{"line":4,"column":23}}, + "elements": [ + { + "type": "Identifier", + "start":49,"end":50,"loc":{"start":{"line":4,"column":21},"end":{"line":4,"column":22},"identifierName":"x"}, + "name": "x" + } + ] + } + } + ] + } + }, + { + "type": "ObjectProperty", + "start":55,"end":72,"loc":{"start":{"line":4,"column":27},"end":{"line":4,"column":44}}, + "key": { + "type": "Identifier", + "start":55,"end":56,"loc":{"start":{"line":4,"column":27},"end":{"line":4,"column":28},"identifierName":"y"}, + "name": "y" + }, + "computed": false, + "method": false, + "shorthand": false, + "value": { + "type": "ArrayPattern", + "start":58,"end":72,"loc":{"start":{"line":4,"column":30},"end":{"line":4,"column":44}}, + "elements": [ + { + "type": "RestElement", + "start":59,"end":71,"loc":{"start":{"line":4,"column":31},"end":{"line":4,"column":43}}, + "argument": { + "type": "ObjectPattern", + "start":62,"end":71,"loc":{"start":{"line":4,"column":34},"end":{"line":4,"column":43}}, + "properties": [ + { + "type": "ObjectProperty", + "start":64,"end":69,"loc":{"start":{"line":4,"column":36},"end":{"line":4,"column":41}}, + "key": { + "type": "PrivateName", + "start":64,"end":66,"loc":{"start":{"line":4,"column":36},"end":{"line":4,"column":38}}, + "id": { + "type": "Identifier", + "start":65,"end":66,"loc":{"start":{"line":4,"column":37},"end":{"line":4,"column":38},"identifierName":"x"}, + "name": "x" + } + }, + "method": false, + "shorthand": false, + "value": { + "type": "Identifier", + "start":68,"end":69,"loc":{"start":{"line":4,"column":40},"end":{"line":4,"column":41},"identifierName":"y"}, + "name": "y" + } + } + ] + } + } + ] + } + } + ] + }, + "init": { + "type": "ThisExpression", + "start":76,"end":80,"loc":{"start":{"line":4,"column":48},"end":{"line":4,"column":52}} + } + } + ], + "kind": "const" + } + ], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-private-key/input.js b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-private-key/input.js new file mode 100644 index 000000000000..7d2ecb91874b --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-private-key/input.js @@ -0,0 +1 @@ +#{ #x: x } diff --git a/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-private-key/options.json b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-private-key/options.json new file mode 100644 index 000000000000..3bf47e579109 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-private-key/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [ + ["recordAndTuple", { "syntaxType": "hash" }], + "destructuringPrivate" + ] +} diff --git a/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-private-key/output.json b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-private-key/output.json new file mode 100644 index 000000000000..7791c0b4671e --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-private-key/output.json @@ -0,0 +1,46 @@ +{ + "type": "File", + "start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}}, + "errors": [ + "SyntaxError: Unexpected private name. (1:3)" + ], + "program": { + "type": "Program", + "start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}}, + "expression": { + "type": "RecordExpression", + "start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}}, + "properties": [ + { + "type": "ObjectProperty", + "start":3,"end":8,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":8}}, + "method": false, + "key": { + "type": "PrivateName", + "start":3,"end":5,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":5}}, + "id": { + "type": "Identifier", + "start":4,"end":5,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":5},"identifierName":"x"}, + "name": "x" + } + }, + "shorthand": false, + "value": { + "type": "Identifier", + "start":7,"end":8,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":8},"identifierName":"x"}, + "name": "x" + } + } + ] + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/typings/babel-parser.d.ts b/packages/babel-parser/typings/babel-parser.d.ts index 3a3b3af22cdf..3e71da8bd0a1 100644 --- a/packages/babel-parser/typings/babel-parser.d.ts +++ b/packages/babel-parser/typings/babel-parser.d.ts @@ -129,6 +129,7 @@ export type ParserPlugin = | "decimal" | "decorators" | "decorators-legacy" + | "destructuringPrivate" | "doExpressions" | "dynamicImport" | "estree" diff --git a/packages/babel-plugin-syntax-destructuring-private/.npmignore b/packages/babel-plugin-syntax-destructuring-private/.npmignore new file mode 100644 index 000000000000..f9806945836e --- /dev/null +++ b/packages/babel-plugin-syntax-destructuring-private/.npmignore @@ -0,0 +1,3 @@ +src +test +*.log diff --git a/packages/babel-plugin-syntax-destructuring-private/README.md b/packages/babel-plugin-syntax-destructuring-private/README.md new file mode 100644 index 000000000000..c5c5afa43b77 --- /dev/null +++ b/packages/babel-plugin-syntax-destructuring-private/README.md @@ -0,0 +1,19 @@ +# @babel/plugin-syntax-destructuring-private + +> Allow parsing of destructuring private fields + +See our website [@babel/plugin-syntax-destructuring-private](https://babeljs.io/docs/en/babel-plugin-syntax-destructuring-private) for more information. + +## Install + +Using npm: + +```sh +npm install --save-dev @babel/plugin-syntax-destructuring-private +``` + +or using yarn: + +```sh +yarn add @babel/plugin-syntax-destructuring-private --dev +``` diff --git a/packages/babel-plugin-syntax-destructuring-private/package.json b/packages/babel-plugin-syntax-destructuring-private/package.json new file mode 100644 index 000000000000..a54355612d34 --- /dev/null +++ b/packages/babel-plugin-syntax-destructuring-private/package.json @@ -0,0 +1,32 @@ +{ + "name": "@babel/plugin-syntax-destructuring-private", + "version": "0.0.0", + "description": "Allow parsing of destructuring private fields", + "repository": { + "type": "git", + "url": "https://github.com/babel/babel.git", + "directory": "packages/babel-plugin-syntax-destructuring-private" + }, + "homepage": "https://babel.dev/docs/en/next/babel-plugin-syntax-destructuring-private", + "license": "MIT", + "publishConfig": { + "access": "public" + }, + "main": "./lib/index.js", + "exports": { + ".": "./lib/index.js" + }, + "keywords": [ + "babel-plugin" + ], + "dependencies": { + "@babel/helper-plugin-utils": "workspace:^" + }, + "peerDependencies": { + "@babel/core": "^7.0.0-0" + }, + "engines": { + "node": ">=6.9.0" + }, + "author": "The Babel Team (https://babel.dev/team)" +} diff --git a/packages/babel-plugin-syntax-destructuring-private/src/index.ts b/packages/babel-plugin-syntax-destructuring-private/src/index.ts new file mode 100644 index 000000000000..71169f24f853 --- /dev/null +++ b/packages/babel-plugin-syntax-destructuring-private/src/index.ts @@ -0,0 +1,13 @@ +import { declare } from "@babel/helper-plugin-utils"; + +export default declare(api => { + api.assertVersion(7); + + return { + name: "syntax-destructuring-private", + + manipulateOptions(_: any, parserOpts: { plugins: string[] }) { + parserOpts.plugins.push("destructuringPrivate"); + }, + }; +}); diff --git a/packages/babel-standalone/package.json b/packages/babel-standalone/package.json index 5fc080571ebc..671f7cce4d4d 100644 --- a/packages/babel-standalone/package.json +++ b/packages/babel-standalone/package.json @@ -37,6 +37,7 @@ "@babel/plugin-syntax-class-static-block": "^7.14.5", "@babel/plugin-syntax-decimal": "workspace:^", "@babel/plugin-syntax-decorators": "workspace:^", + "@babel/plugin-syntax-destructuring-private": "workspace:^", "@babel/plugin-syntax-do-expressions": "workspace:^", "@babel/plugin-syntax-export-default-from": "workspace:^", "@babel/plugin-syntax-flow": "workspace:^", diff --git a/packages/babel-standalone/scripts/pluginConfig.json b/packages/babel-standalone/scripts/pluginConfig.json index 7fd28384f68b..4225511093b8 100644 --- a/packages/babel-standalone/scripts/pluginConfig.json +++ b/packages/babel-standalone/scripts/pluginConfig.json @@ -5,6 +5,7 @@ "syntax-class-static-block", "syntax-decimal", "syntax-decorators", + "syntax-destructuring-private", "syntax-do-expressions", "syntax-export-default-from", "syntax-flow", diff --git a/packages/babel-standalone/src/generated/plugins.ts b/packages/babel-standalone/src/generated/plugins.ts index d97f81053bbe..724211513a81 100644 --- a/packages/babel-standalone/src/generated/plugins.ts +++ b/packages/babel-standalone/src/generated/plugins.ts @@ -8,6 +8,7 @@ import syntaxClassProperties from "@babel/plugin-syntax-class-properties"; import syntaxClassStaticBlock from "@babel/plugin-syntax-class-static-block"; import syntaxDecimal from "@babel/plugin-syntax-decimal"; import syntaxDecorators from "@babel/plugin-syntax-decorators"; +import syntaxDestructuringPrivate from "@babel/plugin-syntax-destructuring-private"; import syntaxDoExpressions from "@babel/plugin-syntax-do-expressions"; import syntaxExportDefaultFrom from "@babel/plugin-syntax-export-default-from"; import syntaxFlow from "@babel/plugin-syntax-flow"; @@ -103,6 +104,7 @@ export { syntaxClassStaticBlock, syntaxDecimal, syntaxDecorators, + syntaxDestructuringPrivate, syntaxDoExpressions, syntaxExportDefaultFrom, syntaxFlow, @@ -199,6 +201,7 @@ export const all: { [k: string]: any } = { "syntax-class-static-block": syntaxClassStaticBlock, "syntax-decimal": syntaxDecimal, "syntax-decorators": syntaxDecorators, + "syntax-destructuring-private": syntaxDestructuringPrivate, "syntax-do-expressions": syntaxDoExpressions, "syntax-export-default-from": syntaxExportDefaultFrom, "syntax-flow": syntaxFlow, diff --git a/packages/babel-standalone/src/preset-stage-2.ts b/packages/babel-standalone/src/preset-stage-2.ts index 4d743659dae5..e8bf7b9920a3 100644 --- a/packages/babel-standalone/src/preset-stage-2.ts +++ b/packages/babel-standalone/src/preset-stage-2.ts @@ -19,6 +19,7 @@ export default (_: any, opts: any = {}) => { babelPlugins.proposalDecorators, { legacy: decoratorsLegacy, decoratorsBeforeExport }, ], + babelPlugins.syntaxDestructuringPrivate, [ babelPlugins.proposalPipelineOperator, { proposal: pipelineProposal, topicToken: pipelineTopicToken }, diff --git a/yarn.lock b/yarn.lock index 5bd5c1746677..a46832d0ed63 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1818,6 +1818,16 @@ __metadata: languageName: unknown linkType: soft +"@babel/plugin-syntax-destructuring-private@workspace:^, @babel/plugin-syntax-destructuring-private@workspace:packages/babel-plugin-syntax-destructuring-private": + version: 0.0.0-use.local + resolution: "@babel/plugin-syntax-destructuring-private@workspace:packages/babel-plugin-syntax-destructuring-private" + dependencies: + "@babel/helper-plugin-utils": "workspace:^" + peerDependencies: + "@babel/core": ^7.0.0-0 + languageName: unknown + linkType: soft + "@babel/plugin-syntax-do-expressions@workspace:^, @babel/plugin-syntax-do-expressions@workspace:packages/babel-plugin-syntax-do-expressions": version: 0.0.0-use.local resolution: "@babel/plugin-syntax-do-expressions@workspace:packages/babel-plugin-syntax-do-expressions" @@ -3586,6 +3596,7 @@ __metadata: "@babel/plugin-syntax-class-static-block": ^7.14.5 "@babel/plugin-syntax-decimal": "workspace:^" "@babel/plugin-syntax-decorators": "workspace:^" + "@babel/plugin-syntax-destructuring-private": "workspace:^" "@babel/plugin-syntax-do-expressions": "workspace:^" "@babel/plugin-syntax-export-default-from": "workspace:^" "@babel/plugin-syntax-flow": "workspace:^"