From 284bcc10dde253ea9c6cc3b75f12d02ab523f7e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 1 Mar 2022 15:42:22 -0500 Subject: [PATCH 01/35] misc fix --- .../src/features.ts | 2 +- .../package.json | 2 +- .../package.json | 18 +++++++++++++----- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/babel-helper-create-class-features-plugin/src/features.ts b/packages/babel-helper-create-class-features-plugin/src/features.ts index a895190c6645..a749d8b1b1c0 100644 --- a/packages/babel-helper-create-class-features-plugin/src/features.ts +++ b/packages/babel-helper-create-class-features-plugin/src/features.ts @@ -181,7 +181,7 @@ export function shouldTransform(path: NodePath, file: File): boolean { if (privateMethodPath && !hasFeature(file, FEATURES.privateMethods)) { throw privateMethodPath.buildCodeFrameError( "Class private methods are not enabled. " + - "Please add `@babel/plugin-proposal-private-method` to your configuration.", + "Please add `@babel/plugin-proposal-private-methods` to your configuration.", ); } diff --git a/packages/babel-plugin-proposal-class-static-block/package.json b/packages/babel-plugin-proposal-class-static-block/package.json index 3eabbc73bf72..e1d008c8fd01 100644 --- a/packages/babel-plugin-proposal-class-static-block/package.json +++ b/packages/babel-plugin-proposal-class-static-block/package.json @@ -1,7 +1,7 @@ { "name": "@babel/plugin-proposal-class-static-block", "version": "7.17.12", - "description": "Allow parsing of class static blocks", + "description": "Transform class static blocks", "repository": { "type": "git", "url": "https://github.com/babel/babel.git", diff --git a/packages/babel-plugin-syntax-destructuring-private/package.json b/packages/babel-plugin-syntax-destructuring-private/package.json index c0fdc720461e..cbe465a74d45 100644 --- a/packages/babel-plugin-syntax-destructuring-private/package.json +++ b/packages/babel-plugin-syntax-destructuring-private/package.json @@ -13,10 +13,6 @@ "access": "public" }, "main": "./lib/index.js", - "exports": { - ".": "./lib/index.js", - "./package.json": "./package.json" - }, "keywords": [ "babel-plugin" ], @@ -29,5 +25,17 @@ "engines": { "node": ">=6.9.0" }, - "author": "The Babel Team (https://babel.dev/team)" + "author": "The Babel Team (https://babel.dev/team)", + "conditions": { + "BABEL_8_BREAKING": [ + null, + { + "exports": null + } + ] + }, + "exports": { + ".": "./lib/index.js", + "./package.json": "./package.json" + } } From 32ec0611e866372d58dc2bdf3d6eb3e8d75eb188 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 1 Mar 2022 15:45:45 -0500 Subject: [PATCH 02/35] types: allow PrivateName in ObjectProperty --- packages/babel-helper-define-map/src/index.ts | 6 +++++- .../src/index.ts | 4 +++- .../babel-plugin-transform-proto-to-assign/src/index.ts | 1 - packages/babel-types/src/ast-types/generated/index.ts | 9 ++++++++- packages/babel-types/src/builders/generated/index.ts | 9 ++++++++- packages/babel-types/src/converters/toComputedKey.ts | 2 +- packages/babel-types/src/definitions/core.ts | 6 ++++++ 7 files changed, 31 insertions(+), 6 deletions(-) diff --git a/packages/babel-helper-define-map/src/index.ts b/packages/babel-helper-define-map/src/index.ts index 591cd78193f6..0653c7991744 100644 --- a/packages/babel-helper-define-map/src/index.ts +++ b/packages/babel-helper-define-map/src/index.ts @@ -121,7 +121,11 @@ export function toComputedObjectFromClass(obj: any) { const prop = obj.properties[i]; const val = prop.value; val.properties.unshift( - objectProperty(identifier("key"), toComputedKey(prop)), + objectProperty( + identifier("key"), + // @ts-expect-error toComputedObjectFromClass is not used, maybe we can remove it + toComputedKey(prop), + ), ); objExpr.elements.push(val); } diff --git a/packages/babel-plugin-proposal-object-rest-spread/src/index.ts b/packages/babel-plugin-proposal-object-rest-spread/src/index.ts index caf1c6baa1e6..d0acd5b93711 100644 --- a/packages/babel-plugin-proposal-object-rest-spread/src/index.ts +++ b/packages/babel-plugin-proposal-object-rest-spread/src/index.ts @@ -127,6 +127,7 @@ export default declare((api, opts: Options) => { ), ); } else { + // @ts-expect-error private name has been handled by destructuring-private keys.push(t.cloneNode(prop.key)); allLiteral = false; } @@ -143,7 +144,8 @@ export default declare((api, opts: Options) => { ) { const impureComputedPropertyDeclarators: t.VariableDeclarator[] = []; for (const propPath of properties) { - const key = propPath.get("key"); + // PrivateName is handled in destructuring-private plugin + const key = propPath.get("key") as NodePath; if (propPath.node.computed && !key.isPure()) { const name = scope.generateUidBasedOnNode(key.node); const declarator = t.variableDeclarator(t.identifier(name), key.node); diff --git a/packages/babel-plugin-transform-proto-to-assign/src/index.ts b/packages/babel-plugin-transform-proto-to-assign/src/index.ts index 7bfce080e361..bd29c5933ff2 100644 --- a/packages/babel-plugin-transform-proto-to-assign/src/index.ts +++ b/packages/babel-plugin-transform-proto-to-assign/src/index.ts @@ -12,7 +12,6 @@ export default declare(api => { const left = node; return ( t.isMemberExpression(left) && - // @ts-expect-error todo(flow->ts): property can be t.PrivateName t.isLiteral(t.toComputedKey(left, left.property), { value: "__proto__" }) ); } diff --git a/packages/babel-types/src/ast-types/generated/index.ts b/packages/babel-types/src/ast-types/generated/index.ts index 511e42f24d0e..e6da586d995e 100644 --- a/packages/babel-types/src/ast-types/generated/index.ts +++ b/packages/babel-types/src/ast-types/generated/index.ts @@ -593,7 +593,14 @@ export interface ObjectMethod extends BaseNode { export interface ObjectProperty extends BaseNode { type: "ObjectProperty"; - key: Expression | Identifier | StringLiteral | NumericLiteral; + key: + | Expression + | Identifier + | StringLiteral + | NumericLiteral + | BigIntLiteral + | DecimalLiteral + | PrivateName; value: Expression | PatternLike; computed: boolean; shorthand: boolean; diff --git a/packages/babel-types/src/builders/generated/index.ts b/packages/babel-types/src/builders/generated/index.ts index 062e659e3898..30870df8a9af 100644 --- a/packages/babel-types/src/builders/generated/index.ts +++ b/packages/babel-types/src/builders/generated/index.ts @@ -383,7 +383,14 @@ export function objectMethod( }); } export function objectProperty( - key: t.Expression | t.Identifier | t.StringLiteral | t.NumericLiteral, + key: + | t.Expression + | t.Identifier + | t.StringLiteral + | t.NumericLiteral + | t.BigIntLiteral + | t.DecimalLiteral + | t.PrivateName, value: t.Expression | t.PatternLike, computed: boolean = false, shorthand: boolean = false, diff --git a/packages/babel-types/src/converters/toComputedKey.ts b/packages/babel-types/src/converters/toComputedKey.ts index 4db773ddb56d..9302c9302bdc 100644 --- a/packages/babel-types/src/converters/toComputedKey.ts +++ b/packages/babel-types/src/converters/toComputedKey.ts @@ -12,7 +12,7 @@ export default function toComputedKey( | t.MemberExpression | t.OptionalMemberExpression, // @ts-expect-error todo(flow->ts): maybe check the type of node before accessing .key and .property - key: t.Expression = node.key || node.property, + key: t.Expression | t.PrivateName = node.key || node.property, ) { if (!node.computed && isIdentifier(key)) key = stringLiteral(key.name); diff --git a/packages/babel-types/src/definitions/core.ts b/packages/babel-types/src/definitions/core.ts index a660422bd172..92591951cafd 100644 --- a/packages/babel-types/src/definitions/core.ts +++ b/packages/babel-types/src/definitions/core.ts @@ -833,6 +833,9 @@ defineType("ObjectProperty", { "Identifier", "StringLiteral", "NumericLiteral", + "BigIntLiteral", + "DecimalLiteral", + "PrivateName", ); const computed = assertNodeType("Expression"); @@ -846,6 +849,9 @@ defineType("ObjectProperty", { "Identifier", "StringLiteral", "NumericLiteral", + "BigIntLiteral", + "DecimalLiteral", + "PrivateName", ]; return validator; })(), From e4e90e5e79546b08ca54926dcab5166ed816a106 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 1 Mar 2022 15:47:36 -0500 Subject: [PATCH 03/35] optimize: ignore completion record in static block --- packages/babel-traverse/src/path/introspection.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-traverse/src/path/introspection.ts b/packages/babel-traverse/src/path/introspection.ts index e5f7e7b49368..7680d6b08b91 100644 --- a/packages/babel-traverse/src/path/introspection.ts +++ b/packages/babel-traverse/src/path/introspection.ts @@ -135,10 +135,10 @@ export function isCompletionRecord( let first = true; do { - const container = path.container; + const { type, container } = path; // we're in a function so can't be a completion record - if (path.isFunction() && !first) { + if (!first && (path.isFunction() || type === "StaticBlock")) { return !!allowInsideFunction; } From 36b6ae162f5ac1ae664e776cf8626330718c583b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 1 Mar 2022 15:49:28 -0500 Subject: [PATCH 04/35] fix: push new binding from initializer out of function --- packages/babel-traverse/src/scope/index.ts | 21 +++++++++++++++++- packages/babel-traverse/test/scope.js | 25 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/packages/babel-traverse/src/scope/index.ts b/packages/babel-traverse/src/scope/index.ts index b96794f31710..444407f4f02c 100644 --- a/packages/babel-traverse/src/scope/index.ts +++ b/packages/babel-traverse/src/scope/index.ts @@ -1019,7 +1019,9 @@ export default class Scope { }) { let path = this.path; - if (!path.isBlockStatement() && !path.isProgram()) { + if (path.isPattern()) { + path = this.getPatternParent().path; + } else if (!path.isBlockStatement() && !path.isProgram()) { path = this.getBlockParent().path; } @@ -1099,6 +1101,23 @@ export default class Scope { ); } + /** + * Walk up from a pattern scope (function param initializer) until we hit a non-pattern scope, + * then returns its block parent + * @returns An ancestry scope whose path is a block parent + */ + getPatternParent() { + let scope: Scope = this; + do { + if (!scope.path.isPattern()) { + return scope.getBlockParent(); + } + } while ((scope = scope.parent.parent)); + throw new Error( + "We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...", + ); + } + /** * Walks the scope tree and gathers **all** bindings. */ diff --git a/packages/babel-traverse/test/scope.js b/packages/babel-traverse/test/scope.js index 375089129e16..84961905a53c 100644 --- a/packages/babel-traverse/test/scope.js +++ b/packages/babel-traverse/test/scope.js @@ -902,5 +902,30 @@ describe("scope", () => { `); expect(program.scope.hasOwnBinding("class")).toBe(true); }); + it("registers the new binding outside function when the path is a param initializer", () => { + const program = getPath("(a = f()) => {}"); + const assignmentPattern = program.get("body.0.expression.params.0"); + assignmentPattern.scope.push({ id: t.identifier("ref") }); + expect(program.toString()).toMatchInlineSnapshot(` + "var ref; + + (a = f()) => {};" + `); + expect(program.scope.hasOwnBinding("ref")).toBe(true); + }); + it("registers the new binding outside class method when the path is a param initializer", () => { + const program = getPath("class C { m(a = f()) {} }"); + const assignmentPattern = program.get("body.0.body.body.0.params.0"); + assignmentPattern.scope.push({ id: t.identifier("ref") }); + expect(program.toString()).toMatchInlineSnapshot(` + "var ref; + + class C { + m(a = f()) {} + + }" + `); + expect(program.scope.hasOwnBinding("ref")).toBe(true); + }); }); }); From 98dbb0560957482c700cd10ebe9b197a5926c3e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 1 Mar 2022 15:50:38 -0500 Subject: [PATCH 05/35] optimize: don't memoise RHS when in SequenceExpression --- .../src/util.ts | 15 +++++++++++---- .../input.js | 2 ++ .../output.js | 2 ++ 3 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/assignment-sequence-expression-completion-record/input.js create mode 100644 packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/assignment-sequence-expression-completion-record/output.js diff --git a/packages/babel-plugin-transform-destructuring/src/util.ts b/packages/babel-plugin-transform-destructuring/src/util.ts index a973b5600904..d40c3ae77214 100644 --- a/packages/babel-plugin-transform-destructuring/src/util.ts +++ b/packages/babel-plugin-transform-destructuring/src/util.ts @@ -290,7 +290,10 @@ export class DestructuringTransformer { const key = prop.key; if (prop.computed && !this.scope.isPure(key)) { const name = this.scope.generateUidIdentifierBasedOnNode(key); - this.nodes.push(this.buildVariableDeclaration(name, key)); + this.nodes.push( + //@ts-expect-error PrivateName has been handled by destructuring-private + this.buildVariableDeclaration(name, key), + ); if (!copiedPattern) { copiedPattern = pattern = { ...pattern, @@ -630,7 +633,7 @@ export function convertAssignmentExpression( objectRestNoSymbols: boolean, useBuiltIns: boolean, ) { - const { node, scope } = path; + const { node, scope, parentPath } = path; const nodes = []; @@ -646,7 +649,11 @@ export function convertAssignmentExpression( }); let ref: t.Identifier | void; - if (path.isCompletionRecord() || !path.parentPath.isExpressionStatement()) { + if ( + (!parentPath.isExpressionStatement() && + !parentPath.isSequenceExpression()) || + path.isCompletionRecord() + ) { ref = scope.generateUidIdentifierBasedOnNode(node.right, "ref"); nodes.push( @@ -661,7 +668,7 @@ export function convertAssignmentExpression( destructuring.init(node.left, ref || node.right); if (ref) { - if (path.parentPath.isArrowFunctionExpression()) { + if (parentPath.isArrowFunctionExpression()) { path.replaceWith(t.blockStatement([])); nodes.push(t.returnStatement(t.cloneNode(ref))); } else { diff --git a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/assignment-sequence-expression-completion-record/input.js b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/assignment-sequence-expression-completion-record/input.js new file mode 100644 index 000000000000..8986163f9364 --- /dev/null +++ b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/assignment-sequence-expression-completion-record/input.js @@ -0,0 +1,2 @@ +var x, y; +([x, y] = [1, 2], y); diff --git a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/assignment-sequence-expression-completion-record/output.js b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/assignment-sequence-expression-completion-record/output.js new file mode 100644 index 000000000000..0048baa438b9 --- /dev/null +++ b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/assignment-sequence-expression-completion-record/output.js @@ -0,0 +1,2 @@ +var x, y; +(x = 1, y = 2), y; From cfc8c50fa80fa7b81078108310f2a9962a8da356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 1 Mar 2022 15:52:09 -0500 Subject: [PATCH 06/35] feat: expose buildObjectExcludingKeys --- packages/babel-plugin-transform-destructuring/src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/babel-plugin-transform-destructuring/src/index.ts b/packages/babel-plugin-transform-destructuring/src/index.ts index 0726446b0053..8f60c1c0ce37 100644 --- a/packages/babel-plugin-transform-destructuring/src/index.ts +++ b/packages/babel-plugin-transform-destructuring/src/index.ts @@ -5,6 +5,7 @@ import { convertVariableDeclaration, convertAssignmentExpression, } from "./util"; +export { buildObjectExcludingKeys } from "./util"; /** * Test if a VariableDeclaration's declarations contains any Patterns. From 65575c28933b35e736fd19540fa89675664a7503 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 1 Mar 2022 19:39:44 -0500 Subject: [PATCH 07/35] refactor: remove parameter property in constructor visitor --- .../src/index.ts | 58 ++++++++----------- .../parameter-properties-with-class/output.js | 12 ++-- 2 files changed, 27 insertions(+), 43 deletions(-) diff --git a/packages/babel-plugin-transform-typescript/src/index.ts b/packages/babel-plugin-transform-typescript/src/index.ts index ff202ca01231..b881f48e9151 100644 --- a/packages/babel-plugin-transform-typescript/src/index.ts +++ b/packages/babel-plugin-transform-typescript/src/index.ts @@ -166,36 +166,35 @@ export default declare((api, opts: Options) => { // property is only added once. This is necessary for cases like // using `transform-classes`, which causes this visitor to run // twice. - const parameterProperties = []; - for (const param of path.node.params) { - if ( - param.type === "TSParameterProperty" && - !PARSED_PARAMS.has(param.parameter) - ) { - PARSED_PARAMS.add(param.parameter); - parameterProperties.push(param.parameter); - } - } - - if (parameterProperties.length) { - const assigns = parameterProperties.map(p => { + const assigns = []; + const { scope } = path; + for (const paramPath of path.get("params")) { + const param = paramPath.node; + if (param.type === "TSParameterProperty") { + const parameter = param.parameter; + if (PARSED_PARAMS.has(parameter)) continue; + PARSED_PARAMS.add(parameter); let id; - if (t.isIdentifier(p)) { - id = p; - } else if (t.isAssignmentPattern(p) && t.isIdentifier(p.left)) { - id = p.left; + if (t.isIdentifier(parameter)) { + id = parameter; + } else if ( + t.isAssignmentPattern(parameter) && + t.isIdentifier(parameter.left) + ) { + id = parameter.left; } else { - throw path.buildCodeFrameError( + throw paramPath.buildCodeFrameError( "Parameter properties can not be destructuring patterns.", ); } + assigns.push(template.statement.ast` + this.${t.cloneNode(id)} = ${t.cloneNode(id)}`); - return template.statement.ast` - this.${t.cloneNode(id)} = ${t.cloneNode(id)}`; - }); - - injectInitialization(classPath, path, assigns); + paramPath.replaceWith(paramPath.get("parameter")); + scope.registerBinding("param", paramPath); + } } + injectInitialization(classPath, path, assigns); }, }; @@ -502,7 +501,7 @@ export default declare((api, opts: Options) => { }, Function(path) { - const { node, scope } = path; + const { node } = path; if (node.typeParameters) node.typeParameters = null; if (node.returnType) node.returnType = null; @@ -510,17 +509,6 @@ export default declare((api, opts: Options) => { if (params.length > 0 && t.isIdentifier(params[0], { name: "this" })) { params.shift(); } - - // We replace `TSParameterProperty` here so that transforms that - // rely on a `Function` visitor to deal with arguments, like - // `transform-parameters`, work properly. - const paramsPath = path.get("params"); - for (const p of paramsPath) { - if (p.type === "TSParameterProperty") { - p.replaceWith(p.get("parameter")); - scope.registerBinding("param", p); - } - } }, TSModuleDeclaration(path) { diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-class/output.js b/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-class/output.js index 47b225e90906..fc4fb9cfe297 100644 --- a/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-class/output.js +++ b/packages/babel-plugin-transform-typescript/test/fixtures/class/parameter-properties-with-class/output.js @@ -1,10 +1,6 @@ -let Person = /*#__PURE__*/function () { +let Person = /*#__PURE__*/babelHelpers.createClass(function Person(name) { "use strict"; - function Person(name) { - babelHelpers.classCallCheck(this, Person); - this.name = name; - } - - return babelHelpers.createClass(Person); -}(); + babelHelpers.classCallCheck(this, Person); + this.name = name; +}); From 9af24c71fa7f1a8333705b509326949627b7e21e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 1 Mar 2022 15:53:26 -0500 Subject: [PATCH 08/35] implement transform --- Gulpfile.mjs | 1 + .../.npmignore | 3 + .../README.md | 19 + .../package.json | 50 ++ .../src/index.ts | 205 +++++++++ .../src/util.ts | 428 ++++++++++++++++++ .../assignment--es2015/array-rest/exec.js | 10 + .../assignment--es2015/array-rest/input.js | 7 + .../assignment--es2015/array-rest/output.js | 14 + .../fixtures/assignment--es2015/basic/exec.js | 10 + .../assignment--es2015/basic/input.js | 7 + .../assignment--es2015/basic/output.js | 18 + .../assignment--es2015/completion/exec.js | 13 + .../member-expression/exec.js | 11 + .../member-expression/input.js | 10 + .../member-expression/output.js | 23 + .../nested-under-array-pattern/exec.js | 14 + .../nested-under-array-pattern/input.js | 11 + .../nested-under-array-pattern/output.js | 26 ++ .../assignment--es2015/nested/exec.js | 21 + .../assignment--es2015/nested/input.js | 12 + .../assignment--es2015/nested/options.json | 9 + .../assignment--es2015/nested/output.js | 30 ++ .../object-rest-and-private-keys/exec.js | 16 + .../object-rest-and-private-keys/input.js | 13 + .../object-rest-and-private-keys/output.js | 27 ++ .../object-rest-under-private/exec.js | 13 + .../object-rest-under-private/input.js | 12 + .../object-rest-under-private/output.js | 28 ++ .../assignment--es2015/object-rest/exec.js | 16 + .../assignment--es2015/object-rest/input.js | 13 + .../assignment--es2015/object-rest/output.js | 33 ++ .../fixtures/assignment--es2015/options.json | 9 + .../under-param-initializer/exec.js | 11 + .../under-param-initializer/input.js | 10 + .../under-param-initializer/output.js | 30 ++ .../fixtures/assignment/array-rest/exec.js | 10 + .../fixtures/assignment/array-rest/input.js | 7 + .../fixtures/assignment/array-rest/output.js | 10 + .../test/fixtures/assignment/basic/exec.js | 10 + .../test/fixtures/assignment/basic/input.js | 7 + .../test/fixtures/assignment/basic/output.js | 14 + .../fixtures/assignment/completion/exec.js | 13 + .../assignment/member-expression/exec.js | 11 + .../assignment/member-expression/input.js | 10 + .../assignment/member-expression/output.js | 13 + .../nested-under-array-pattern/exec.js | 14 + .../nested-under-array-pattern/input.js | 11 + .../nested-under-array-pattern/output.js | 17 + .../test/fixtures/assignment/nested/exec.js | 21 + .../test/fixtures/assignment/nested/input.js | 12 + .../test/fixtures/assignment/nested/output.js | 17 + .../object-rest-and-private-keys/exec.js | 16 + .../object-rest-and-private-keys/input.js | 13 + .../object-rest-and-private-keys/output.js | 19 + .../object-rest-under-private/exec.js | 13 + .../object-rest-under-private/input.js | 12 + .../object-rest-under-private/output.js | 22 + .../fixtures/assignment/object-rest/exec.js | 16 + .../fixtures/assignment/object-rest/input.js | 13 + .../fixtures/assignment/object-rest/output.js | 26 ++ .../test/fixtures/assignment/options.json | 4 + .../under-param-initializer/exec.js | 11 + .../under-param-initializer/input.js | 10 + .../under-param-initializer/output.js | 27 ++ .../options.json | 12 + .../variable-declaration/exec.js | 15 + .../variable-declaration/input.js | 12 + .../variable-declaration/output.js | 36 ++ .../no-shadowed-params/input.js | 7 + .../no-shadowed-params/options.json | 7 + .../no-shadowed-params/output.js | 19 + .../catch-param/no-shadowed-params/input.js | 7 + .../no-shadowed-params/options.json | 4 + .../catch-param/no-shadowed-params/output.js | 11 + .../for-init--es2015/lhs-with-assign/exec.js | 12 + .../for-init--es2015/lhs-with-assign/input.js | 7 + .../lhs-with-assign/output.js | 16 + .../fixtures/for-init--es2015/lhs/input.js | 6 + .../fixtures/for-init--es2015/lhs/output.js | 19 + .../fixtures/for-init--es2015/options.json | 7 + .../variable-declaration-with-assign/exec.js | 12 + .../variable-declaration-with-assign/input.js | 9 + .../output.js | 18 + .../variable-declaration/input.js | 6 + .../variable-declaration/output.js | 12 + .../fixtures/for-init/lhs-with-assign/exec.js | 12 + .../for-init/lhs-with-assign/input.js | 7 + .../for-init/lhs-with-assign/output.js | 13 + .../test/fixtures/for-init/lhs/input.js | 6 + .../test/fixtures/for-init/lhs/output.js | 11 + .../test/fixtures/for-init/options.json | 4 + .../variable-declaration-with-assign/exec.js | 12 + .../variable-declaration-with-assign/input.js | 9 + .../output.js | 14 + .../for-init/variable-declaration/input.js | 6 + .../for-init/variable-declaration/output.js | 9 + .../test/fixtures/for-of--es2015/lhs/input.js | 6 + .../fixtures/for-of--es2015/lhs/output.js | 18 + .../test/fixtures/for-of--es2015/options.json | 7 + .../variable-declaration/input.js | 6 + .../variable-declaration/output.js | 18 + .../test/fixtures/for-of/lhs/input.js | 6 + .../test/fixtures/for-of/lhs/output.js | 10 + .../test/fixtures/for-of/options.json | 4 + .../for-of/variable-declaration/input.js | 6 + .../for-of/variable-declaration/output.js | 10 + .../function-length/exec.js | 5 + .../no-shadowed-params/input.js | 6 + .../no-shadowed-params/output.js | 17 + .../function-params--es2015/options.json | 7 + .../shadowed-params/input.js | 6 + .../shadowed-params/output.js | 20 + .../function-params/function-length/exec.js | 5 + .../no-shadowed-params/input.js | 6 + .../no-shadowed-params/output.js | 10 + .../fixtures/function-params/options.json | 4 + .../function-params/shadowed-params/input.js | 6 + .../function-params/shadowed-params/output.js | 13 + .../array-pattern-with-rest/exec.js | 18 + .../ordering--es2015/array-pattern/exec.js | 18 + .../object-pattern-with-rest/exec.js | 21 + .../ordering--es2015/object-pattern/exec.js | 18 + .../fixtures/ordering--es2015/options.json | 8 + .../ordering/array-pattern-with-rest/exec.js | 18 + .../fixtures/ordering/array-pattern/exec.js | 18 + .../ordering/object-pattern-with-rest/exec.js | 21 + .../fixtures/ordering/object-pattern/exec.js | 18 + .../test/fixtures/ordering/options.json | 4 + .../.valid-preset-typescript/input.ts | 4 + .../.valid-preset-typescript/options.json | 4 + .../.valid-preset-typescript/output.js | 10 + .../input.ts | 4 + .../options.json | 6 + .../input.ts | 4 + .../options.json | 3 + .../output.js | 10 + .../array-rest/exec.js | 9 + .../array-rest/input.js | 6 + .../array-rest/output.js | 14 + .../basic/exec.js | 9 + .../basic/input.js | 6 + .../basic/output.js | 17 + .../nested-under-array-pattern/exec.js | 13 + .../nested-under-array-pattern/input.js | 10 + .../nested-under-array-pattern/output.js | 32 ++ .../nested/exec.js | 21 + .../nested/input.js | 12 + .../nested/options.json | 9 + .../nested/output.js | 37 ++ .../non-identifier-keys/input.js | 6 + .../non-identifier-keys/options.json | 4 + .../non-identifier-keys/output.js | 14 + .../object-rest-and-private-keys/exec.js | 15 + .../object-rest-and-private-keys/input.js | 12 + .../object-rest-and-private-keys/output.js | 26 ++ .../object-rest-under-private/exec.js | 12 + .../object-rest-under-private/input.js | 11 + .../object-rest-under-private/output.js | 29 ++ .../object-rest/exec.js | 15 + .../object-rest/input.js | 12 + .../object-rest/options.json | 8 + .../object-rest/output.js | 36 ++ .../variable-declaration--es2015/options.json | 9 + .../variable-declaration/array-rest/exec.js | 9 + .../variable-declaration/array-rest/input.js | 6 + .../variable-declaration/array-rest/output.js | 10 + .../variable-declaration/basic/exec.js | 9 + .../variable-declaration/basic/input.js | 6 + .../variable-declaration/basic/output.js | 13 + .../nested-under-array-pattern/exec.js | 13 + .../nested-under-array-pattern/input.js | 10 + .../nested-under-array-pattern/output.js | 24 + .../variable-declaration/nested/exec.js | 21 + .../variable-declaration/nested/input.js | 12 + .../variable-declaration/nested/output.js | 25 + .../non-identifier-keys/input.js | 6 + .../non-identifier-keys/options.json | 4 + .../non-identifier-keys/output.js | 14 + .../object-rest-and-private-keys/exec.js | 15 + .../object-rest-and-private-keys/input.js | 12 + .../object-rest-and-private-keys/output.js | 20 + .../object-rest-under-private/exec.js | 12 + .../object-rest-under-private/input.js | 11 + .../object-rest-under-private/output.js | 22 + .../variable-declaration/object-rest/exec.js | 15 + .../variable-declaration/object-rest/input.js | 12 + .../object-rest/output.js | 29 ++ .../variable-declaration/options.json | 4 + .../test/index.js | 3 + .../test/package.json | 1 + .../test/plugin-ordering.js | 28 ++ .../test/util.skip-bundled.js | 77 ++++ tsconfig.json | 4 + 194 files changed, 3193 insertions(+) create mode 100644 packages/babel-plugin-proposal-destructuring-private/.npmignore create mode 100644 packages/babel-plugin-proposal-destructuring-private/README.md create mode 100644 packages/babel-plugin-proposal-destructuring-private/package.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/src/index.ts create mode 100644 packages/babel-plugin-proposal-destructuring-private/src/util.ts create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/function-length/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern-with-rest/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern-with-rest/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern-with-rest/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern-with-rest/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/.valid-preset-typescript/input.ts create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/.valid-preset-typescript/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/.valid-preset-typescript/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/invalid-after-destructuring-private/input.ts create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/invalid-after-destructuring-private/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/input.ts create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/index.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/package.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/plugin-ordering.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/util.skip-bundled.js diff --git a/Gulpfile.mjs b/Gulpfile.mjs index 51681902950c..169cfe9719e9 100644 --- a/Gulpfile.mjs +++ b/Gulpfile.mjs @@ -459,6 +459,7 @@ function copyDts(packages) { const libBundles = [ "packages/babel-parser", + "packages/babel-plugin-proposal-destructuring-private", "packages/babel-plugin-proposal-object-rest-spread", "packages/babel-plugin-proposal-optional-chaining", "packages/babel-preset-react", diff --git a/packages/babel-plugin-proposal-destructuring-private/.npmignore b/packages/babel-plugin-proposal-destructuring-private/.npmignore new file mode 100644 index 000000000000..f9806945836e --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/.npmignore @@ -0,0 +1,3 @@ +src +test +*.log diff --git a/packages/babel-plugin-proposal-destructuring-private/README.md b/packages/babel-plugin-proposal-destructuring-private/README.md new file mode 100644 index 000000000000..eeca2d9af0b2 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/README.md @@ -0,0 +1,19 @@ +# @babel/plugin-proposal-destructuring-private + +> Transform destructuring private proposal + +See our website [@babel/plugin-proposal-destructuring-private](https://babeljs.io/docs/en/babel-plugin-proposal-destructuring-private) for more information. + +## Install + +Using npm: + +```sh +npm install --save-dev @babel/plugin-proposal-destructuring-private +``` + +or using yarn: + +```sh +yarn add @babel/plugin-proposal-destructuring-private --dev +``` diff --git a/packages/babel-plugin-proposal-destructuring-private/package.json b/packages/babel-plugin-proposal-destructuring-private/package.json new file mode 100644 index 000000000000..a914fb6682d9 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/package.json @@ -0,0 +1,50 @@ +{ + "name": "@babel/plugin-proposal-destructuring-private", + "version": "0.0.0", + "description": "Transform destructuring private proposal", + "repository": { + "type": "git", + "url": "https://github.com/babel/babel.git", + "directory": "packages/babel-plugin-proposal-destructuring-private" + }, + "license": "MIT", + "publishConfig": { + "access": "public" + }, + "main": "./lib/index.js", + "keywords": [ + "babel-plugin" + ], + "dependencies": { + "@babel/helper-plugin-utils": "workspace:^", + "@babel/plugin-syntax-destructuring-private": "workspace:^", + "@babel/plugin-transform-destructuring": "workspace:^", + "@babel/plugin-transform-parameters": "workspace:^" + }, + "peerDependencies": { + "@babel/core": "^7.17.0" + }, + "devDependencies": { + "@babel/core": "workspace:^", + "@babel/helper-plugin-test-runner": "workspace:^", + "@babel/traverse": "workspace:^", + "@babel/types": "workspace:^" + }, + "homepage": "https://babel.dev/docs/en/next/babel-plugin-proposal-destructuring-private", + "engines": { + "node": ">=6.9.0" + }, + "author": "The Babel Team (https://babel.dev/team)", + "conditions": { + "BABEL_8_BREAKING": [ + null, + { + "exports": null + } + ] + }, + "exports": { + ".": "./lib/index.js", + "./package.json": "./package.json" + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/src/index.ts b/packages/babel-plugin-proposal-destructuring-private/src/index.ts new file mode 100644 index 000000000000..bb33c86057e1 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/src/index.ts @@ -0,0 +1,205 @@ +import { declare } from "@babel/helper-plugin-utils"; +import syntaxDestructuringPrivate from "@babel/plugin-syntax-destructuring-private"; +import { + hasPrivateKeys, + hasPrivateClassElement, + transformPrivateKeyDestructuring, + buildVariableDeclarationFromParams, +} from "./util"; +import { convertFunctionParams } from "@babel/plugin-transform-parameters"; + +import type { PluginPass } from "@babel/core"; +import type { Visitor } from "@babel/traverse"; +import { types as t } from "@babel/core"; + +const { + assignmentExpression, + cloneNode, + expressionStatement, + isExpressionStatement, + isIdentifier, + isSequenceExpression, + sequenceExpression, + variableDeclaration, + variableDeclarator, +} = t; + +export default declare(function ({ + assertVersion, + assumption, +}: { + assumption: (string) => boolean; + assertVersion: (string) => void; +}) { + assertVersion("^7.17.0"); + + const ignoreFunctionLength = assumption("ignoreFunctionLength"); + const objectRestNoSymbols = assumption("objectRestNoSymbols"); + + const privateKeyDestructuringVisitor: Visitor = { + Function(path) { + // (b, { #x: x } = I) => body + // transforms to: + // (p1, p2) => { var b = p1, { #x: x } = p2 === undefined ? I : p2; body; } + if (!path.node.params.some(param => hasPrivateKeys(param))) return; + // wrap function body within IIFE if any param is shadowed + convertFunctionParams(path, ignoreFunctionLength, () => false, false); + const { node, scope } = path; + const { params, variableDeclaration } = + buildVariableDeclarationFromParams(node.params, scope); + + path + .get("body") // invariant: path.body is always a BlockStatement + .unshiftContainer("body", variableDeclaration); + node.params = params; + scope.crawl(); + // the pattern will be handled by VariableDeclaration visitor. + }, + CatchClause(path) { + // catch({ #x: x }) { body } + // transforms to: + // catch(_e) { var {#x: x } = _e; body } + const { node, scope } = path; + if (!hasPrivateKeys(node.param)) return; + // todo: handle shadowed param as we did in convertFunctionParams + const ref = scope.generateUidIdentifier("e"); + path + .get("body") + .unshiftContainer( + "body", + variableDeclaration("var", [variableDeclarator(node.param, ref)]), + ); + node.param = cloneNode(ref); + // the pattern will be handled by VariableDeclaration visitor. + }, + ForXStatement(path) { + const { node, scope } = path; + const leftPath = path.get("left"); + if (leftPath.isVariableDeclaration()) { + const left = leftPath.node; + if (!hasPrivateKeys(left.declarations[0].id)) return; + // for (const { #x: x } of cls) body; + // transforms to: + // for (const ref of cls) { const { #x: x } = ref; body; } + const declarator = scope.generateUidIdentifier("ref"); + node.left = variableDeclaration(left.kind, [ + variableDeclarator(declarator, null), + ]); + left.declarations[0].init = cloneNode(declarator); + path.ensureBlock(); + // todo: handle shadowed variables referenced in computed keys: + // var a = 0;for (const { #x: x, [a++]: y } of z) { const a = 1; } + const blockBody = (node.body as t.BlockStatement).body; + blockBody.unshift(left); + scope.crawl(); + // the pattern will be handled by VariableDeclaration visitor. + } else if (leftPath.isPattern()) { + if (!hasPrivateKeys(leftPath.node)) return; + // for ({ #x: x } of cls); + // transforms to: + // for (const ref of cls) { ({ #x: x } = ref); body; } + // This transform assumes that any expression within the pattern + // does not interfere with the iterable `cls`. + const declarator = scope.generateUidIdentifier("ref"); + node.left = variableDeclaration("const", [ + variableDeclarator(declarator, null), + ]); + path.ensureBlock(); + + const blockBody = (node.body as t.BlockStatement).body; + + // preserve completion record: + // for ({ #x: x } of []); + if (blockBody.length === 0 && path.isCompletionRecord()) { + blockBody.unshift(expressionStatement(scope.buildUndefinedNode())); + } + + // todo: handle shadowed variables referenced in computed keys: + // var a = 0, x;for ({ #x: x, [a++]: y } of z) { const a = 1; } + blockBody.unshift( + expressionStatement( + assignmentExpression("=", leftPath.node, cloneNode(declarator)), + ), + ); + } + }, + VariableDeclaration(path, state) { + const { scope, node } = path; + const { declarations } = node; + if (!declarations.some(declarator => hasPrivateKeys(declarator.id))) { + return; + } + const newDeclarations = []; + for (const declarator of declarations) { + for (const { left, right } of transformPrivateKeyDestructuring( + declarator.id, + declarator.init, + scope, + /* isAssignment */ false, + name => state.addHelper(name), + objectRestNoSymbols, + /* useBuiltIns */ true, + )) { + newDeclarations.push(variableDeclarator(left, right)); + } + } + node.declarations = newDeclarations; + scope.crawl(); + }, + + AssignmentExpression(path, state) { + const { node, scope, parent } = path; + if (!hasPrivateKeys(node.left)) return; + const assignments = []; + for (const { left, right } of transformPrivateKeyDestructuring( + node.left, + node.right, + scope, + /* isAssignment */ true, + name => state.addHelper(name), + objectRestNoSymbols, + /* useBuiltIns */ true, + )) { + assignments.push(assignmentExpression("=", left, right)); + } + // preserve completion record + if ( + (!isExpressionStatement(parent) && !isSequenceExpression(parent)) || + path.isCompletionRecord() + ) { + const { left } = assignments[0]; + if (scope.isStatic(node.right)) { + const tempId = scope.generateDeclaredUidIdentifier("m"); + assignments.unshift( + assignmentExpression("=", tempId, cloneNode(node.right)), + ); + assignments.push(cloneNode(tempId)); + } else if ( + !isIdentifier(assignments[assignments.length - 1].right, { + name: left.name, + }) + ) { + // If node.right is non-static and then the left is an effectively-constant memoised id + // If the last assignment does not end with left, that we can safely reuse `left` as the completion value + assignments.push(cloneNode(left)); + } + } + + path.replaceWith(sequenceExpression(assignments)); + scope.crawl(); + }, + }; + + const visitor: Visitor = { + Class(path, state) { + if (!hasPrivateClassElement(path.node.body)) return; + path.traverse(privateKeyDestructuringVisitor, state); + }, + }; + + return { + name: "proposal-destructuring-private", + inherits: syntaxDestructuringPrivate, + visitor: visitor, + }; +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/src/util.ts b/packages/babel-plugin-proposal-destructuring-private/src/util.ts new file mode 100644 index 000000000000..757c75784059 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/src/util.ts @@ -0,0 +1,428 @@ +import type * as t from "@babel/types"; +import type { Scope } from "@babel/traverse"; +import { types } from "@babel/core"; +import type { File } from "@babel/core"; +import { buildObjectExcludingKeys } from "@babel/plugin-transform-destructuring"; +import { assignmentExpression, ObjectProperty } from "@babel/types"; +const { + binaryExpression, + conditionalExpression, + cloneNode, + isObjectProperty, + isPrivateName, + memberExpression, + numericLiteral, + objectPattern, + restElement, + variableDeclarator, + variableDeclaration, + unaryExpression, +} = types; + +function buildUndefinedNode() { + return unaryExpression("void", numericLiteral(0)); +} + +function transformAssignmentPattern( + initializer: t.Expression, + tempId: t.Identifier, +) { + return conditionalExpression( + binaryExpression("===", cloneNode(tempId), buildUndefinedNode()), + initializer, + cloneNode(tempId), + ); +} + +function initRestExcludingKeys(pattern: t.LVal) { + if (pattern.type === "ObjectPattern") { + const { properties } = pattern; + if (properties[properties.length - 1].type === "RestElement") { + return []; + } + } + return null; +} + +/** + * grow restExcludingKeys on given properties. This routine mutates properties by + * memoising the computed non-static keys. + * + * @param {ObjectProperty[]} properties An array of object properties that should be excluded by rest element transform + * @param {Scope} scope Where should we register the memoised id + */ +function* growRestExcludingKeys(properties: ObjectProperty[], scope: Scope) { + for (const property of properties) { + if (property.computed && !scope.isStatic(property.key)) { + const tempId = scope.generateDeclaredUidIdentifier("m"); + // @ts-expect-error A computed property key must not be a private name + property.key = assignmentExpression("=", tempId, property.key); + yield { key: tempId, computed: true }; + } else { + yield property; + } + } +} + +/** + * Prepare var declarations for params. Only param initializers + * will be transformed to undefined coalescing, other features are preserved. + * This function does NOT mutate given AST structures. + * + * @export + * @param {Function["params"]} params An array of function params + * @param {Scope} scope A scope used to generate uid for function params + * @returns {{ params: Identifier[]; variableDeclaration: VariableDeclaration }} An array of new id for params + * and variable declaration to be prepended to the function body + */ +export function buildVariableDeclarationFromParams( + params: t.Function["params"], + scope: Scope, +): { + params: (t.Identifier | t.RestElement)[]; + variableDeclaration: t.VariableDeclaration; +} { + const { elements, transformed } = buildAssignmentsFromPatternList( + params, + scope, + /* isAssignment */ false, + ); + return { + params: elements, + variableDeclaration: variableDeclaration( + "var", + transformed.map(({ left, right }) => variableDeclarator(left, right)), + ), + }; +} + +interface Transformed { + left: t.Identifier | t.Pattern | t.MemberExpression; + right: t.Expression; +} + +function buildAssignmentsFromPatternList( + elements: (t.LVal | null)[], + scope: Scope, + isAssignment: boolean, +): { + elements: (t.Identifier | t.RestElement | null)[]; + transformed: Transformed[]; +} { + const newElements: (t.Identifier | t.RestElement)[] = [], + transformed = []; + for (let element of elements) { + if (element === null) { + newElements.push(null); + transformed.push(null); + continue; + } + const tempId = scope.generateUidIdentifier("p"); + if (isAssignment) { + scope.push({ id: cloneNode(tempId) }); + } + if (element.type === "RestElement") { + newElements.push(restElement(tempId)); + // The argument of a RestElement within a BindingPattern must be either Identifier or BindingPattern + element = element.argument as t.Identifier | t.Pattern; + } else { + newElements.push(tempId); + } + if (element.type === "AssignmentPattern") { + transformed.push({ + left: element.left, + right: transformAssignmentPattern(element.right, tempId), + }); + } else { + transformed.push({ + left: element, + right: cloneNode(tempId), + }); + } + } + return { elements: newElements, transformed }; +} + +/** + * A DFS simplified pattern traverser. It skips computed property keys and assignment pattern + * initializers. The following-type path will be delegate to the visitor: + * - ArrayPattern + * - ArrayPattern elements + * - AssignmentPattern + * - ObjectPattern + * - ObjectProperty + * - RestElement + * @param root + * @param visitor + */ +export function* traversePattern( + root: t.LVal, + visitor: ( + node: t.LVal | t.ObjectProperty, + index: number, + depth: number, + ) => Generator, +) { + const stack = []; + stack.push({ node: root, index: 0, depth: 0 }); + let item: { + node: t.LVal | t.ObjectProperty | null; + index: number; + depth: number; + }; + while ((item = stack.pop()) !== undefined) { + const { node, index } = item; + if (node === null) continue; + yield* visitor(node, index, item.depth); + const depth = item.depth + 1; + switch (node.type) { + case "AssignmentPattern": + stack.push({ node: node.left, index: 0, depth }); + break; + case "ObjectProperty": + // inherit the depth and index as an object property can not be an LHS without object pattern + stack.push({ node: node.value, index, depth: item.depth }); + break; + case "RestElement": + stack.push({ node: node.argument, index: 0, depth }); + break; + case "ObjectPattern": + for (let list = node.properties, i = list.length - 1; i >= 0; i--) { + stack.push({ node: list[i], index: i, depth }); + } + break; + case "ArrayPattern": + for (let list = node.elements, i = list.length - 1; i >= 0; i--) { + stack.push({ node: list[i], index: i, depth }); + } + break; + case "TSParameterProperty": + throw new Error( + `TypeScript parameter properties must first be transformed by ` + + `@babel/plugin-transform-typescript.\n` + + `If you have already enabled that plugin (or '@babel/preset-typescript'), make sure ` + + `that it runs before @babel/plugin-proposal-destructuring-private.`, + ); + default: + break; + } + } +} + +export function hasPrivateKeys(pattern: t.LVal) { + return ( + traversePattern(pattern, function* (node) { + if (isObjectProperty(node) && isPrivateName(node.key)) { + yield; + } + }).next().done === false + ); +} + +export function hasPrivateClassElement(node: t.ClassBody): boolean { + return node.body.some(element => + isPrivateName( + // @ts-expect-error: for those class element without `key`, they must + // not be a private element + element.key, + ), + ); +} + +/** + * Traverse the given pattern and report the private key path. + * A private key path is analagous to an array of `key` from the pattern NodePath + * to the private key NodePath. See also test/util.skip-bundled.js for an example output + * + * @export + * @param {t.LVal} pattern + */ +export function* privateKeyPathIterator(pattern: t.LVal) { + const indexPath = []; + yield* traversePattern(pattern, function* (node, index, depth) { + indexPath[depth] = index; + if (isObjectProperty(node)) { + const propertyKey = node.key; + if (isPrivateName(propertyKey)) { + yield indexPath.slice(1, depth + 1); + } + } + }); +} + +type Item = { + left: t.LVal; + right: t.Expression; + restExcludingKeys?: t.ObjectProperty[] | null; +}; + +/** + * Transform private destructuring without object rest element. It returns a generator + * which yields a pair of transformed LHS and RHS, which can form VariableDeclaration or + * AssignmentExpression later. + * + * @export + * @param {t.LVal} left The root pattern + * @param {t.Expression} right The initializer or the RHS of pattern + * @param {Scope} scope The scope where memoized id should be registered + * @param {boolean} isAssignment Whether we are transforming from an AssignmengExpression of VariableDeclaration + * @returns {Generator} + */ +export function* transformPrivateKeyDestructuring( + left: t.LVal, + right: t.Expression, + scope: Scope, + isAssignment: boolean, + addHelper: File["addHelper"], + objectRestNoSymbols: boolean, + useBuiltIns: boolean, +): Generator { + const stack: Item[] = []; + // The stack holds patterns that we don't known whether they contain private key + stack.push({ + left, + right, + restExcludingKeys: initRestExcludingKeys(left), + }); + let item: Item; + while ((item = stack.pop()) !== undefined) { + const { restExcludingKeys } = item; + let { left, right } = item; + const searchPrivateKey = privateKeyPathIterator(left).next(); + if (searchPrivateKey.done) { + if (restExcludingKeys != null && restExcludingKeys.length > 0) { + // optimize out the rest element because `objectWithoutProperties` + // always return a new object + // `{ ...z } = babelHelpers.objectWithoutProperties(m, ["x"])` + // to + // `z = babelHelpers.objectWithoutProperties(m, ["x"])` + const { properties } = left as t.ObjectPattern; + if (properties.length === 1) { + left = (properties[0] as t.RestElement).argument; + } + yield { + left: left as t.ObjectPattern, + right: buildObjectExcludingKeys( + restExcludingKeys, + right, + scope, + addHelper, + objectRestNoSymbols, + useBuiltIns, + ), + }; + } else { + // @ts-expect-error left must not contain RestElement if restExcludingKeys is nullish + yield { left, right }; + } + } else { + // now we need to split according to the indexPath; + const indexPath = searchPrivateKey.value; + let index; + let isFirst = true; + while ( + (index = indexPath.shift()) !== undefined || + left.type === "AssignmentPattern" + ) { + if (!scope.isStatic(right) && left.type !== "Identifier") { + const tempId = scope.generateUidIdentifier("m"); + if (isAssignment) { + scope.push({ id: cloneNode(tempId) }); + } + yield { left: tempId, right }; + right = cloneNode(tempId); + } + // invariant: at this point right must be a static identifier; + switch (left.type) { + case "ObjectPattern": { + const { properties } = left; + // inherit the restExcludingKeys on the stack if we are at + // the first level, otherwise initialize a new restExcludingKeys + let nextRestExcludingKeys = restExcludingKeys; + if (!isFirst) { + nextRestExcludingKeys = initRestExcludingKeys(left); + } + if (index > 0) { + // properties[0, index) must not contain private keys + const propertiesSlice = properties.slice(0, index); + if (nextRestExcludingKeys !== null) { + nextRestExcludingKeys.push( + // @ts-expect-error + ...growRestExcludingKeys(propertiesSlice, scope), + ); + } + yield { + left: objectPattern(propertiesSlice), + right: cloneNode(right), + }; + } + if (index < properties.length - 1) { + // for properties after `index`, push them to stack so we can process them later + stack.push({ + left: objectPattern(properties.slice(index + 1)), + right: cloneNode(right), + restExcludingKeys: nextRestExcludingKeys, + }); + } + // An object rest element must not contain a private key + const property = properties[index] as t.ObjectProperty; + left = property.value as t.LVal; + const { key } = property; + const computed = + property.computed || + // `{ 0: x } = RHS` is transformed to a computed member expression `x = RHS[0]` + (key.type !== "Identifier" && key.type !== "PrivateName"); + right = memberExpression(right, key, computed); + break; + } + case "AssignmentPattern": { + right = transformAssignmentPattern( + left.right, + right as t.Identifier, + ); + left = left.left; + break; + } + case "ArrayPattern": { + // todo: the transform here assumes that any expression within + // the array pattern, when evluated, do not interfere with the iterable + // in RHS. Otherwise we have to pause the iterable and interleave + // the expressions. + // See also https://gist.github.com/nicolo-ribaudo/f8ac7916f89450f2ead77d99855b2098 + const { elements, transformed } = buildAssignmentsFromPatternList( + left.elements, + scope, + isAssignment, + ); + left.elements = elements; + yield { left, right: cloneNode(right) }; + // we are sure elements[0, index) does not contain private keys + for (let i = 0; i < index; i++) { + // skipping array holes + if (transformed[i] !== null) { + yield transformed[i]; + } + } + // for elements after `index`, push them to stack so we can process them later + for (let i = transformed.length - 1; i > index; i--) { + // skipping array holes + if (transformed[i] !== null) { + stack.push(transformed[i]); + } + } + ({ left, right } = transformed[index]); + break; + } + default: + break; + } + isFirst = false; + } + stack.push({ + left, + right, + restExcludingKeys: initRestExcludingKeys(left), + }); + } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/exec.js new file mode 100644 index 000000000000..54c0347d10e9 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/exec.js @@ -0,0 +1,10 @@ +let x, z; +class C { + static #x; + static { + ([{ #x: x = 1 }, ...z] = [C]); + } +} + +expect(x).toBe(1); +expect(z).toStrictEqual([]); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/input.js new file mode 100644 index 000000000000..fc244ab5af80 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/input.js @@ -0,0 +1,7 @@ +let x, z; +class C { + static #x; + static { + ([{ #x: x = 1 }, ...z] = [C]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/output.js new file mode 100644 index 000000000000..25816df07323 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/output.js @@ -0,0 +1,14 @@ +let x, z; + +class C {} + +var _x = { + writable: true, + value: void 0 +}; + +(() => { + var _m, _p, _p2, _m2; + + _m = [C], [_p, ..._p2] = _m, _m2 = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x), x = _m2 === void 0 ? 1 : _m2, z = _p2; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/exec.js new file mode 100644 index 000000000000..b0c00d030344 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/exec.js @@ -0,0 +1,10 @@ +let a, x, b; +class C { + static #x; + static { + ({ a = 1, #x: x = 2, b = 3 } = C); + } +} +expect(a).toBe(1); +expect(x).toBe(2); +expect(b).toBe(3); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/input.js new file mode 100644 index 000000000000..16d994cde245 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/input.js @@ -0,0 +1,7 @@ +let a, x, b; +class C { + static #x; + static { + ({ a = 1, #x: x = 2, b = 3 } = C); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/output.js new file mode 100644 index 000000000000..42e29778c281 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/basic/output.js @@ -0,0 +1,18 @@ +let a, x, b; + +class C {} + +var _x = { + writable: true, + value: void 0 +}; + +(() => { + var _m; + + ({ + a = 1 + } = C), _m = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), x = _m === void 0 ? 2 : _m, ({ + b = 3 + } = C); +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion/exec.js new file mode 100644 index 000000000000..f56e05c08f33 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion/exec.js @@ -0,0 +1,13 @@ +let r1, r2, r3; +class C { + static #x = { b: 2 }; + static { + let a, b; + r1 = ({ a = 1, #x: { b }} = C); + (function f(r = ({ #x: { b }} = C)) { r2 = r })(); + ((g = (r = ({ #x: { a } } = { #x: { b }} = C)) => { r3 = r }) => g())(); + } +} +expect(r1).toBe(C); +expect(r2).toBe(C); +expect(r3).toBe(C); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/exec.js new file mode 100644 index 000000000000..3c865df1a3e8 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/exec.js @@ -0,0 +1,11 @@ +let x; +class C { + static #x; + static #y; + static #z; + static { + let z; + ([C.#x, { #x: x }, ...z] = [0, C]); + } +} +expect(x).toBe(0); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/input.js new file mode 100644 index 000000000000..45be876183ce --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/input.js @@ -0,0 +1,10 @@ +let x; +class C { + static #x; + static #y; + static #z; + static { + let z; + ([C.#x, { #x: x }, ...z] = [0, C]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/output.js new file mode 100644 index 000000000000..c63cef733485 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/output.js @@ -0,0 +1,23 @@ +let x; + +class C {} + +var _x = { + writable: true, + value: void 0 +}; +var _y = { + writable: true, + value: void 0 +}; +var _z = { + writable: true, + value: void 0 +}; + +(() => { + var _m, _p, _p2, _p3; + + let z; + _m = [0, C], [_p, _p2, ..._p3] = _m, babelHelpers.classStaticPrivateFieldSpecSet(C, C, _x, _p), x = babelHelpers.classStaticPrivateFieldSpecGet(_p2, C, _x), z = _p3; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/exec.js new file mode 100644 index 000000000000..686556b8466b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/exec.js @@ -0,0 +1,14 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + static #self() { return C } + static { + let x, y, z; + [{ #x: x } = C.self, { #y: [,{ #z: y = C.#self() } = C.self ] },,z = y.#y] = [this,this]; + expect(x).toBe("x"); + expect(y).toBe(C); + expect(z).toStrictEqual([]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/input.js new file mode 100644 index 000000000000..5ca915829bdb --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/input.js @@ -0,0 +1,11 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + static #self() { return C } + static { + let x, y, z; + [{ #x: x } = C.self, { #y: [,{ #z: y = C.#self() } = C.self ] },,z = y.#y] = [this,this]; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/output.js new file mode 100644 index 000000000000..1087c312042c --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/output.js @@ -0,0 +1,26 @@ +class C {} + +function _self() { + return C; +} + +var _x = { + writable: true, + value: "x" +}; +var _y = { + writable: true, + value: [] +}; +var _z = { + writable: true, + value: void 0 +}; +babelHelpers.defineProperty(C, "self", C); + +(() => { + var _m, _p, _p2, _p3, _m2, _m3, _p4, _m4, _m5; + + let x, y, z; + _m = [C, C], [_p, _p2,, _p3] = _m, _m2 = _p === void 0 ? C.self : _p, x = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _x), _m3 = babelHelpers.classStaticPrivateFieldSpecGet(_p2, C, _y), [, _p4] = _m3, _m4 = _p4 === void 0 ? C.self : _p4, _m5 = babelHelpers.classStaticPrivateFieldSpecGet(_m4, C, _z), y = _m5 === void 0 ? babelHelpers.classStaticPrivateMethodGet(C, C, _self).call(C) : _m5, z = _p3 === void 0 ? babelHelpers.classStaticPrivateFieldSpecGet(y, C, _y) : _p3; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/exec.js new file mode 100644 index 000000000000..c59766b8f366 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/exec.js @@ -0,0 +1,21 @@ +let result; +class C { + static #y = "y"; + static #z = "self"; + static #x; + static b = "b"; + static self = C; + static #self = C; + static { + let cloned, b, y, yy, yy2; + ({ #x: { [C.#z]: { b, #x: y = (C.b = "bb", C.#self.#y) }, #x: yy = (delete C.self, { ...cloned } = C, C.#y = "yy") } = C.#self, #y: yy2 } = C); + result = { b, y, yy, cloned, yy2 }; + } +} +expect(result).toStrictEqual({ + b: "b", + y: "y", + yy: "yy", + cloned: { b: "bb" }, + yy2: "yy" +}) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/input.js new file mode 100644 index 000000000000..890f16f203bd --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/input.js @@ -0,0 +1,12 @@ +class C { + static #y = "y"; + static #z = "self"; + static #x; + static b = "b"; + static self = C; + static #self = C; + static { + let cloned, b, y, yy, yy2; + ({ #x: { [C.#z]: { b, #x: y = (C.b = "bb", C.#self.#y) }, #x: yy = (delete C.self, { ...cloned } = C, C.#y = "yy") } = C.#self, #y: yy2 } = C); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/options.json new file mode 100644 index 000000000000..48eb48aad7d6 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/options.json @@ -0,0 +1,9 @@ +{ + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties", + "proposal-private-methods" + ], + "minNodeVersion": "8.0.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/output.js new file mode 100644 index 000000000000..d207099e0f1f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested/output.js @@ -0,0 +1,30 @@ +class C {} + +var _y = { + writable: true, + value: "y" +}; +var _z = { + writable: true, + value: "self" +}; +var _x = { + writable: true, + value: void 0 +}; +babelHelpers.defineProperty(C, "b", "b"); +babelHelpers.defineProperty(C, "self", C); +var _self = { + writable: true, + value: C +}; + +(() => { + var _m, _m2, _m3, _m4, _m5; + + let cloned, b, y, yy, yy2; + _m = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), _m2 = _m === void 0 ? babelHelpers.classStaticPrivateFieldSpecGet(C, C, _self) : _m, _m3 = _m2[babelHelpers.classStaticPrivateFieldSpecGet(C, C, _z)], ({ + b + } = _m3), _m4 = babelHelpers.classStaticPrivateFieldSpecGet(_m3, C, _x), y = _m4 === void 0 ? (C.b = "bb", babelHelpers.classStaticPrivateFieldSpecGet(babelHelpers.classStaticPrivateFieldSpecGet(C, C, _self), C, _y)) : _m4, _m5 = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _x), yy = _m5 === void 0 ? (delete C.self, ({ ...cloned + } = C), babelHelpers.classStaticPrivateFieldSpecSet(C, C, _y, "yy")) : _m5, yy2 = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _y); +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/exec.js new file mode 100644 index 000000000000..fcfcf3bafcd5 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/exec.js @@ -0,0 +1,16 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + let x, y, z; + ({ #x: x, #y: y, ...z } = C); + result = { x, y, z }; + } +} +expect(result).toStrictEqual({ + x: "#x", y: "#y", z: { a: "a", b: "b", c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/input.js new file mode 100644 index 000000000000..f29b28b5a652 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/input.js @@ -0,0 +1,13 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + let x, y, z; + ({ #x: x, #y: y, ...z } = C); + result = { x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/output.js new file mode 100644 index 000000000000..d13cce9b234b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-private-keys/output.js @@ -0,0 +1,27 @@ +let result; + +class C {} + +var _x = { + writable: true, + value: "#x" +}; +var _y = { + writable: true, + value: "#y" +}; +babelHelpers.defineProperty(C, "a", "a"); +babelHelpers.defineProperty(C, "b", "b"); +babelHelpers.defineProperty(C, "c", "c"); + +(() => { + var _C; + + let x, y, z; + x = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), y = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _y), (_C = C, ({} = _C), z = Object.assign({}, _C), _C); + result = { + x, + y, + z + }; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/exec.js new file mode 100644 index 000000000000..6dff97a97aff --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/exec.js @@ -0,0 +1,13 @@ +let result; +class C { + static x = "x"; + static y = "y"; + static z = "z"; + static #x = C; + static { + var x, y, z; + ({ x, #x: { y, ...z } } = C); + result = { x, y, z }; + } +} +expect(result).toStrictEqual({ x: "x", y: "y", z: { x: "x", z: "z" } }) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/input.js new file mode 100644 index 000000000000..d67ebdde0914 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/input.js @@ -0,0 +1,12 @@ +let result; +class C { + static x = "x"; + static y = "y"; + static z = "z"; + static #x = C; + static { + var x, y, z; + ({ x, #x: { y, ...z } } = C); + result = { x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/output.js new file mode 100644 index 000000000000..3ada6df296df --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-under-private/output.js @@ -0,0 +1,28 @@ +const _excluded = ["y"]; +let result; + +class C {} + +babelHelpers.defineProperty(C, "x", "x"); +babelHelpers.defineProperty(C, "y", "y"); +babelHelpers.defineProperty(C, "z", "z"); +var _x = { + writable: true, + value: C +}; + +(() => { + var _babelHelpers$classSt; + + var x, y, z; + ({ + x + } = C), (_babelHelpers$classSt = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), ({ + y + } = _babelHelpers$classSt), z = babelHelpers.objectWithoutProperties(_babelHelpers$classSt, _excluded), _babelHelpers$classSt); + result = { + x, + y, + z + }; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/exec.js new file mode 100644 index 000000000000..f03d0735bead --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/exec.js @@ -0,0 +1,16 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var a, b, x, y, z; + ({ [C.a]: a, #x: x, [C.b]: b, #y: y, ...z } = C); + result = { a, b, x, y, z }; + } +} +expect(result).toStrictEqual({ + a: "a", b: "b", x: "#x", y: "#y", z: { c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/input.js new file mode 100644 index 000000000000..231b2f804345 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/input.js @@ -0,0 +1,13 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var a, b, x, y, z; + ({ [C.a]: a, #x: x, [C.b]: b, #y: y, ...z } = C); + result = { a, b, x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/output.js new file mode 100644 index 000000000000..eab489f2dde1 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest/output.js @@ -0,0 +1,33 @@ +let result; + +class C {} + +var _x = { + writable: true, + value: "#x" +}; +var _y = { + writable: true, + value: "#y" +}; +babelHelpers.defineProperty(C, "a", "a"); +babelHelpers.defineProperty(C, "b", "b"); +babelHelpers.defineProperty(C, "c", "c"); + +(() => { + var _m, _m2; + + var a, b, x, y, z; + ({ + [_m = C.a]: a + } = C), x = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), ({ + [_m2 = C.b]: b + } = C), y = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _y), z = babelHelpers.objectWithoutProperties(C, [_m, _m2].map(babelHelpers.toPropertyKey)); + result = { + a, + b, + x, + y, + z + }; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/options.json new file mode 100644 index 000000000000..e1c5858604ed --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/options.json @@ -0,0 +1,9 @@ +{ + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties", + "proposal-private-methods", + ["proposal-object-rest-spread", { "useBuiltIns": true }] + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/exec.js new file mode 100644 index 000000000000..387b1623a8f9 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/exec.js @@ -0,0 +1,11 @@ +let a,b; +class C { + static #x = { a: 1, b: 2 }; + static { + (function f(r = { #x: { b }} = C) {})() + } + static m(r = { #x: { a } } = C) {} +} +C.m(); +expect(a).toBe(1); +expect(b).toBe(2); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/input.js new file mode 100644 index 000000000000..cd50bcd759bf --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/input.js @@ -0,0 +1,10 @@ +let a; +class C { + static #x = { a: 1, b: 2 }; + static { + let b; + (function f(r = { #x: { b }} = C) {})() + } + static m(r = { #x: { a } } = C) {} +} +C.m(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/output.js new file mode 100644 index 000000000000..829bd11aa63a --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/output.js @@ -0,0 +1,30 @@ +var _m2; + +let a; + +class C { + static m(r = (_m2 = C, ({ + a + } = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x)), _m2)) {} + +} + +var _x = { + writable: true, + value: { + a: 1, + b: 2 + } +}; + +(() => { + var _m; + + let b; + + (function f(r = (_m = C, ({ + b + } = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x)), _m)) {})(); +})(); + +C.m(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/exec.js new file mode 100644 index 000000000000..54c0347d10e9 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/exec.js @@ -0,0 +1,10 @@ +let x, z; +class C { + static #x; + static { + ([{ #x: x = 1 }, ...z] = [C]); + } +} + +expect(x).toBe(1); +expect(z).toStrictEqual([]); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/input.js new file mode 100644 index 000000000000..fc244ab5af80 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/input.js @@ -0,0 +1,7 @@ +let x, z; +class C { + static #x; + static { + ([{ #x: x = 1 }, ...z] = [C]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/output.js new file mode 100644 index 000000000000..1ca9c8ead1cf --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/output.js @@ -0,0 +1,10 @@ +let x, z; + +class C { + static #x; + static { + var _m, _p, _p2, _m2; + + _m = [C], [_p, ..._p2] = _m, _m2 = _p.#x, x = _m2 === void 0 ? 1 : _m2, z = _p2; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/exec.js new file mode 100644 index 000000000000..b0c00d030344 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/exec.js @@ -0,0 +1,10 @@ +let a, x, b; +class C { + static #x; + static { + ({ a = 1, #x: x = 2, b = 3 } = C); + } +} +expect(a).toBe(1); +expect(x).toBe(2); +expect(b).toBe(3); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/input.js new file mode 100644 index 000000000000..16d994cde245 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/input.js @@ -0,0 +1,7 @@ +let a, x, b; +class C { + static #x; + static { + ({ a = 1, #x: x = 2, b = 3 } = C); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/output.js new file mode 100644 index 000000000000..9e2c02707338 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/basic/output.js @@ -0,0 +1,14 @@ +let a, x, b; + +class C { + static #x; + static { + var _m; + + ({ + a = 1 + } = C), _m = C.#x, x = _m === void 0 ? 2 : _m, ({ + b = 3 + } = C); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion/exec.js new file mode 100644 index 000000000000..f56e05c08f33 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion/exec.js @@ -0,0 +1,13 @@ +let r1, r2, r3; +class C { + static #x = { b: 2 }; + static { + let a, b; + r1 = ({ a = 1, #x: { b }} = C); + (function f(r = ({ #x: { b }} = C)) { r2 = r })(); + ((g = (r = ({ #x: { a } } = { #x: { b }} = C)) => { r3 = r }) => g())(); + } +} +expect(r1).toBe(C); +expect(r2).toBe(C); +expect(r3).toBe(C); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/exec.js new file mode 100644 index 000000000000..3c865df1a3e8 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/exec.js @@ -0,0 +1,11 @@ +let x; +class C { + static #x; + static #y; + static #z; + static { + let z; + ([C.#x, { #x: x }, ...z] = [0, C]); + } +} +expect(x).toBe(0); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/input.js new file mode 100644 index 000000000000..45be876183ce --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/input.js @@ -0,0 +1,10 @@ +let x; +class C { + static #x; + static #y; + static #z; + static { + let z; + ([C.#x, { #x: x }, ...z] = [0, C]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/output.js new file mode 100644 index 000000000000..ff4f866363fe --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/output.js @@ -0,0 +1,13 @@ +let x; + +class C { + static #x; + static #y; + static #z; + static { + var _m, _p, _p2, _p3; + + let z; + _m = [0, C], [_p, _p2, ..._p3] = _m, C.#x = _p, x = _p2.#x, z = _p3; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/exec.js new file mode 100644 index 000000000000..686556b8466b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/exec.js @@ -0,0 +1,14 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + static #self() { return C } + static { + let x, y, z; + [{ #x: x } = C.self, { #y: [,{ #z: y = C.#self() } = C.self ] },,z = y.#y] = [this,this]; + expect(x).toBe("x"); + expect(y).toBe(C); + expect(z).toStrictEqual([]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/input.js new file mode 100644 index 000000000000..5ca915829bdb --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/input.js @@ -0,0 +1,11 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + static #self() { return C } + static { + let x, y, z; + [{ #x: x } = C.self, { #y: [,{ #z: y = C.#self() } = C.self ] },,z = y.#y] = [this,this]; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/output.js new file mode 100644 index 000000000000..7cde39fb5f0f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/output.js @@ -0,0 +1,17 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + + static #self() { + return C; + } + + static { + var _m, _p, _p2, _p3, _m2, _m3, _p4, _m4, _m5; + + let x, y, z; + _m = [this, this], [_p, _p2,, _p3] = _m, _m2 = _p === void 0 ? C.self : _p, x = _m2.#x, _m3 = _p2.#y, [, _p4] = _m3, _m4 = _p4 === void 0 ? C.self : _p4, _m5 = _m4.#z, y = _m5 === void 0 ? C.#self() : _m5, z = _p3 === void 0 ? y.#y : _p3; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/exec.js new file mode 100644 index 000000000000..c59766b8f366 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/exec.js @@ -0,0 +1,21 @@ +let result; +class C { + static #y = "y"; + static #z = "self"; + static #x; + static b = "b"; + static self = C; + static #self = C; + static { + let cloned, b, y, yy, yy2; + ({ #x: { [C.#z]: { b, #x: y = (C.b = "bb", C.#self.#y) }, #x: yy = (delete C.self, { ...cloned } = C, C.#y = "yy") } = C.#self, #y: yy2 } = C); + result = { b, y, yy, cloned, yy2 }; + } +} +expect(result).toStrictEqual({ + b: "b", + y: "y", + yy: "yy", + cloned: { b: "bb" }, + yy2: "yy" +}) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/input.js new file mode 100644 index 000000000000..890f16f203bd --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/input.js @@ -0,0 +1,12 @@ +class C { + static #y = "y"; + static #z = "self"; + static #x; + static b = "b"; + static self = C; + static #self = C; + static { + let cloned, b, y, yy, yy2; + ({ #x: { [C.#z]: { b, #x: y = (C.b = "bb", C.#self.#y) }, #x: yy = (delete C.self, { ...cloned } = C, C.#y = "yy") } = C.#self, #y: yy2 } = C); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/output.js new file mode 100644 index 000000000000..6ddd0084d98b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested/output.js @@ -0,0 +1,17 @@ +class C { + static #y = "y"; + static #z = "self"; + static #x; + static b = "b"; + static self = C; + static #self = C; + static { + var _m, _m2, _m3, _m4, _m5; + + let cloned, b, y, yy, yy2; + _m = C.#x, _m2 = _m === void 0 ? C.#self : _m, _m3 = _m2[C.#z], ({ + b + } = _m3), _m4 = _m3.#x, y = _m4 === void 0 ? (C.b = "bb", C.#self.#y) : _m4, _m5 = _m2.#x, yy = _m5 === void 0 ? (delete C.self, ({ ...cloned + } = C), C.#y = "yy") : _m5, yy2 = C.#y; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/exec.js new file mode 100644 index 000000000000..fcfcf3bafcd5 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/exec.js @@ -0,0 +1,16 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + let x, y, z; + ({ #x: x, #y: y, ...z } = C); + result = { x, y, z }; + } +} +expect(result).toStrictEqual({ + x: "#x", y: "#y", z: { a: "a", b: "b", c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/input.js new file mode 100644 index 000000000000..f29b28b5a652 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/input.js @@ -0,0 +1,13 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + let x, y, z; + ({ #x: x, #y: y, ...z } = C); + result = { x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/output.js new file mode 100644 index 000000000000..9cbe29ab2cce --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-private-keys/output.js @@ -0,0 +1,19 @@ +let result; + +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + let x, y, z; + x = C.#x, y = C.#y, ({ ...z + } = C); + result = { + x, + y, + z + }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/exec.js new file mode 100644 index 000000000000..6dff97a97aff --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/exec.js @@ -0,0 +1,13 @@ +let result; +class C { + static x = "x"; + static y = "y"; + static z = "z"; + static #x = C; + static { + var x, y, z; + ({ x, #x: { y, ...z } } = C); + result = { x, y, z }; + } +} +expect(result).toStrictEqual({ x: "x", y: "y", z: { x: "x", z: "z" } }) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/input.js new file mode 100644 index 000000000000..d67ebdde0914 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/input.js @@ -0,0 +1,12 @@ +let result; +class C { + static x = "x"; + static y = "y"; + static z = "z"; + static #x = C; + static { + var x, y, z; + ({ x, #x: { y, ...z } } = C); + result = { x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/output.js new file mode 100644 index 000000000000..a31104ad6357 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-under-private/output.js @@ -0,0 +1,22 @@ +let result; + +class C { + static x = "x"; + static y = "y"; + static z = "z"; + static #x = C; + static { + var x, y, z; + ({ + x + } = C), ({ + y, + ...z + } = C.#x); + result = { + x, + y, + z + }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/exec.js new file mode 100644 index 000000000000..f03d0735bead --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/exec.js @@ -0,0 +1,16 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var a, b, x, y, z; + ({ [C.a]: a, #x: x, [C.b]: b, #y: y, ...z } = C); + result = { a, b, x, y, z }; + } +} +expect(result).toStrictEqual({ + a: "a", b: "b", x: "#x", y: "#y", z: { c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/input.js new file mode 100644 index 000000000000..231b2f804345 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/input.js @@ -0,0 +1,13 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var a, b, x, y, z; + ({ [C.a]: a, #x: x, [C.b]: b, #y: y, ...z } = C); + result = { a, b, x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/output.js new file mode 100644 index 000000000000..10ca5e47a965 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest/output.js @@ -0,0 +1,26 @@ +let result; + +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var _m, _m2; + + var a, b, x, y, z; + ({ + [_m = C.a]: a + } = C), x = C.#x, ({ + [_m2 = C.b]: b + } = C), y = C.#y, z = babelHelpers.objectWithoutProperties(C, [_m, _m2].map(babelHelpers.toPropertyKey)); + result = { + a, + b, + x, + y, + z + }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/options.json new file mode 100644 index 000000000000..b671b8bdab63 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-destructuring-private"], + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/exec.js new file mode 100644 index 000000000000..387b1623a8f9 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/exec.js @@ -0,0 +1,11 @@ +let a,b; +class C { + static #x = { a: 1, b: 2 }; + static { + (function f(r = { #x: { b }} = C) {})() + } + static m(r = { #x: { a } } = C) {} +} +C.m(); +expect(a).toBe(1); +expect(b).toBe(2); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/input.js new file mode 100644 index 000000000000..cd50bcd759bf --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/input.js @@ -0,0 +1,10 @@ +let a; +class C { + static #x = { a: 1, b: 2 }; + static { + let b; + (function f(r = { #x: { b }} = C) {})() + } + static m(r = { #x: { a } } = C) {} +} +C.m(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/output.js new file mode 100644 index 000000000000..21c71cdb74c7 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/output.js @@ -0,0 +1,27 @@ +var _m2; + +let a; + +class C { + static #x = { + a: 1, + b: 2 + }; + static { + var _m; + + let b; + + (function f(r = (_m = C, ({ + b + } = C.#x), _m)) {})(); + + } + + static m(r = (_m2 = C, ({ + a + } = C.#x), _m2)) {} + +} + +C.m(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/options.json new file mode 100644 index 000000000000..613db701c964 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/options.json @@ -0,0 +1,12 @@ +{ + "assumptions": { + "objectRestNoSymbols": true + }, + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties", + "proposal-private-methods", + ["proposal-object-rest-spread", { "useBuiltIns": true }] + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/exec.js new file mode 100644 index 000000000000..53ee397c5820 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/exec.js @@ -0,0 +1,15 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { [C.a]: a, #x: x, [C.b]: b, #y: y, ...z } = C; + result = { a, b, x, y, z }; + } +} +expect(result).toStrictEqual({ + a: "a", b: "b", x: "#x", y: "#y", z: { c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/input.js new file mode 100644 index 000000000000..bffc7a5ee2b4 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/input.js @@ -0,0 +1,12 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { [C.a]: a, #x: x, [C.b]: b, #y: y, ...z } = C; + result = { a, b, x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/output.js new file mode 100644 index 000000000000..f525646493a3 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-objectRestNoSymbols/variable-declaration/output.js @@ -0,0 +1,36 @@ +let result; + +class C {} + +var _x = { + writable: true, + value: "#x" +}; +var _y = { + writable: true, + value: "#y" +}; +babelHelpers.defineProperty(C, "a", "a"); +babelHelpers.defineProperty(C, "b", "b"); +babelHelpers.defineProperty(C, "c", "c"); + +(() => { + var _m, _m2; + + var { + [_m = C.a]: a + } = C, + x = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), + { + [_m2 = C.b]: b + } = C, + y = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _y), + z = babelHelpers.objectWithoutPropertiesLoose(C, [_m, _m2].map(babelHelpers.toPropertyKey)); + result = { + a, + b, + x, + y, + z + }; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/input.js new file mode 100644 index 000000000000..e2ac65996b95 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/input.js @@ -0,0 +1,7 @@ +class C { + #x; + static { + try { throw new C() } catch ({ #x: x }) { + } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/options.json new file mode 100644 index 000000000000..8d357aef7402 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties" + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/output.js new file mode 100644 index 000000000000..23c3b28e7821 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/output.js @@ -0,0 +1,19 @@ +var _x = /*#__PURE__*/new WeakMap(); + +class C { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _x, { + writable: true, + value: void 0 + }); + } + +} + +(() => { + try { + throw new C(); + } catch (_e) { + var x = babelHelpers.classPrivateFieldGet(_e, _x); + } +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/input.js new file mode 100644 index 000000000000..e2ac65996b95 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/input.js @@ -0,0 +1,7 @@ +class C { + #x; + static { + try { throw new C() } catch ({ #x: x }) { + } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/options.json new file mode 100644 index 000000000000..b671b8bdab63 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-destructuring-private"], + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/output.js new file mode 100644 index 000000000000..a6e307b2cae2 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/output.js @@ -0,0 +1,11 @@ +class C { + #x; + static { + try { + throw new C(); + } catch (_e) { + var x = _e.#x; + } + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/exec.js new file mode 100644 index 000000000000..a5758b3f342f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/exec.js @@ -0,0 +1,12 @@ +let result; +class C { + static #x = 42; + static { + let x, y; + for ({ #x: x } = { #x: y } = C;;) { + result = { x, y }; + break; + } + } +} +expect(result).toStrictEqual({ x: 42, y: 42}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/input.js new file mode 100644 index 000000000000..82b14c5452f3 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/input.js @@ -0,0 +1,7 @@ +class C { + static #x = 42; + static { + let x, y; + for ({ #x: x } = { #x: y } = C;;) { break; } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/output.js new file mode 100644 index 000000000000..a1dbc5da395e --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/output.js @@ -0,0 +1,16 @@ +class C {} + +var _x = { + writable: true, + value: 42 +}; + +(() => { + let x, y; + + for (_m = (_m2 = C, y = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), _m2), x = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x), _m;;) { + var _m, _m2; + + break; + } +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs/input.js new file mode 100644 index 000000000000..435022d57a6d --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs/input.js @@ -0,0 +1,6 @@ +class C { + #x; + static { + for ({ #x: x } = this;;) { break; } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs/output.js new file mode 100644 index 000000000000..48d4b0febd9b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs/output.js @@ -0,0 +1,19 @@ +var _x = /*#__PURE__*/new WeakMap(); + +class C { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _x, { + writable: true, + value: void 0 + }); + } + +} + +(() => { + for (_m = C, x = babelHelpers.classPrivateFieldGet(C, _x), _m;;) { + var _m; + + break; + } +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/options.json new file mode 100644 index 000000000000..8d357aef7402 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties" + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/exec.js new file mode 100644 index 000000000000..50c718f481c0 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/exec.js @@ -0,0 +1,12 @@ +let result; +class C { + static #x = 42; + static { + let y; + for (let { #x: x } = { #x: y } = C;;) { + result = { x, y }; + break + }; + } +} +expect(result).toStrictEqual({ x: 42, y: 42}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/input.js new file mode 100644 index 000000000000..0b30c4deb3d2 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/input.js @@ -0,0 +1,9 @@ +class C { + static #x = 42; + static { + let y; + for (let { #x: x } = { #x: y } = C;;) { + break + }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/output.js new file mode 100644 index 000000000000..bef362866c61 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/output.js @@ -0,0 +1,18 @@ +class C {} + +var _x = { + writable: true, + value: 42 +}; + +(() => { + let y; + + for (let _m = (_m2 = C, y = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), _m2), x = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x);;) { + var _m2; + + break; + } + + ; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration/input.js new file mode 100644 index 000000000000..d9b6b142e217 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration/input.js @@ -0,0 +1,6 @@ +class C { + static #x; + static { + for (let { #x: x } = C;;) { break; } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration/output.js new file mode 100644 index 000000000000..b24ba926176f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration/output.js @@ -0,0 +1,12 @@ +class C {} + +var _x = { + writable: true, + value: void 0 +}; + +(() => { + for (let x = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x);;) { + break; + } +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/exec.js new file mode 100644 index 000000000000..a5758b3f342f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/exec.js @@ -0,0 +1,12 @@ +let result; +class C { + static #x = 42; + static { + let x, y; + for ({ #x: x } = { #x: y } = C;;) { + result = { x, y }; + break; + } + } +} +expect(result).toStrictEqual({ x: 42, y: 42}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/input.js new file mode 100644 index 000000000000..82b14c5452f3 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/input.js @@ -0,0 +1,7 @@ +class C { + static #x = 42; + static { + let x, y; + for ({ #x: x } = { #x: y } = C;;) { break; } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/output.js new file mode 100644 index 000000000000..e52600816752 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/output.js @@ -0,0 +1,13 @@ +class C { + static #x = 42; + static { + let x, y; + + for (_m = (_m2 = C, y = C.#x, _m2), x = _m.#x, _m;;) { + var _m, _m2; + + break; + } + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs/input.js new file mode 100644 index 000000000000..435022d57a6d --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs/input.js @@ -0,0 +1,6 @@ +class C { + #x; + static { + for ({ #x: x } = this;;) { break; } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs/output.js new file mode 100644 index 000000000000..e4f394a6488b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs/output.js @@ -0,0 +1,11 @@ +class C { + #x; + static { + for (_m = this, x = this.#x, _m;;) { + var _m; + + break; + } + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/options.json new file mode 100644 index 000000000000..b671b8bdab63 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-destructuring-private"], + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/exec.js new file mode 100644 index 000000000000..50c718f481c0 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/exec.js @@ -0,0 +1,12 @@ +let result; +class C { + static #x = 42; + static { + let y; + for (let { #x: x } = { #x: y } = C;;) { + result = { x, y }; + break + }; + } +} +expect(result).toStrictEqual({ x: 42, y: 42}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/input.js new file mode 100644 index 000000000000..0b30c4deb3d2 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/input.js @@ -0,0 +1,9 @@ +class C { + static #x = 42; + static { + let y; + for (let { #x: x } = { #x: y } = C;;) { + break + }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/output.js new file mode 100644 index 000000000000..29bb33212367 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/output.js @@ -0,0 +1,14 @@ +class C { + static #x = 42; + static { + let y; + + for (let _m = (_m2 = C, y = C.#x, _m2), x = _m.#x;;) { + var _m2; + + break; + } + + ; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration/input.js new file mode 100644 index 000000000000..d9b6b142e217 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration/input.js @@ -0,0 +1,6 @@ +class C { + static #x; + static { + for (let { #x: x } = C;;) { break; } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration/output.js new file mode 100644 index 000000000000..99b21d9250da --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration/output.js @@ -0,0 +1,9 @@ +class C { + static #x; + static { + for (let x = C.#x;;) { + break; + } + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs/input.js new file mode 100644 index 000000000000..f02105a5d715 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs/input.js @@ -0,0 +1,6 @@ +class C { + #x; + static { + for ({ #x: x } of [this]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs/output.js new file mode 100644 index 000000000000..356869f3e61f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs/output.js @@ -0,0 +1,18 @@ +var _x = /*#__PURE__*/new WeakMap(); + +class C { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _x, { + writable: true, + value: void 0 + }); + } + +} + +(() => { + for (const _ref of [C]) { + x = babelHelpers.classPrivateFieldGet(_ref, _x); + ; + } +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/options.json new file mode 100644 index 000000000000..8d357aef7402 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties" + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration/input.js new file mode 100644 index 000000000000..96346f23285b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration/input.js @@ -0,0 +1,6 @@ +class C { + #x; + static { + for (const { #x: x } of [this]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration/output.js new file mode 100644 index 000000000000..b44bf3a082ec --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration/output.js @@ -0,0 +1,18 @@ +var _x = /*#__PURE__*/new WeakMap(); + +class C { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _x, { + writable: true, + value: void 0 + }); + } + +} + +(() => { + for (const _ref of [C]) { + const x = babelHelpers.classPrivateFieldGet(_ref, _x); + ; + } +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs/input.js new file mode 100644 index 000000000000..f02105a5d715 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs/input.js @@ -0,0 +1,6 @@ +class C { + #x; + static { + for ({ #x: x } of [this]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs/output.js new file mode 100644 index 000000000000..abd59a8e704d --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs/output.js @@ -0,0 +1,10 @@ +class C { + #x; + static { + for (const _ref of [this]) { + x = _ref.#x; + ; + } + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/options.json new file mode 100644 index 000000000000..b671b8bdab63 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-destructuring-private"], + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration/input.js new file mode 100644 index 000000000000..96346f23285b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration/input.js @@ -0,0 +1,6 @@ +class C { + #x; + static { + for (const { #x: x } of [this]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration/output.js new file mode 100644 index 000000000000..236bc19524d2 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration/output.js @@ -0,0 +1,10 @@ +class C { + #x; + static { + for (const _ref of [this]) { + const x = _ref.#x; + ; + } + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/function-length/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/function-length/exec.js new file mode 100644 index 000000000000..6ad25f261e99 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/function-length/exec.js @@ -0,0 +1,5 @@ +class C { + #x; + static m(a, { #x: x }, ...b) {} +} +expect(C.m.length).toBe(2) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/input.js new file mode 100644 index 000000000000..90f84a73805f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/input.js @@ -0,0 +1,6 @@ +class C { + #x; + m(a, { #x: x }, ...b) { + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/output.js new file mode 100644 index 000000000000..2b2fd2073cdf --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/output.js @@ -0,0 +1,17 @@ +var _x = /*#__PURE__*/new WeakMap(); + +class C { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _x, { + writable: true, + value: void 0 + }); + } + + m(_p, _p2, ..._p3) { + var a = _p, + x = babelHelpers.classPrivateFieldGet(_p2, _x), + b = _p3; + } + +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/options.json new file mode 100644 index 000000000000..8d357aef7402 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties" + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/input.js new file mode 100644 index 000000000000..4c877a3d1481 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/input.js @@ -0,0 +1,6 @@ +class C { + #x; + m(a, { #x: x }, ...b) { + var a = 1; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/output.js new file mode 100644 index 000000000000..1ed0c90d22d3 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/output.js @@ -0,0 +1,20 @@ +var _x = /*#__PURE__*/new WeakMap(); + +class C { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _x, { + writable: true, + value: void 0 + }); + } + + m(_p, _p2, ..._p3) { + var a = _p, + x = babelHelpers.classPrivateFieldGet(_p2, _x), + b = _p3; + return function (a) { + var a = 1; + }(a); + } + +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/exec.js new file mode 100644 index 000000000000..6ad25f261e99 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/exec.js @@ -0,0 +1,5 @@ +class C { + #x; + static m(a, { #x: x }, ...b) {} +} +expect(C.m.length).toBe(2) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/input.js new file mode 100644 index 000000000000..90f84a73805f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/input.js @@ -0,0 +1,6 @@ +class C { + #x; + m(a, { #x: x }, ...b) { + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/output.js new file mode 100644 index 000000000000..b110d694a6fc --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/output.js @@ -0,0 +1,10 @@ +class C { + #x; + + m(_p, _p2, ..._p3) { + var a = _p, + x = _p2.#x, + b = _p3; + } + +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/options.json new file mode 100644 index 000000000000..b671b8bdab63 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-destructuring-private"], + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/input.js new file mode 100644 index 000000000000..4c877a3d1481 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/input.js @@ -0,0 +1,6 @@ +class C { + #x; + m(a, { #x: x }, ...b) { + var a = 1; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/output.js new file mode 100644 index 000000000000..ced7c933c0c4 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/output.js @@ -0,0 +1,13 @@ +class C { + #x; + + m(_p, _p2, ..._p3) { + var a = _p, + x = _p2.#x, + b = _p3; + return function (a) { + var a = 1; + }(a); + } + +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern-with-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern-with-rest/exec.js new file mode 100644 index 000000000000..42b2f89bd467 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern-with-rest/exec.js @@ -0,0 +1,18 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + static #x; + static #y; + static { + var [{ [push(0)]: a = push(1), #x: { + [push(3)]: b = push(4), + #y: y = push(5), + [push(6)]: c = push(7) + } = push(2, C), [push(8)]: d = push(9) }, ...{ [push(10)]: e = push(11), ...f }] = [C]; + } +} + +var nums = Array.from({ length: 12 }, (_, i) => i); +expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern/exec.js new file mode 100644 index 000000000000..280a7a1a424e --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern/exec.js @@ -0,0 +1,18 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + static #x; + static #y; + static { + var [{ [push(0)]: a = push(1), #x: { + [push(3)]: b = push(4), + #y: y = push(5), + [push(6)]: c = push(7) + } = push(2, C), [push(8)]: d = push(9) }, e = push(10)] = [C]; + } +} + +var nums = Array.from({ length: 11 }, (_, i) => i); +expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern-with-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern-with-rest/exec.js new file mode 100644 index 000000000000..46e9cfe5b3f6 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern-with-rest/exec.js @@ -0,0 +1,21 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + static #x; + static #y; + static { + var { [push(0)]: a = push(1), #x: { + [push(3)]: b = push(4), + #y: y = push(5), + [push(6)]: c = push(7), + #x: x = push(8), + [push(9)]: d = push(10), + ...e + } = push(2, C), [push(11)]: d = push(12), #y: z = push(13), ...f } = C; + } +} + +var nums = Array.from({ length: 14 }, (_, i) => i); +expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern/exec.js new file mode 100644 index 000000000000..bf9aedfe8ca0 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern/exec.js @@ -0,0 +1,18 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + static #x; + static #y; + static { + var { [push(0)]: a = push(1), #x: { + [push(3)]: b = push(4), + #y: y = push(5), + [push(6)]: c = push(7) + } = push(2, C), [push(8)]: d = push(9) } = C; + } +} + +var nums = Array.from({ length: 10 }, (_, i) => i); +expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/options.json new file mode 100644 index 000000000000..8099cdd9d774 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties", + ["proposal-object-rest-spread", { "useBuiltIns": true }] + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern-with-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern-with-rest/exec.js new file mode 100644 index 000000000000..42b2f89bd467 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern-with-rest/exec.js @@ -0,0 +1,18 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + static #x; + static #y; + static { + var [{ [push(0)]: a = push(1), #x: { + [push(3)]: b = push(4), + #y: y = push(5), + [push(6)]: c = push(7) + } = push(2, C), [push(8)]: d = push(9) }, ...{ [push(10)]: e = push(11), ...f }] = [C]; + } +} + +var nums = Array.from({ length: 12 }, (_, i) => i); +expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern/exec.js new file mode 100644 index 000000000000..280a7a1a424e --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern/exec.js @@ -0,0 +1,18 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + static #x; + static #y; + static { + var [{ [push(0)]: a = push(1), #x: { + [push(3)]: b = push(4), + #y: y = push(5), + [push(6)]: c = push(7) + } = push(2, C), [push(8)]: d = push(9) }, e = push(10)] = [C]; + } +} + +var nums = Array.from({ length: 11 }, (_, i) => i); +expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern-with-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern-with-rest/exec.js new file mode 100644 index 000000000000..46e9cfe5b3f6 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern-with-rest/exec.js @@ -0,0 +1,21 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + static #x; + static #y; + static { + var { [push(0)]: a = push(1), #x: { + [push(3)]: b = push(4), + #y: y = push(5), + [push(6)]: c = push(7), + #x: x = push(8), + [push(9)]: d = push(10), + ...e + } = push(2, C), [push(11)]: d = push(12), #y: z = push(13), ...f } = C; + } +} + +var nums = Array.from({ length: 14 }, (_, i) => i); +expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern/exec.js new file mode 100644 index 000000000000..bf9aedfe8ca0 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern/exec.js @@ -0,0 +1,18 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + static #x; + static #y; + static { + var { [push(0)]: a = push(1), #x: { + [push(3)]: b = push(4), + #y: y = push(5), + [push(6)]: c = push(7) + } = push(2, C), [push(8)]: d = push(9) } = C; + } +} + +var nums = Array.from({ length: 10 }, (_, i) => i); +expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/options.json new file mode 100644 index 000000000000..b671b8bdab63 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-destructuring-private"], + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/.valid-preset-typescript/input.ts b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/.valid-preset-typescript/input.ts new file mode 100644 index 000000000000..e108eb0a6b7e --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/.valid-preset-typescript/input.ts @@ -0,0 +1,4 @@ +class C { + #x; + constructor(public foo, { #x: x}) {} +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/.valid-preset-typescript/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/.valid-preset-typescript/options.json new file mode 100644 index 000000000000..901cfe6b50f9 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/.valid-preset-typescript/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-destructuring-private"], + "presets": ["typescript"] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/.valid-preset-typescript/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/.valid-preset-typescript/output.js new file mode 100644 index 000000000000..67bc13190763 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/.valid-preset-typescript/output.js @@ -0,0 +1,10 @@ +class C { + #x; + + constructor(_p, _p2) { + var foo = _p, + x = _p2.#x; + this.foo = foo; + } + +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/invalid-after-destructuring-private/input.ts b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/invalid-after-destructuring-private/input.ts new file mode 100644 index 000000000000..e108eb0a6b7e --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/invalid-after-destructuring-private/input.ts @@ -0,0 +1,4 @@ +class C { + #x; + constructor(public foo, { #x: x}) {} +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/invalid-after-destructuring-private/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/invalid-after-destructuring-private/options.json new file mode 100644 index 000000000000..2e503cc0a75f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/invalid-after-destructuring-private/options.json @@ -0,0 +1,6 @@ +{ + "plugins": ["proposal-destructuring-private", "transform-typescript"], + "throws": [ + "TypeScript parameter properties must first be transformed by @babel/plugin-transform-typescript." + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/input.ts b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/input.ts new file mode 100644 index 000000000000..e108eb0a6b7e --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/input.ts @@ -0,0 +1,4 @@ +class C { + #x; + constructor(public foo, { #x: x}) {} +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/options.json new file mode 100644 index 000000000000..21e1abbf8a7b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["transform-typescript", "proposal-destructuring-private"] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/output.js new file mode 100644 index 000000000000..67bc13190763 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/output.js @@ -0,0 +1,10 @@ +class C { + #x; + + constructor(_p, _p2) { + var foo = _p, + x = _p2.#x; + this.foo = foo; + } + +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/exec.js new file mode 100644 index 000000000000..216a27c621bf --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/exec.js @@ -0,0 +1,9 @@ +let result; +class C { + static #x; + static { + var [{ #x: x = 1 }, ...z] = [C]; + result = { x, z }; + } +} +expect(result).toStrictEqual({ x: 1, z: [] }); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/input.js new file mode 100644 index 000000000000..7f0e8f3446f3 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/input.js @@ -0,0 +1,6 @@ +class C { + static #x; + static { + var [{ #x: x = 1 }, ...z] = [C]; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/output.js new file mode 100644 index 000000000000..8e4a9f07f94e --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/output.js @@ -0,0 +1,14 @@ +class C {} + +var _x = { + writable: true, + value: void 0 +}; + +(() => { + var _m = [C], + [_p, ..._p2] = _m, + _m2 = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x), + x = _m2 === void 0 ? 1 : _m2, + z = _p2; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/exec.js new file mode 100644 index 000000000000..cbee2112f773 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/exec.js @@ -0,0 +1,9 @@ +let result; +class C { + static #x; + static { + var { a = 1, #x: x = 2, b = 3 } = C; + result = { a, x, b }; + } +} +expect(result).toStrictEqual({ a: 1, x: 2, b: 3 }); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/input.js new file mode 100644 index 000000000000..0ed7639bb2c8 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/input.js @@ -0,0 +1,6 @@ +class C { + static #x; + static { + var { a = 1, #x: x = 2, b = 3 } = C; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/output.js new file mode 100644 index 000000000000..d0b62f6685cb --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/basic/output.js @@ -0,0 +1,17 @@ +class C {} + +var _x = { + writable: true, + value: void 0 +}; + +(() => { + var { + a = 1 + } = C, + _m = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), + x = _m === void 0 ? 2 : _m, + { + b = 3 + } = C; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/exec.js new file mode 100644 index 000000000000..026ddd6c82a8 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/exec.js @@ -0,0 +1,13 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + static #self() { return C } + static { + var [{ #x: x } = C.self, { #y: [,{ #z: y = C.#self() } = C.self ] },,z = y.#y] = [this,this]; + expect(x).toBe("x"); + expect(y).toBe(C); + expect(z).toStrictEqual([]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/input.js new file mode 100644 index 000000000000..7570c494dd96 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/input.js @@ -0,0 +1,10 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + static #self() { return C } + static { + var [{ #x: x } = C.self, { #y: [,{ #z: y = C.#self() } = C.self ] },,z = y.#y] = [this,this]; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/output.js new file mode 100644 index 000000000000..0f7f7a4f8f6b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/output.js @@ -0,0 +1,32 @@ +class C {} + +function _self() { + return C; +} + +var _x = { + writable: true, + value: "x" +}; +var _y = { + writable: true, + value: [] +}; +var _z = { + writable: true, + value: void 0 +}; +babelHelpers.defineProperty(C, "self", C); + +(() => { + var _m = [C, C], + [_p, _p2,, _p3] = _m, + _m2 = _p === void 0 ? C.self : _p, + x = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _x), + _m3 = babelHelpers.classStaticPrivateFieldSpecGet(_p2, C, _y), + [, _p4] = _m3, + _m4 = _p4 === void 0 ? C.self : _p4, + _m5 = babelHelpers.classStaticPrivateFieldSpecGet(_m4, C, _z), + y = _m5 === void 0 ? babelHelpers.classStaticPrivateMethodGet(C, C, _self).call(C) : _m5, + z = _p3 === void 0 ? babelHelpers.classStaticPrivateFieldSpecGet(y, C, _y) : _p3; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/exec.js new file mode 100644 index 000000000000..504cee587e65 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/exec.js @@ -0,0 +1,21 @@ +let result; +class C { + static #y = "y"; + static #z = "self"; + static #x; + static b = "b"; + static self = C; + static #self = C; + static { + let cloned; + var { #x: { [C.#z]: { b, #x: y = (C.b = "bb", C.#self.#y) }, #x: yy = (delete C.self, { ...cloned } = C, C.#y = "yy") } = C.#self, #y: yy2 } = C; + result = { b, y, yy, cloned, yy2 }; + } +} +expect(result).toStrictEqual({ + b: "b", + y: "y", + yy: "yy", + cloned: { b: "bb" }, + yy2: "yy" +}) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/input.js new file mode 100644 index 000000000000..50fe617b26cc --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/input.js @@ -0,0 +1,12 @@ +class C { + static #y = "y"; + static #z = "self"; + static #x; + static b = "b"; + static self = C; + static #self = C; + static { + let cloned; + var { #x: { [C.#z]: { b, #x: y = (C.b = "bb", C.#self.#y) }, #x: yy = (delete C.self, { ...cloned } = C, C.#y = "yy") } = C.#self, #y: yy2 } = C; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/options.json new file mode 100644 index 000000000000..48eb48aad7d6 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/options.json @@ -0,0 +1,9 @@ +{ + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties", + "proposal-private-methods" + ], + "minNodeVersion": "8.0.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/output.js new file mode 100644 index 000000000000..77a8a9a3bbcc --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested/output.js @@ -0,0 +1,37 @@ +class C {} + +var _y = { + writable: true, + value: "y" +}; +var _z = { + writable: true, + value: "self" +}; +var _x = { + writable: true, + value: void 0 +}; +babelHelpers.defineProperty(C, "b", "b"); +babelHelpers.defineProperty(C, "self", C); +var _self = { + writable: true, + value: C +}; + +(() => { + let cloned; + + var _m = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), + _m2 = _m === void 0 ? babelHelpers.classStaticPrivateFieldSpecGet(C, C, _self) : _m, + _m3 = _m2[babelHelpers.classStaticPrivateFieldSpecGet(C, C, _z)], + { + b + } = _m3, + _m4 = babelHelpers.classStaticPrivateFieldSpecGet(_m3, C, _x), + y = _m4 === void 0 ? (C.b = "bb", babelHelpers.classStaticPrivateFieldSpecGet(babelHelpers.classStaticPrivateFieldSpecGet(C, C, _self), C, _y)) : _m4, + _m5 = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _x), + yy = _m5 === void 0 ? (delete C.self, ({ ...cloned + } = C), babelHelpers.classStaticPrivateFieldSpecSet(C, C, _y, "yy")) : _m5, + yy2 = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _y); +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/input.js new file mode 100644 index 000000000000..fc5b7e36b72c --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/input.js @@ -0,0 +1,6 @@ +class C { + static #x; + static { + var { "0": { #x: w }, 1: { #x: x }, 2n: {#x: y}, 3m: {#x: z} } = [C, C, C, C]; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/options.json new file mode 100644 index 000000000000..5e85d3896305 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-destructuring-private", "syntax-decimal"], + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/output.js new file mode 100644 index 000000000000..5c6376193ae1 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/output.js @@ -0,0 +1,14 @@ +class C { + static #x; + static { + var _m = [C, C, C, C], + _m2 = _m["0"], + w = _m2.#x, + _m3 = _m[1], + x = _m3.#x, + _m4 = _m[2n], + y = _m4.#x, + _m5 = _m[3m], + z = _m5.#x; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/exec.js new file mode 100644 index 000000000000..109bcfba54aa --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/exec.js @@ -0,0 +1,15 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { #x: x, #y: y, ...z } = C; + result = { x, y, z }; + } +} +expect(result).toStrictEqual({ + x: "#x", y: "#y", z: { a: "a", b: "b", c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/input.js new file mode 100644 index 000000000000..30e7783da1b1 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/input.js @@ -0,0 +1,12 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { #x: x, #y: y, ...z } = C; + result = { x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/output.js new file mode 100644 index 000000000000..a4cb5e90ca77 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-private-keys/output.js @@ -0,0 +1,26 @@ +let result; + +class C {} + +var _x = { + writable: true, + value: "#x" +}; +var _y = { + writable: true, + value: "#y" +}; +babelHelpers.defineProperty(C, "a", "a"); +babelHelpers.defineProperty(C, "b", "b"); +babelHelpers.defineProperty(C, "c", "c"); + +(() => { + var x = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), + y = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _y), + z = Object.assign({}, C); + result = { + x, + y, + z + }; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/exec.js new file mode 100644 index 000000000000..57bd774a7c43 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/exec.js @@ -0,0 +1,12 @@ +let result; +class C { + static x = "x"; + static y = "y"; + static z = "z"; + static #x = C; + static { + var { x, #x: { y, ...z } } = C; + result = { x, y, z }; + } +} +expect(result).toStrictEqual({ x: "x", y: "y", z: { x: "x", z: "z" } }) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/input.js new file mode 100644 index 000000000000..abbe3dd5d8f5 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/input.js @@ -0,0 +1,11 @@ +let result; +class C { + static x = "x"; + static y = "y"; + static z = "z"; + static #x = C; + static { + var { x, #x: { y, ...z } } = C; + result = { x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/output.js new file mode 100644 index 000000000000..c0c57671bfbd --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-under-private/output.js @@ -0,0 +1,29 @@ +const _excluded = ["y"]; +let result; + +class C {} + +babelHelpers.defineProperty(C, "x", "x"); +babelHelpers.defineProperty(C, "y", "y"); +babelHelpers.defineProperty(C, "z", "z"); +var _x = { + writable: true, + value: C +}; + +(() => { + var { + x + } = C, + _babelHelpers$classSt = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), + { + y + } = _babelHelpers$classSt, + z = babelHelpers.objectWithoutProperties(_babelHelpers$classSt, _excluded); + + result = { + x, + y, + z + }; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/exec.js new file mode 100644 index 000000000000..53ee397c5820 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/exec.js @@ -0,0 +1,15 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { [C.a]: a, #x: x, [C.b]: b, #y: y, ...z } = C; + result = { a, b, x, y, z }; + } +} +expect(result).toStrictEqual({ + a: "a", b: "b", x: "#x", y: "#y", z: { c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/input.js new file mode 100644 index 000000000000..bffc7a5ee2b4 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/input.js @@ -0,0 +1,12 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { [C.a]: a, #x: x, [C.b]: b, #y: y, ...z } = C; + result = { a, b, x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/options.json new file mode 100644 index 000000000000..82bccc4aeed5 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties", + "proposal-private-methods" + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/output.js new file mode 100644 index 000000000000..bcacc784b73f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest/output.js @@ -0,0 +1,36 @@ +let result; + +class C {} + +var _x = { + writable: true, + value: "#x" +}; +var _y = { + writable: true, + value: "#y" +}; +babelHelpers.defineProperty(C, "a", "a"); +babelHelpers.defineProperty(C, "b", "b"); +babelHelpers.defineProperty(C, "c", "c"); + +(() => { + var _m, _m2; + + var { + [_m = C.a]: a + } = C, + x = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), + { + [_m2 = C.b]: b + } = C, + y = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _y), + z = babelHelpers.objectWithoutProperties(C, [_m, _m2].map(babelHelpers.toPropertyKey)); + result = { + a, + b, + x, + y, + z + }; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/options.json new file mode 100644 index 000000000000..e1c5858604ed --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/options.json @@ -0,0 +1,9 @@ +{ + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties", + "proposal-private-methods", + ["proposal-object-rest-spread", { "useBuiltIns": true }] + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/exec.js new file mode 100644 index 000000000000..216a27c621bf --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/exec.js @@ -0,0 +1,9 @@ +let result; +class C { + static #x; + static { + var [{ #x: x = 1 }, ...z] = [C]; + result = { x, z }; + } +} +expect(result).toStrictEqual({ x: 1, z: [] }); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/input.js new file mode 100644 index 000000000000..7f0e8f3446f3 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/input.js @@ -0,0 +1,6 @@ +class C { + static #x; + static { + var [{ #x: x = 1 }, ...z] = [C]; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/output.js new file mode 100644 index 000000000000..9c642bfc8702 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/output.js @@ -0,0 +1,10 @@ +class C { + static #x; + static { + var _m = [C], + [_p, ..._p2] = _m, + _m2 = _p.#x, + x = _m2 === void 0 ? 1 : _m2, + z = _p2; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/exec.js new file mode 100644 index 000000000000..cbee2112f773 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/exec.js @@ -0,0 +1,9 @@ +let result; +class C { + static #x; + static { + var { a = 1, #x: x = 2, b = 3 } = C; + result = { a, x, b }; + } +} +expect(result).toStrictEqual({ a: 1, x: 2, b: 3 }); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/input.js new file mode 100644 index 000000000000..0ed7639bb2c8 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/input.js @@ -0,0 +1,6 @@ +class C { + static #x; + static { + var { a = 1, #x: x = 2, b = 3 } = C; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/output.js new file mode 100644 index 000000000000..768092799091 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/basic/output.js @@ -0,0 +1,13 @@ +class C { + static #x; + static { + var { + a = 1 + } = C, + _m = C.#x, + x = _m === void 0 ? 2 : _m, + { + b = 3 + } = C; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/exec.js new file mode 100644 index 000000000000..026ddd6c82a8 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/exec.js @@ -0,0 +1,13 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + static #self() { return C } + static { + var [{ #x: x } = C.self, { #y: [,{ #z: y = C.#self() } = C.self ] },,z = y.#y] = [this,this]; + expect(x).toBe("x"); + expect(y).toBe(C); + expect(z).toStrictEqual([]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/input.js new file mode 100644 index 000000000000..7570c494dd96 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/input.js @@ -0,0 +1,10 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + static #self() { return C } + static { + var [{ #x: x } = C.self, { #y: [,{ #z: y = C.#self() } = C.self ] },,z = y.#y] = [this,this]; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/output.js new file mode 100644 index 000000000000..f487f9d2b173 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/output.js @@ -0,0 +1,24 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + + static #self() { + return C; + } + + static { + var _m = [this, this], + [_p, _p2,, _p3] = _m, + _m2 = _p === void 0 ? C.self : _p, + x = _m2.#x, + _m3 = _p2.#y, + [, _p4] = _m3, + _m4 = _p4 === void 0 ? C.self : _p4, + _m5 = _m4.#z, + y = _m5 === void 0 ? C.#self() : _m5, + z = _p3 === void 0 ? y.#y : _p3; + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/exec.js new file mode 100644 index 000000000000..504cee587e65 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/exec.js @@ -0,0 +1,21 @@ +let result; +class C { + static #y = "y"; + static #z = "self"; + static #x; + static b = "b"; + static self = C; + static #self = C; + static { + let cloned; + var { #x: { [C.#z]: { b, #x: y = (C.b = "bb", C.#self.#y) }, #x: yy = (delete C.self, { ...cloned } = C, C.#y = "yy") } = C.#self, #y: yy2 } = C; + result = { b, y, yy, cloned, yy2 }; + } +} +expect(result).toStrictEqual({ + b: "b", + y: "y", + yy: "yy", + cloned: { b: "bb" }, + yy2: "yy" +}) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/input.js new file mode 100644 index 000000000000..50fe617b26cc --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/input.js @@ -0,0 +1,12 @@ +class C { + static #y = "y"; + static #z = "self"; + static #x; + static b = "b"; + static self = C; + static #self = C; + static { + let cloned; + var { #x: { [C.#z]: { b, #x: y = (C.b = "bb", C.#self.#y) }, #x: yy = (delete C.self, { ...cloned } = C, C.#y = "yy") } = C.#self, #y: yy2 } = C; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/output.js new file mode 100644 index 000000000000..77f9a4a16296 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested/output.js @@ -0,0 +1,25 @@ +class C { + static #y = "y"; + static #z = "self"; + static #x; + static b = "b"; + static self = C; + static #self = C; + static { + let cloned; + + var _m = C.#x, + _m2 = _m === void 0 ? C.#self : _m, + _m3 = _m2[C.#z], + { + b + } = _m3, + _m4 = _m3.#x, + y = _m4 === void 0 ? (C.b = "bb", C.#self.#y) : _m4, + _m5 = _m2.#x, + yy = _m5 === void 0 ? (delete C.self, ({ ...cloned + } = C), C.#y = "yy") : _m5, + yy2 = C.#y; + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/input.js new file mode 100644 index 000000000000..fc5b7e36b72c --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/input.js @@ -0,0 +1,6 @@ +class C { + static #x; + static { + var { "0": { #x: w }, 1: { #x: x }, 2n: {#x: y}, 3m: {#x: z} } = [C, C, C, C]; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/options.json new file mode 100644 index 000000000000..5e85d3896305 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-destructuring-private", "syntax-decimal"], + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/output.js new file mode 100644 index 000000000000..5c6376193ae1 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/output.js @@ -0,0 +1,14 @@ +class C { + static #x; + static { + var _m = [C, C, C, C], + _m2 = _m["0"], + w = _m2.#x, + _m3 = _m[1], + x = _m3.#x, + _m4 = _m[2n], + y = _m4.#x, + _m5 = _m[3m], + z = _m5.#x; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/exec.js new file mode 100644 index 000000000000..109bcfba54aa --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/exec.js @@ -0,0 +1,15 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { #x: x, #y: y, ...z } = C; + result = { x, y, z }; + } +} +expect(result).toStrictEqual({ + x: "#x", y: "#y", z: { a: "a", b: "b", c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/input.js new file mode 100644 index 000000000000..30e7783da1b1 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/input.js @@ -0,0 +1,12 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { #x: x, #y: y, ...z } = C; + result = { x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/output.js new file mode 100644 index 000000000000..612e5fac9d45 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-private-keys/output.js @@ -0,0 +1,20 @@ +let result; + +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var x = C.#x, + y = C.#y, + { ...z + } = C; + result = { + x, + y, + z + }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/exec.js new file mode 100644 index 000000000000..57bd774a7c43 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/exec.js @@ -0,0 +1,12 @@ +let result; +class C { + static x = "x"; + static y = "y"; + static z = "z"; + static #x = C; + static { + var { x, #x: { y, ...z } } = C; + result = { x, y, z }; + } +} +expect(result).toStrictEqual({ x: "x", y: "y", z: { x: "x", z: "z" } }) diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/input.js new file mode 100644 index 000000000000..abbe3dd5d8f5 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/input.js @@ -0,0 +1,11 @@ +let result; +class C { + static x = "x"; + static y = "y"; + static z = "z"; + static #x = C; + static { + var { x, #x: { y, ...z } } = C; + result = { x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/output.js new file mode 100644 index 000000000000..8f3720bacca2 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-under-private/output.js @@ -0,0 +1,22 @@ +let result; + +class C { + static x = "x"; + static y = "y"; + static z = "z"; + static #x = C; + static { + var { + x + } = C, + { + y, + ...z + } = C.#x; + result = { + x, + y, + z + }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/exec.js new file mode 100644 index 000000000000..53ee397c5820 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/exec.js @@ -0,0 +1,15 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { [C.a]: a, #x: x, [C.b]: b, #y: y, ...z } = C; + result = { a, b, x, y, z }; + } +} +expect(result).toStrictEqual({ + a: "a", b: "b", x: "#x", y: "#y", z: { c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/input.js new file mode 100644 index 000000000000..bffc7a5ee2b4 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/input.js @@ -0,0 +1,12 @@ +let result; +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { [C.a]: a, #x: x, [C.b]: b, #y: y, ...z } = C; + result = { a, b, x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/output.js new file mode 100644 index 000000000000..6a40c624b11c --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest/output.js @@ -0,0 +1,29 @@ +let result; + +class C { + static #x = "#x"; + static #y = "#y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var _m, _m2; + + var { + [_m = C.a]: a + } = C, + x = C.#x, + { + [_m2 = C.b]: b + } = C, + y = C.#y, + z = babelHelpers.objectWithoutProperties(C, [_m, _m2].map(babelHelpers.toPropertyKey)); + result = { + a, + b, + x, + y, + z + }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/options.json new file mode 100644 index 000000000000..b671b8bdab63 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-destructuring-private"], + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/index.js b/packages/babel-plugin-proposal-destructuring-private/test/index.js new file mode 100644 index 000000000000..21a55ce6b5e7 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/index.js @@ -0,0 +1,3 @@ +import runner from "@babel/helper-plugin-test-runner"; + +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/package.json b/packages/babel-plugin-proposal-destructuring-private/test/package.json new file mode 100644 index 000000000000..5ffd9800b97c --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/package.json @@ -0,0 +1 @@ +{ "type": "module" } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/plugin-ordering.js b/packages/babel-plugin-proposal-destructuring-private/test/plugin-ordering.js new file mode 100644 index 000000000000..64ace639375e --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/plugin-ordering.js @@ -0,0 +1,28 @@ +import babel from "@babel/core"; +import proposalDestructuringPrivate from "../lib/index.js"; + +describe("plugin ordering", () => { + it("should work when @babel/plugin-proposal-destructuring-private is after class features plugin", () => { + const source = `class Foo { + static #x = 1; + static { + const {#x: x } = Foo; + }; + } + `; + expect( + () => + babel.transformSync(source, { + filename: "example.js", + highlightCode: false, + configFile: false, + babelrc: false, + plugins: [ + "@babel/plugin-proposal-class-properties", + "@babel/plugin-proposal-class-static-block", + proposalDestructuringPrivate, + ], + }).code, + ).not.toThrow(); + }); +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/util.skip-bundled.js b/packages/babel-plugin-proposal-destructuring-private/test/util.skip-bundled.js new file mode 100644 index 000000000000..5e50a88acb64 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/util.skip-bundled.js @@ -0,0 +1,77 @@ +import babel from "@babel/core"; +import { traversePattern, privateKeyPathIterator } from "../lib/util.js"; +const { isObjectProperty, isPrivateName } = babel.types; + +const { parseSync, traverse } = babel; + +function wrapSourceInClassEnvironment(input) { + const usedPrivateNames = new Set(); + let matched; + const re = /#[\w_]+/g; + while ((matched = re.exec(input)) !== null) { + usedPrivateNames.add(matched[0]); + } + let result = "(class {"; + for (const name of usedPrivateNames) { + result += name + ";"; + } + result += "m(){ " + input + "}})"; + return result; +} + +function getPath(input, parserOpts = { plugins: ["destructuringPrivate"] }) { + let targetPath; + traverse( + parseSync(wrapSourceInClassEnvironment(input), { + parserOpts, + filename: "example.js", + configFile: false, + }), + { + Pattern(path) { + targetPath = path; + path.stop(); + }, + }, + ); + return targetPath; +} + +describe("traversePattern", () => { + it("should visit property with private keys in depth-first order", () => { + const patternPath = getPath( + "const { #a: { #b: b, c, ...d }, e: [{ #c: [{ #d: { #c: g } }] }, ...{ #b: h }], #a: i } = obj;", + ); + const keys = [ + ...traversePattern(patternPath.node, function* (node) { + if (isObjectProperty(node)) { + const propertyKey = node.key; + if (isPrivateName(propertyKey)) { + yield propertyKey.id.name; + } + } + }), + ]; + + expect(keys).toEqual(["a", "b", "c", "d", "c", "b", "a"]); + }); +}); + +describe("privateKeyPathIterator", () => { + const indexPaths = [ + ...privateKeyPathIterator( + getPath( + "const { #a: { a, #b: b, c, ...d }, e: [{ #c: [{ d: e, #d: { #c: f } }] }, ...{ #b: g }], #a: i } = obj;", + ).node, + ), + ].map(indexPath => indexPath.join(",")); + expect(indexPaths).toEqual([ + "0", + "0,1", + "1,0,0", + "1,0,0,0,1", + "1,0,0,0,1,0", + "1,1,0,0", + "2", + ]); +}); diff --git a/tsconfig.json b/tsconfig.json index 6272d8c1625a..49d94006ce9f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -43,6 +43,7 @@ "./packages/babel-plugin-proposal-class-properties/src/**/*.ts", "./packages/babel-plugin-proposal-class-static-block/src/**/*.ts", "./packages/babel-plugin-proposal-decorators/src/**/*.ts", + "./packages/babel-plugin-proposal-destructuring-private/src/**/*.ts", "./packages/babel-plugin-proposal-do-expressions/src/**/*.ts", "./packages/babel-plugin-proposal-dynamic-import/src/**/*.ts", "./packages/babel-plugin-proposal-export-default-from/src/**/*.ts", @@ -271,6 +272,9 @@ "@babel/plugin-proposal-decorators": [ "./packages/babel-plugin-proposal-decorators/src" ], + "@babel/plugin-proposal-destructuring-private": [ + "./packages/babel-plugin-proposal-destructuring-private/src" + ], "@babel/plugin-proposal-do-expressions": [ "./packages/babel-plugin-proposal-do-expressions/src" ], From 291987abadc2a7529946bfb4181653510522c5e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 1 Mar 2022 15:54:14 -0500 Subject: [PATCH 09/35] feat: add destructuring-private to standalone --- packages/babel-standalone/package.json | 1 + .../babel-standalone/scripts/pluginConfig.json | 1 + .../babel-standalone/src/generated/plugins.ts | 3 +++ .../babel-standalone/src/preset-stage-2.ts | 2 +- .../test/preset-stage-1.test.js | 14 ++++++++++++++ yarn.lock | 18 ++++++++++++++++++ 6 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/babel-standalone/package.json b/packages/babel-standalone/package.json index 3dd50d579d98..0806cf0ac369 100644 --- a/packages/babel-standalone/package.json +++ b/packages/babel-standalone/package.json @@ -14,6 +14,7 @@ "@babel/plugin-proposal-class-properties": "workspace:^", "@babel/plugin-proposal-class-static-block": "workspace:^", "@babel/plugin-proposal-decorators": "workspace:^", + "@babel/plugin-proposal-destructuring-private": "workspace:^", "@babel/plugin-proposal-do-expressions": "workspace:^", "@babel/plugin-proposal-dynamic-import": "workspace:^", "@babel/plugin-proposal-export-default-from": "workspace:^", diff --git a/packages/babel-standalone/scripts/pluginConfig.json b/packages/babel-standalone/scripts/pluginConfig.json index 4225511093b8..11bb05332ab1 100644 --- a/packages/babel-standalone/scripts/pluginConfig.json +++ b/packages/babel-standalone/scripts/pluginConfig.json @@ -25,6 +25,7 @@ "proposal-class-properties", "proposal-class-static-block", "proposal-decorators", + "proposal-destructuring-private", "proposal-do-expressions", "proposal-dynamic-import", "proposal-export-default-from", diff --git a/packages/babel-standalone/src/generated/plugins.ts b/packages/babel-standalone/src/generated/plugins.ts index 724211513a81..55d24bbdad1f 100644 --- a/packages/babel-standalone/src/generated/plugins.ts +++ b/packages/babel-standalone/src/generated/plugins.ts @@ -28,6 +28,7 @@ import proposalAsyncGeneratorFunctions from "@babel/plugin-proposal-async-genera import proposalClassProperties from "@babel/plugin-proposal-class-properties"; import proposalClassStaticBlock from "@babel/plugin-proposal-class-static-block"; import proposalDecorators from "@babel/plugin-proposal-decorators"; +import proposalDestructuringPrivate from "@babel/plugin-proposal-destructuring-private"; import proposalDoExpressions from "@babel/plugin-proposal-do-expressions"; import proposalDynamicImport from "@babel/plugin-proposal-dynamic-import"; import proposalExportDefaultFrom from "@babel/plugin-proposal-export-default-from"; @@ -124,6 +125,7 @@ export { proposalClassProperties, proposalClassStaticBlock, proposalDecorators, + proposalDestructuringPrivate, proposalDoExpressions, proposalDynamicImport, proposalExportDefaultFrom, @@ -221,6 +223,7 @@ export const all: { [k: string]: any } = { "proposal-class-properties": proposalClassProperties, "proposal-class-static-block": proposalClassStaticBlock, "proposal-decorators": proposalDecorators, + "proposal-destructuring-private": proposalDestructuringPrivate, "proposal-do-expressions": proposalDoExpressions, "proposal-dynamic-import": proposalDynamicImport, "proposal-export-default-from": proposalExportDefaultFrom, diff --git a/packages/babel-standalone/src/preset-stage-2.ts b/packages/babel-standalone/src/preset-stage-2.ts index ca4e77c369d7..0ecb70562407 100644 --- a/packages/babel-standalone/src/preset-stage-2.ts +++ b/packages/babel-standalone/src/preset-stage-2.ts @@ -23,7 +23,7 @@ export default (_: any, opts: any = {}) => { decoratorsBeforeExport, }, ], - babelPlugins.syntaxDestructuringPrivate, + babelPlugins.proposalDestructuringPrivate, [ babelPlugins.proposalPipelineOperator, { proposal: pipelineProposal, topicToken: pipelineTopicToken }, diff --git a/packages/babel-standalone/test/preset-stage-1.test.js b/packages/babel-standalone/test/preset-stage-1.test.js index b40159b77035..6e763f9e949c 100644 --- a/packages/babel-standalone/test/preset-stage-1.test.js +++ b/packages/babel-standalone/test/preset-stage-1.test.js @@ -62,5 +62,19 @@ const require = createRequire(import.meta.url); }).code; expect(output).toMatch("babelHelpers.applyDecs"); }); + it("should support private destructuring", () => { + const output = Babel.transform("class C { #x; m({ #x: x}) {} }", { + plugins: [["external-helpers", { helperVersion: "7.100.0" }]], + presets: [ + [ + "stage-1", + { + decoratorsVersion: "2021-12", + }, + ], + ], + }).code; + expect(output).not.toContain("#x:"); + }); }, ); diff --git a/yarn.lock b/yarn.lock index 991f40179bf5..aa5fcca1306b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1304,6 +1304,23 @@ __metadata: languageName: unknown linkType: soft +"@babel/plugin-proposal-destructuring-private@workspace:^, @babel/plugin-proposal-destructuring-private@workspace:packages/babel-plugin-proposal-destructuring-private": + version: 0.0.0-use.local + resolution: "@babel/plugin-proposal-destructuring-private@workspace:packages/babel-plugin-proposal-destructuring-private" + dependencies: + "@babel/core": "workspace:^" + "@babel/helper-plugin-test-runner": "workspace:^" + "@babel/helper-plugin-utils": "workspace:^" + "@babel/plugin-syntax-destructuring-private": "workspace:^" + "@babel/plugin-transform-destructuring": "workspace:^" + "@babel/plugin-transform-parameters": "workspace:^" + "@babel/traverse": "workspace:^" + "@babel/types": "workspace:^" + peerDependencies: + "@babel/core": ^7.17.0 + languageName: unknown + linkType: soft + "@babel/plugin-proposal-do-expressions@workspace:^, @babel/plugin-proposal-do-expressions@workspace:packages/babel-plugin-proposal-do-expressions": version: 0.0.0-use.local resolution: "@babel/plugin-proposal-do-expressions@workspace:packages/babel-plugin-proposal-do-expressions" @@ -3575,6 +3592,7 @@ __metadata: "@babel/plugin-proposal-class-properties": "workspace:^" "@babel/plugin-proposal-class-static-block": "workspace:^" "@babel/plugin-proposal-decorators": "workspace:^" + "@babel/plugin-proposal-destructuring-private": "workspace:^" "@babel/plugin-proposal-do-expressions": "workspace:^" "@babel/plugin-proposal-dynamic-import": "workspace:^" "@babel/plugin-proposal-export-default-from": "workspace:^" From 14cd6df22dd64801cf4e019abc3ce689b53d43ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 25 Mar 2022 17:13:51 -0400 Subject: [PATCH 10/35] Update packages/babel-plugin-proposal-destructuring-private/package.json MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Nicolò Ribaudo --- .../package.json | 8 -------- 1 file changed, 8 deletions(-) diff --git a/packages/babel-plugin-proposal-destructuring-private/package.json b/packages/babel-plugin-proposal-destructuring-private/package.json index a914fb6682d9..2da4b4c798bb 100644 --- a/packages/babel-plugin-proposal-destructuring-private/package.json +++ b/packages/babel-plugin-proposal-destructuring-private/package.json @@ -35,14 +35,6 @@ "node": ">=6.9.0" }, "author": "The Babel Team (https://babel.dev/team)", - "conditions": { - "BABEL_8_BREAKING": [ - null, - { - "exports": null - } - ] - }, "exports": { ".": "./lib/index.js", "./package.json": "./package.json" From f1bbd3b52c2b2f20e9084c495e8a6930c600bbcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 25 Mar 2022 17:15:19 -0400 Subject: [PATCH 11/35] Update packages/babel-plugin-proposal-destructuring-private/src/index.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Nicolò Ribaudo --- .../src/index.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/babel-plugin-proposal-destructuring-private/src/index.ts b/packages/babel-plugin-proposal-destructuring-private/src/index.ts index bb33c86057e1..48f3656f7cba 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/index.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/index.ts @@ -114,13 +114,12 @@ export default declare(function ({ blockBody.unshift(expressionStatement(scope.buildUndefinedNode())); } - // todo: handle shadowed variables referenced in computed keys: - // var a = 0, x;for ({ #x: x, [a++]: y } of z) { const a = 1; } - blockBody.unshift( + node.body = t.blockStatement([ expressionStatement( assignmentExpression("=", leftPath.node, cloneNode(declarator)), ), - ); + node.body, + ]); } }, VariableDeclaration(path, state) { From a0ca19bf1709fdcbf55576447ee1632c9e91ce84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 28 Mar 2022 16:27:00 -0400 Subject: [PATCH 12/35] fix: avoid reusing AST node --- packages/babel-plugin-transform-destructuring/src/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/babel-plugin-transform-destructuring/src/index.ts b/packages/babel-plugin-transform-destructuring/src/index.ts index 8f60c1c0ce37..879a681a7a7e 100644 --- a/packages/babel-plugin-transform-destructuring/src/index.ts +++ b/packages/babel-plugin-transform-destructuring/src/index.ts @@ -89,7 +89,9 @@ export default declare((api, options: Options) => { } statementBody.unshift( - t.expressionStatement(t.assignmentExpression("=", left, temp)), + t.expressionStatement( + t.assignmentExpression("=", left, t.cloneNode(temp)), + ), ); scope.crawl(); From 9df6e7cc9221e4901d7649ceea6e9a7beb7c075f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 28 Mar 2022 17:31:31 -0400 Subject: [PATCH 13/35] fix: wrap forStmt body in a new block when required --- .../src/index.ts | 60 ++++++++++--------- .../lhs-with-shadowed-block-scoped/exec.js | 10 ++++ .../lhs-with-shadowed-block-scoped/input.js | 10 ++++ .../lhs-with-shadowed-block-scoped/output.js | 18 ++++++ .../variable-declaration-block-scoped/exec.js | 10 ++++ .../input.js | 10 ++++ .../output.js | 18 ++++++ 7 files changed, 109 insertions(+), 27 deletions(-) create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs-with-shadowed-block-scoped/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs-with-shadowed-block-scoped/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs-with-shadowed-block-scoped/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration-block-scoped/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration-block-scoped/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration-block-scoped/output.js diff --git a/packages/babel-plugin-proposal-destructuring-private/src/index.ts b/packages/babel-plugin-proposal-destructuring-private/src/index.ts index 48f3656f7cba..d6f49cbf213c 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/index.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/index.ts @@ -9,7 +9,7 @@ import { import { convertFunctionParams } from "@babel/plugin-transform-parameters"; import type { PluginPass } from "@babel/core"; -import type { Visitor } from "@babel/traverse"; +import type { Visitor, NodePath } from "@babel/traverse"; import { types as t } from "@babel/core"; const { @@ -24,6 +24,27 @@ const { variableDeclarator, } = t; +function unshiftForXStatementBody( + statementPath: NodePath, + newStatement: t.Statement, +) { + statementPath.ensureBlock(); + const { scope, node } = statementPath; + const bodyScopeBindings = statementPath.get("body").scope.bindings; + const hasShadowedBlockScopedBindings = Object.keys(bodyScopeBindings).some( + name => scope.hasBinding(name), + ); + + if (hasShadowedBlockScopedBindings) { + // handle shadowed variables referenced in computed keys: + // var a = 0;for (const { #x: x, [a++]: y } of z) { const a = 1; } + node.body = t.blockStatement([newStatement, node.body]); + } else { + // @ts-ignore statementPath.ensureBlock() has been called, node.body is always a BlockStatement + node.body.body.unshift(newStatement); + } +} + export default declare(function ({ assertVersion, assumption, @@ -81,16 +102,12 @@ export default declare(function ({ // for (const { #x: x } of cls) body; // transforms to: // for (const ref of cls) { const { #x: x } = ref; body; } - const declarator = scope.generateUidIdentifier("ref"); + const temp = scope.generateUidIdentifier("ref"); node.left = variableDeclaration(left.kind, [ - variableDeclarator(declarator, null), + variableDeclarator(temp, null), ]); - left.declarations[0].init = cloneNode(declarator); - path.ensureBlock(); - // todo: handle shadowed variables referenced in computed keys: - // var a = 0;for (const { #x: x, [a++]: y } of z) { const a = 1; } - const blockBody = (node.body as t.BlockStatement).body; - blockBody.unshift(left); + left.declarations[0].init = cloneNode(temp); + unshiftForXStatementBody(path, left); scope.crawl(); // the pattern will be handled by VariableDeclaration visitor. } else if (leftPath.isPattern()) { @@ -100,26 +117,15 @@ export default declare(function ({ // for (const ref of cls) { ({ #x: x } = ref); body; } // This transform assumes that any expression within the pattern // does not interfere with the iterable `cls`. - const declarator = scope.generateUidIdentifier("ref"); + const temp = scope.generateUidIdentifier("ref"); node.left = variableDeclaration("const", [ - variableDeclarator(declarator, null), - ]); - path.ensureBlock(); - - const blockBody = (node.body as t.BlockStatement).body; - - // preserve completion record: - // for ({ #x: x } of []); - if (blockBody.length === 0 && path.isCompletionRecord()) { - blockBody.unshift(expressionStatement(scope.buildUndefinedNode())); - } - - node.body = t.blockStatement([ - expressionStatement( - assignmentExpression("=", leftPath.node, cloneNode(declarator)), - ), - node.body, + variableDeclarator(temp, null), ]); + const assignExpr = expressionStatement( + assignmentExpression("=", leftPath.node, cloneNode(temp)), + ); + unshiftForXStatementBody(path, assignExpr); + scope.crawl(); } }, VariableDeclaration(path, state) { diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs-with-shadowed-block-scoped/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs-with-shadowed-block-scoped/exec.js new file mode 100644 index 000000000000..d9289567a2f7 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs-with-shadowed-block-scoped/exec.js @@ -0,0 +1,10 @@ +class C { + static a = "a"; + static #x; + static { + var x, a = "a"; + for ({ #x: x, [a]: a } of [C]) { + const a = "A"; + } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs-with-shadowed-block-scoped/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs-with-shadowed-block-scoped/input.js new file mode 100644 index 000000000000..d9289567a2f7 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs-with-shadowed-block-scoped/input.js @@ -0,0 +1,10 @@ +class C { + static a = "a"; + static #x; + static { + var x, a = "a"; + for ({ #x: x, [a]: a } of [C]) { + const a = "A"; + } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs-with-shadowed-block-scoped/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs-with-shadowed-block-scoped/output.js new file mode 100644 index 000000000000..b55626d8c90b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/lhs-with-shadowed-block-scoped/output.js @@ -0,0 +1,18 @@ +class C { + static a = "a"; + static #x; + static { + var x, + a = "a"; + + for (const _ref of [C]) { + x = _ref.#x, ({ + [a]: a + } = _ref); + { + const a = "A"; + } + } + + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration-block-scoped/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration-block-scoped/exec.js new file mode 100644 index 000000000000..59f9f48f8c71 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration-block-scoped/exec.js @@ -0,0 +1,10 @@ +class C { + static a = "a"; + static #x; + static { + const a = "a"; + for (const { #x: x, [a]: _ } of [C]) { + const a = "A"; + } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration-block-scoped/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration-block-scoped/input.js new file mode 100644 index 000000000000..59f9f48f8c71 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration-block-scoped/input.js @@ -0,0 +1,10 @@ +class C { + static a = "a"; + static #x; + static { + const a = "a"; + for (const { #x: x, [a]: _ } of [C]) { + const a = "A"; + } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration-block-scoped/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration-block-scoped/output.js new file mode 100644 index 000000000000..a4e6fd6b4598 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of/variable-declaration-block-scoped/output.js @@ -0,0 +1,18 @@ +class C { + static a = "a"; + static #x; + static { + const a = "a"; + + for (const _ref of [C]) { + const x = _ref.#x, + { + [a]: _ + } = _ref; + { + const a = "A"; + } + } + + } +} From 157798042e2bd64af3054943b95ba59c541f9f3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 28 Mar 2022 17:53:17 -0400 Subject: [PATCH 14/35] fix: move unshiftForXStatementBody to transform-destructuring --- .../src/index.ts | 28 +++---------------- .../lhs-with-shadowed-block-scoped/exec.js | 10 +++++++ .../lhs-with-shadowed-block-scoped/input.js | 10 +++++++ .../lhs-with-shadowed-block-scoped/output.js | 21 ++++++++++++++ .../variable-declaration-block-scoped/exec.js | 10 +++++++ .../input.js | 10 +++++++ .../output.js | 21 ++++++++++++++ .../src/index.ts | 18 +++++++----- .../src/util.ts | 21 ++++++++++++++ .../for-of-shadowd-block-scoped/exec.js | 8 ++++++ .../for-of-shadowd-block-scoped/input.js | 8 ++++++ .../for-of-shadowd-block-scoped/options.json | 3 ++ .../for-of-shadowd-block-scoped/output.js | 20 +++++++++++++ 13 files changed, 157 insertions(+), 31 deletions(-) create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs-with-shadowed-block-scoped/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs-with-shadowed-block-scoped/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs-with-shadowed-block-scoped/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration-block-scoped/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration-block-scoped/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration-block-scoped/output.js create mode 100644 packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/exec.js create mode 100644 packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/input.js create mode 100644 packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/options.json create mode 100644 packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/output.js diff --git a/packages/babel-plugin-proposal-destructuring-private/src/index.ts b/packages/babel-plugin-proposal-destructuring-private/src/index.ts index d6f49cbf213c..ef56b1e15e1e 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/index.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/index.ts @@ -7,9 +7,10 @@ import { buildVariableDeclarationFromParams, } from "./util"; import { convertFunctionParams } from "@babel/plugin-transform-parameters"; +import { unshiftForXStatementBody } from "@babel/plugin-transform-destructuring"; import type { PluginPass } from "@babel/core"; -import type { Visitor, NodePath } from "@babel/traverse"; +import type { Visitor } from "@babel/traverse"; import { types as t } from "@babel/core"; const { @@ -24,27 +25,6 @@ const { variableDeclarator, } = t; -function unshiftForXStatementBody( - statementPath: NodePath, - newStatement: t.Statement, -) { - statementPath.ensureBlock(); - const { scope, node } = statementPath; - const bodyScopeBindings = statementPath.get("body").scope.bindings; - const hasShadowedBlockScopedBindings = Object.keys(bodyScopeBindings).some( - name => scope.hasBinding(name), - ); - - if (hasShadowedBlockScopedBindings) { - // handle shadowed variables referenced in computed keys: - // var a = 0;for (const { #x: x, [a++]: y } of z) { const a = 1; } - node.body = t.blockStatement([newStatement, node.body]); - } else { - // @ts-ignore statementPath.ensureBlock() has been called, node.body is always a BlockStatement - node.body.body.unshift(newStatement); - } -} - export default declare(function ({ assertVersion, assumption, @@ -107,7 +87,7 @@ export default declare(function ({ variableDeclarator(temp, null), ]); left.declarations[0].init = cloneNode(temp); - unshiftForXStatementBody(path, left); + unshiftForXStatementBody(path, [left]); scope.crawl(); // the pattern will be handled by VariableDeclaration visitor. } else if (leftPath.isPattern()) { @@ -124,7 +104,7 @@ export default declare(function ({ const assignExpr = expressionStatement( assignmentExpression("=", leftPath.node, cloneNode(temp)), ); - unshiftForXStatementBody(path, assignExpr); + unshiftForXStatementBody(path, [assignExpr]); scope.crawl(); } }, diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs-with-shadowed-block-scoped/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs-with-shadowed-block-scoped/exec.js new file mode 100644 index 000000000000..d9289567a2f7 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs-with-shadowed-block-scoped/exec.js @@ -0,0 +1,10 @@ +class C { + static a = "a"; + static #x; + static { + var x, a = "a"; + for ({ #x: x, [a]: a } of [C]) { + const a = "A"; + } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs-with-shadowed-block-scoped/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs-with-shadowed-block-scoped/input.js new file mode 100644 index 000000000000..d9289567a2f7 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs-with-shadowed-block-scoped/input.js @@ -0,0 +1,10 @@ +class C { + static a = "a"; + static #x; + static { + var x, a = "a"; + for ({ #x: x, [a]: a } of [C]) { + const a = "A"; + } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs-with-shadowed-block-scoped/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs-with-shadowed-block-scoped/output.js new file mode 100644 index 000000000000..2720cf683d2c --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/lhs-with-shadowed-block-scoped/output.js @@ -0,0 +1,21 @@ +class C {} + +babelHelpers.defineProperty(C, "a", "a"); +var _x = { + writable: true, + value: void 0 +}; + +(() => { + var x, + a = "a"; + + for (const _ref of [C]) { + x = babelHelpers.classStaticPrivateFieldSpecGet(_ref, C, _x), ({ + [a]: a + } = _ref); + { + const a = "A"; + } + } +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration-block-scoped/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration-block-scoped/exec.js new file mode 100644 index 000000000000..59f9f48f8c71 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration-block-scoped/exec.js @@ -0,0 +1,10 @@ +class C { + static a = "a"; + static #x; + static { + const a = "a"; + for (const { #x: x, [a]: _ } of [C]) { + const a = "A"; + } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration-block-scoped/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration-block-scoped/input.js new file mode 100644 index 000000000000..59f9f48f8c71 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration-block-scoped/input.js @@ -0,0 +1,10 @@ +class C { + static a = "a"; + static #x; + static { + const a = "a"; + for (const { #x: x, [a]: _ } of [C]) { + const a = "A"; + } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration-block-scoped/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration-block-scoped/output.js new file mode 100644 index 000000000000..dc98f60842bd --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-of--es2015/variable-declaration-block-scoped/output.js @@ -0,0 +1,21 @@ +class C {} + +babelHelpers.defineProperty(C, "a", "a"); +var _x = { + writable: true, + value: void 0 +}; + +(() => { + const a = "a"; + + for (const _ref of [C]) { + const x = babelHelpers.classStaticPrivateFieldSpecGet(_ref, C, _x), + { + [a]: _ + } = _ref; + { + const a = "A"; + } + } +})(); diff --git a/packages/babel-plugin-transform-destructuring/src/index.ts b/packages/babel-plugin-transform-destructuring/src/index.ts index 879a681a7a7e..a7ae0178bc8f 100644 --- a/packages/babel-plugin-transform-destructuring/src/index.ts +++ b/packages/babel-plugin-transform-destructuring/src/index.ts @@ -4,8 +4,9 @@ import { DestructuringTransformer, convertVariableDeclaration, convertAssignmentExpression, + unshiftForXStatementBody, } from "./util"; -export { buildObjectExcludingKeys } from "./util"; +export { buildObjectExcludingKeys, unshiftForXStatementBody } from "./util"; /** * Test if a VariableDeclaration's declarations contains any Patterns. @@ -81,19 +82,22 @@ export default declare((api, options: Options) => { path.ensureBlock(); const statementBody = (node.body as t.BlockStatement).body; - + const nodes = []; + // todo: the completion of a for statement can only be observed from + // a do block (or eval that we don't support), + // but do-expression transform already handled the case of for statement well + // maybe we can get rid of this if (statementBody.length === 0 && path.isCompletionRecord()) { - statementBody.unshift( - t.expressionStatement(scope.buildUndefinedNode()), - ); + nodes.unshift(t.expressionStatement(scope.buildUndefinedNode())); } - statementBody.unshift( + nodes.unshift( t.expressionStatement( t.assignmentExpression("=", left, t.cloneNode(temp)), ), ); + unshiftForXStatementBody(path, nodes); scope.crawl(); return; } @@ -123,7 +127,7 @@ export default declare((api, options: Options) => { destructuring.init(pattern, key); - path.ensureBlock(); + unshiftForXStatementBody(path, nodes); const block = node.body; // @ts-expect-error: ensureBlock ensures that node.body is a BlockStatement diff --git a/packages/babel-plugin-transform-destructuring/src/util.ts b/packages/babel-plugin-transform-destructuring/src/util.ts index d40c3ae77214..5f22b4a86ee5 100644 --- a/packages/babel-plugin-transform-destructuring/src/util.ts +++ b/packages/babel-plugin-transform-destructuring/src/util.ts @@ -3,6 +3,27 @@ import type { File } from "@babel/core"; import type { Scope, NodePath } from "@babel/traverse"; import type { TraversalAncestors } from "@babel/types"; +export function unshiftForXStatementBody( + statementPath: NodePath, + newStatements: t.Statement[], +) { + statementPath.ensureBlock(); + const { scope, node } = statementPath; + const bodyScopeBindings = statementPath.get("body").scope.bindings; + const hasShadowedBlockScopedBindings = Object.keys(bodyScopeBindings).some( + name => scope.hasBinding(name), + ); + + if (hasShadowedBlockScopedBindings) { + // handle shadowed variables referenced in computed keys: + // var a = 0;for (const { #x: x, [a++]: y } of z) { const a = 1; } + node.body = t.blockStatement([...newStatements, node.body]); + } else { + // @ts-ignore statementPath.ensureBlock() has been called, node.body is always a BlockStatement + node.body.body.unshift(...newStatements); + } +} + /** * Test if an ArrayPattern's elements contain any RestElements. */ diff --git a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/exec.js b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/exec.js new file mode 100644 index 000000000000..e3f8f6f30ea2 --- /dev/null +++ b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/exec.js @@ -0,0 +1,8 @@ +var O = { + a: "a" +} +const a = "a"; +for (const { [a]: _ } of [O]) { const a = "A"; } + +var _; +for ({ [a]: _ } of [O]) { const a = "A"; } diff --git a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/input.js b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/input.js new file mode 100644 index 000000000000..e3f8f6f30ea2 --- /dev/null +++ b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/input.js @@ -0,0 +1,8 @@ +var O = { + a: "a" +} +const a = "a"; +for (const { [a]: _ } of [O]) { const a = "A"; } + +var _; +for ({ [a]: _ } of [O]) { const a = "A"; } diff --git a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/options.json b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/options.json new file mode 100644 index 000000000000..84e28c07f51e --- /dev/null +++ b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["transform-destructuring"] +} diff --git a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/output.js b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/output.js new file mode 100644 index 000000000000..23908dee29eb --- /dev/null +++ b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/output.js @@ -0,0 +1,20 @@ +var O = { + a: "a" +}; +const a = "a"; + +for (const _ref of [O]) { + const _ = _ref[a]; + { + const a = "A"; + } +} + +var _; + +for (var _ref2 of [O]) { + _ = _ref2[a]; + { + const a = "A"; + } +} From 701ff4cbe49c6f43c61ca8d48eb9ff22d51ddfea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 29 Mar 2022 10:25:06 -0400 Subject: [PATCH 15/35] optimize: transpile array pattern from the first private element --- .../src/util.ts | 17 ++++++----------- .../member-expression/output.js | 4 ++-- .../array-rest-destructuring-middle/exec.js | 10 ++++++++++ .../array-rest-destructuring-middle/input.js | 7 +++++++ .../array-rest-destructuring-middle/output.js | 14 ++++++++++++++ .../assignment/member-expression/output.js | 4 ++-- .../src/index.ts | 4 ++-- 7 files changed, 43 insertions(+), 17 deletions(-) create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-destructuring-middle/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-destructuring-middle/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-destructuring-middle/output.js diff --git a/packages/babel-plugin-proposal-destructuring-private/src/util.ts b/packages/babel-plugin-proposal-destructuring-private/src/util.ts index 757c75784059..36c25f0ab8be 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/util.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/util.ts @@ -389,28 +389,23 @@ export function* transformPrivateKeyDestructuring( // in RHS. Otherwise we have to pause the iterable and interleave // the expressions. // See also https://gist.github.com/nicolo-ribaudo/f8ac7916f89450f2ead77d99855b2098 + const leftElements = left.elements; + const leftElementsAfterIndex = leftElements.splice(index); const { elements, transformed } = buildAssignmentsFromPatternList( - left.elements, + leftElementsAfterIndex, scope, isAssignment, ); - left.elements = elements; + leftElements.push(...elements); yield { left, right: cloneNode(right) }; - // we are sure elements[0, index) does not contain private keys - for (let i = 0; i < index; i++) { - // skipping array holes - if (transformed[i] !== null) { - yield transformed[i]; - } - } // for elements after `index`, push them to stack so we can process them later - for (let i = transformed.length - 1; i > index; i--) { + for (let i = transformed.length - 1; i > 0; i--) { // skipping array holes if (transformed[i] !== null) { stack.push(transformed[i]); } } - ({ left, right } = transformed[index]); + ({ left, right } = transformed[0]); break; } default: diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/output.js index c63cef733485..0a8f2fdf5106 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/output.js @@ -16,8 +16,8 @@ var _z = { }; (() => { - var _m, _p, _p2, _p3; + var _m, _p, _p2; let z; - _m = [0, C], [_p, _p2, ..._p3] = _m, babelHelpers.classStaticPrivateFieldSpecSet(C, C, _x, _p), x = babelHelpers.classStaticPrivateFieldSpecGet(_p2, C, _x), z = _p3; + _m = [0, C], [babelHelpers.classStaticPrivateFieldDestructureSet(C, C, _x).value, _p, ..._p2] = _m, x = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x), z = _p2; })(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-destructuring-middle/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-destructuring-middle/exec.js new file mode 100644 index 000000000000..6d1e6de5d111 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-destructuring-middle/exec.js @@ -0,0 +1,10 @@ +let x, y, z; +class C { + static #x; + static { + ([{ y }, { #x: x = y }, ...z] = [{ y: 1}, C]); + } +} + +expect(x).toBe(1); +expect(z).toStrictEqual([]); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-destructuring-middle/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-destructuring-middle/input.js new file mode 100644 index 000000000000..70615f84cae2 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-destructuring-middle/input.js @@ -0,0 +1,7 @@ +let x, y, z; +class C { + static #x; + static { + ([{ y }, { #x: x = y }, ...z] = [{ y: 1}, C]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-destructuring-middle/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-destructuring-middle/output.js new file mode 100644 index 000000000000..af1d0bf1a148 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-destructuring-middle/output.js @@ -0,0 +1,14 @@ +let x, y, z; + +class C { + static #x; + static { + var _m, _p, _p2, _m2; + + _m = [{ + y: 1 + }, C], [{ + y + }, _p, ..._p2] = _m, _m2 = _p.#x, x = _m2 === void 0 ? y : _m2, z = _p2; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/output.js index ff4f866363fe..a7c239f9e544 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/output.js @@ -5,9 +5,9 @@ class C { static #y; static #z; static { - var _m, _p, _p2, _p3; + var _m, _p, _p2; let z; - _m = [0, C], [_p, _p2, ..._p3] = _m, C.#x = _p, x = _p2.#x, z = _p3; + _m = [0, C], [C.#x, _p, ..._p2] = _m, x = _p.#x, z = _p2; } } diff --git a/packages/babel-plugin-transform-destructuring/src/index.ts b/packages/babel-plugin-transform-destructuring/src/index.ts index a7ae0178bc8f..a0566fea6f68 100644 --- a/packages/babel-plugin-transform-destructuring/src/index.ts +++ b/packages/babel-plugin-transform-destructuring/src/index.ts @@ -85,8 +85,8 @@ export default declare((api, options: Options) => { const nodes = []; // todo: the completion of a for statement can only be observed from // a do block (or eval that we don't support), - // but do-expression transform already handled the case of for statement well - // maybe we can get rid of this + // but the new do-expression proposal plans to ban iteration ends in the + // do block, maybe we can get rid of this if (statementBody.length === 0 && path.isCompletionRecord()) { nodes.unshift(t.expressionStatement(scope.buildUndefinedNode())); } From 60f681f87a322e0d8a02bc3716e99ca4bf63c202 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 29 Mar 2022 11:15:38 -0400 Subject: [PATCH 16/35] optimize: transpile function params from the first destructuring private --- .../src/index.ts | 11 +++++++---- .../no-shadowed-params/input.js | 3 +-- .../no-shadowed-params/output.js | 8 ++++---- .../shadowed-params/input.js | 4 ++-- .../shadowed-params/output.js | 14 +++++++------- .../function-params/no-shadowed-params/input.js | 3 +-- .../function-params/no-shadowed-params/output.js | 8 ++++---- .../function-params/shadowed-params/input.js | 4 ++-- .../function-params/shadowed-params/output.js | 14 +++++++------- .../valid-before-destructuring-private/output.js | 5 ++--- 10 files changed, 37 insertions(+), 37 deletions(-) diff --git a/packages/babel-plugin-proposal-destructuring-private/src/index.ts b/packages/babel-plugin-proposal-destructuring-private/src/index.ts index ef56b1e15e1e..ab4fd7cf5a27 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/index.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/index.ts @@ -42,17 +42,20 @@ export default declare(function ({ // (b, { #x: x } = I) => body // transforms to: // (p1, p2) => { var b = p1, { #x: x } = p2 === undefined ? I : p2; body; } - if (!path.node.params.some(param => hasPrivateKeys(param))) return; + const index = path.node.params.findIndex(param => hasPrivateKeys(param)); + if (index === -1) return; // wrap function body within IIFE if any param is shadowed convertFunctionParams(path, ignoreFunctionLength, () => false, false); const { node, scope } = path; - const { params, variableDeclaration } = - buildVariableDeclarationFromParams(node.params, scope); + const params = node.params; + const paramsAfterIndex = params.splice(index); + const { params: transformedParams, variableDeclaration } = + buildVariableDeclarationFromParams(paramsAfterIndex, scope); path .get("body") // invariant: path.body is always a BlockStatement .unshiftContainer("body", variableDeclaration); - node.params = params; + params.push(...transformedParams); scope.crawl(); // the pattern will be handled by VariableDeclaration visitor. }, diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/input.js index 90f84a73805f..6d39f49343ca 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/input.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/input.js @@ -1,6 +1,5 @@ class C { #x; - m(a, { #x: x }, ...b) { - + m(a = 1, { #x: x }, b, ...c) { } } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/output.js index 2b2fd2073cdf..657643b209fe 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/output.js @@ -8,10 +8,10 @@ class C { }); } - m(_p, _p2, ..._p3) { - var a = _p, - x = babelHelpers.classPrivateFieldGet(_p2, _x), - b = _p3; + m(a = 1, _p, _p2, ..._p3) { + var x = babelHelpers.classPrivateFieldGet(_p, _x), + b = _p2, + c = _p3; } } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/input.js index 4c877a3d1481..196951aa94ef 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/input.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/input.js @@ -1,6 +1,6 @@ class C { #x; - m(a, { #x: x }, ...b) { - var a = 1; + m(a = 1, { #x: x }, b, ...c) { + var b = 1; } } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/output.js index 1ed0c90d22d3..e6c11d5e6ab6 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/shadowed-params/output.js @@ -8,13 +8,13 @@ class C { }); } - m(_p, _p2, ..._p3) { - var a = _p, - x = babelHelpers.classPrivateFieldGet(_p2, _x), - b = _p3; - return function (a) { - var a = 1; - }(a); + m(a = 1, _p, _p2, ..._p3) { + var x = babelHelpers.classPrivateFieldGet(_p, _x), + b = _p2, + c = _p3; + return function (b) { + var b = 1; + }(b); } } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/input.js index 90f84a73805f..6d39f49343ca 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/input.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/input.js @@ -1,6 +1,5 @@ class C { #x; - m(a, { #x: x }, ...b) { - + m(a = 1, { #x: x }, b, ...c) { } } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/output.js index b110d694a6fc..d5263e0ac70f 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/output.js @@ -1,10 +1,10 @@ class C { #x; - m(_p, _p2, ..._p3) { - var a = _p, - x = _p2.#x, - b = _p3; + m(a = 1, _p, _p2, ..._p3) { + var x = _p.#x, + b = _p2, + c = _p3; } } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/input.js index 4c877a3d1481..196951aa94ef 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/input.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/input.js @@ -1,6 +1,6 @@ class C { #x; - m(a, { #x: x }, ...b) { - var a = 1; + m(a = 1, { #x: x }, b, ...c) { + var b = 1; } } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/output.js index ced7c933c0c4..a4d9eb4c525a 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/shadowed-params/output.js @@ -1,13 +1,13 @@ class C { #x; - m(_p, _p2, ..._p3) { - var a = _p, - x = _p2.#x, - b = _p3; - return function (a) { - var a = 1; - }(a); + m(a = 1, _p, _p2, ..._p3) { + var x = _p.#x, + b = _p2, + c = _p3; + return function (b) { + var b = 1; + }(b); } } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/output.js index 67bc13190763..f7222cd31345 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/output.js @@ -1,9 +1,8 @@ class C { #x; - constructor(_p, _p2) { - var foo = _p, - x = _p2.#x; + constructor(foo, _p) { + var x = _p.#x; this.foo = foo; } From a6078501f284136e134f3df80074d6587d6474d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 29 Mar 2022 13:46:04 -0400 Subject: [PATCH 17/35] fix: rest should exclude key containing deep private destructuring --- .../src/util.ts | 31 ++++++++++++------- .../array-rest-only/exec.js | 10 ++++++ .../array-rest-only/input.js | 9 ++++++ .../array-rest-only/output.js | 20 ++++++++++++ .../assignment/array-rest-only/exec.js | 10 ++++++ .../assignment/array-rest-only/input.js | 9 ++++++ .../assignment/array-rest-only/output.js | 16 ++++++++++ .../array-rest-only/exec.js | 9 ++++++ .../array-rest-only/input.js | 8 +++++ .../array-rest-only/output.js | 23 ++++++++++++++ .../array-rest-only/exec.js | 9 ++++++ .../array-rest-only/input.js | 8 +++++ .../array-rest-only/output.js | 18 +++++++++++ 13 files changed, 169 insertions(+), 11 deletions(-) create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-only/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-only/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-only/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-only/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-only/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-only/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/output.js diff --git a/packages/babel-plugin-proposal-destructuring-private/src/util.ts b/packages/babel-plugin-proposal-destructuring-private/src/util.ts index 36c25f0ab8be..b987e89964ea 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/util.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/util.ts @@ -53,12 +53,13 @@ function initRestExcludingKeys(pattern: t.LVal) { */ function* growRestExcludingKeys(properties: ObjectProperty[], scope: Scope) { for (const property of properties) { - if (property.computed && !scope.isStatic(property.key)) { + const propertyKey = property.key; + if (property.computed && !scope.isStatic(propertyKey)) { const tempId = scope.generateDeclaredUidIdentifier("m"); // @ts-expect-error A computed property key must not be a private name - property.key = assignmentExpression("=", tempId, property.key); + property.key = assignmentExpression("=", tempId, propertyKey); yield { key: tempId, computed: true }; - } else { + } else if (propertyKey.type !== "PrivateName") { yield property; } } @@ -250,14 +251,18 @@ export function* privateKeyPathIterator(pattern: t.LVal) { }); } +type ExcludingKey = { + key: t.ObjectProperty["key"]; + computed: t.ObjectProperty["computed"]; +}; type Item = { left: t.LVal; right: t.Expression; - restExcludingKeys?: t.ObjectProperty[] | null; + restExcludingKeys?: ExcludingKey[] | null; }; /** - * Transform private destructuring without object rest element. It returns a generator + * Transform private destructuring. It returns a generator * which yields a pair of transformed LHS and RHS, which can form VariableDeclaration or * AssignmentExpression later. * @@ -345,12 +350,6 @@ export function* transformPrivateKeyDestructuring( if (index > 0) { // properties[0, index) must not contain private keys const propertiesSlice = properties.slice(0, index); - if (nextRestExcludingKeys !== null) { - nextRestExcludingKeys.push( - // @ts-expect-error - ...growRestExcludingKeys(propertiesSlice, scope), - ); - } yield { left: objectPattern(propertiesSlice), right: cloneNode(right), @@ -358,6 +357,16 @@ export function* transformPrivateKeyDestructuring( } if (index < properties.length - 1) { // for properties after `index`, push them to stack so we can process them later + if (nextRestExcludingKeys !== null) { + nextRestExcludingKeys.push( + ...growRestExcludingKeys( + // @ts-expect-error properties[0, index] must not contain rest element + // because properties[index] contains a private key + properties.slice(0, index + 1), + scope, + ), + ); + } stack.push({ left: objectPattern(properties.slice(index + 1)), right: cloneNode(right), diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-only/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-only/exec.js new file mode 100644 index 000000000000..3748bab5a407 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-only/exec.js @@ -0,0 +1,10 @@ +let result; +class C { + static #x; + static { + var x, z; + [...{ 0: { #x: x = 1 }, ...z }] = [C]; + result = { x, z }; + } +} +expect(result).toStrictEqual({ x: 1, z: {} }); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-only/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-only/input.js new file mode 100644 index 000000000000..7f14fc9c787a --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-only/input.js @@ -0,0 +1,9 @@ +let result; +class C { + static #x; + static { + var x, z; + [...{ 0: { #x: x = 1 }, ...z }] = [C]; + result = { x, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-only/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-only/output.js new file mode 100644 index 000000000000..229a7504038c --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-only/output.js @@ -0,0 +1,20 @@ +const _excluded = ["0"]; +let result; + +class C {} + +var _x = { + writable: true, + value: void 0 +}; + +(() => { + var _m, _p, _m2, _m3; + + var x, z; + _m = [C], [..._p] = _m, _m2 = _p[0], _m3 = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _x), x = _m3 === void 0 ? 1 : _m3, z = babelHelpers.objectWithoutProperties(_p, _excluded); + result = { + x, + z + }; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-only/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-only/exec.js new file mode 100644 index 000000000000..3748bab5a407 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-only/exec.js @@ -0,0 +1,10 @@ +let result; +class C { + static #x; + static { + var x, z; + [...{ 0: { #x: x = 1 }, ...z }] = [C]; + result = { x, z }; + } +} +expect(result).toStrictEqual({ x: 1, z: {} }); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-only/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-only/input.js new file mode 100644 index 000000000000..7f14fc9c787a --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-only/input.js @@ -0,0 +1,9 @@ +let result; +class C { + static #x; + static { + var x, z; + [...{ 0: { #x: x = 1 }, ...z }] = [C]; + result = { x, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-only/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-only/output.js new file mode 100644 index 000000000000..32ab0c98278b --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-only/output.js @@ -0,0 +1,16 @@ +const _excluded = ["0"]; +let result; + +class C { + static #x; + static { + var _m, _p, _m2, _m3; + + var x, z; + _m = [C], [..._p] = _m, _m2 = _p[0], _m3 = _m2.#x, x = _m3 === void 0 ? 1 : _m3, z = babelHelpers.objectWithoutProperties(_p, _excluded); + result = { + x, + z + }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/exec.js new file mode 100644 index 000000000000..91ce1174575d --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/exec.js @@ -0,0 +1,9 @@ +let result; +class C { + static #x; + static { + var [...{ 0: { #x: x = 1 }, ...z }] = [C]; + result = { x, z }; + } +} +expect(result).toStrictEqual({ x: 1, z: {} }); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/input.js new file mode 100644 index 000000000000..0efba383ed22 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/input.js @@ -0,0 +1,8 @@ +let result; +class C { + static #x; + static { + var [...{ 0: { #x: x = 1 }, ...z }] = [C]; + result = { x, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/output.js new file mode 100644 index 000000000000..689af34d23ad --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/output.js @@ -0,0 +1,23 @@ +const _excluded = ["0"]; +let result; + +class C {} + +var _x = { + writable: true, + value: void 0 +}; + +(() => { + var _m = [C], + [..._p] = _m, + _m2 = _p[0], + _m3 = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _x), + x = _m3 === void 0 ? 1 : _m3, + z = babelHelpers.objectWithoutProperties(_p, _excluded); + + result = { + x, + z + }; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/exec.js new file mode 100644 index 000000000000..91ce1174575d --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/exec.js @@ -0,0 +1,9 @@ +let result; +class C { + static #x; + static { + var [...{ 0: { #x: x = 1 }, ...z }] = [C]; + result = { x, z }; + } +} +expect(result).toStrictEqual({ x: 1, z: {} }); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/input.js new file mode 100644 index 000000000000..0efba383ed22 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/input.js @@ -0,0 +1,8 @@ +let result; +class C { + static #x; + static { + var [...{ 0: { #x: x = 1 }, ...z }] = [C]; + result = { x, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/output.js new file mode 100644 index 000000000000..37ad571bcd4c --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/output.js @@ -0,0 +1,18 @@ +const _excluded = ["0"]; +let result; + +class C { + static #x; + static { + var _m = [C], + [..._p] = _m, + _m2 = _p[0], + _m3 = _m2.#x, + x = _m3 === void 0 ? 1 : _m3, + z = babelHelpers.objectWithoutProperties(_p, _excluded); + result = { + x, + z + }; + } +} From 66261a17f92912b868936f8089f29f8510034330 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 29 Mar 2022 14:32:04 -0400 Subject: [PATCH 18/35] type: fine-tuned transform types --- .../src/index.ts | 2 ++ .../src/util.ts | 31 ++++++++++++------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/packages/babel-plugin-proposal-destructuring-private/src/index.ts b/packages/babel-plugin-proposal-destructuring-private/src/index.ts index ab4fd7cf5a27..77b6b339e560 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/index.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/index.ts @@ -120,6 +120,7 @@ export default declare(function ({ const newDeclarations = []; for (const declarator of declarations) { for (const { left, right } of transformPrivateKeyDestructuring( + // @ts-expect-error The id of a variable declarator must not be a RestElement declarator.id, declarator.init, scope, @@ -140,6 +141,7 @@ export default declare(function ({ if (!hasPrivateKeys(node.left)) return; const assignments = []; for (const { left, right } of transformPrivateKeyDestructuring( + // @ts-expect-error The left of an assignment expression must not be a RestElement node.left, node.right, scope, diff --git a/packages/babel-plugin-proposal-destructuring-private/src/util.ts b/packages/babel-plugin-proposal-destructuring-private/src/util.ts index b987e89964ea..5ad74bc03109 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/util.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/util.ts @@ -251,12 +251,19 @@ export function* privateKeyPathIterator(pattern: t.LVal) { }); } +type LHS = + | t.Identifier + | t.MemberExpression + | t.ArrayPattern + | t.ObjectPattern + | t.AssignmentPattern; + type ExcludingKey = { key: t.ObjectProperty["key"]; computed: t.ObjectProperty["computed"]; }; type Item = { - left: t.LVal; + left: LHS; right: t.Expression; restExcludingKeys?: ExcludingKey[] | null; }; @@ -267,14 +274,14 @@ type Item = { * AssignmentExpression later. * * @export - * @param {t.LVal} left The root pattern + * @param {LHS} left The root pattern * @param {t.Expression} right The initializer or the RHS of pattern * @param {Scope} scope The scope where memoized id should be registered * @param {boolean} isAssignment Whether we are transforming from an AssignmengExpression of VariableDeclaration * @returns {Generator} */ export function* transformPrivateKeyDestructuring( - left: t.LVal, + left: LHS, right: t.Expression, scope: Scope, isAssignment: boolean, @@ -303,7 +310,8 @@ export function* transformPrivateKeyDestructuring( // `z = babelHelpers.objectWithoutProperties(m, ["x"])` const { properties } = left as t.ObjectPattern; if (properties.length === 1) { - left = (properties[0] as t.RestElement).argument; + // The argument of an object rest element must be an Identifier + left = (properties[0] as t.RestElement).argument as t.Identifier; } yield { left: left as t.ObjectPattern, @@ -317,7 +325,6 @@ export function* transformPrivateKeyDestructuring( ), }; } else { - // @ts-expect-error left must not contain RestElement if restExcludingKeys is nullish yield { left, right }; } } else { @@ -341,12 +348,6 @@ export function* transformPrivateKeyDestructuring( switch (left.type) { case "ObjectPattern": { const { properties } = left; - // inherit the restExcludingKeys on the stack if we are at - // the first level, otherwise initialize a new restExcludingKeys - let nextRestExcludingKeys = restExcludingKeys; - if (!isFirst) { - nextRestExcludingKeys = initRestExcludingKeys(left); - } if (index > 0) { // properties[0, index) must not contain private keys const propertiesSlice = properties.slice(0, index); @@ -357,6 +358,11 @@ export function* transformPrivateKeyDestructuring( } if (index < properties.length - 1) { // for properties after `index`, push them to stack so we can process them later + // inherit the restExcludingKeys on the stack if we are at + // the first level, otherwise initialize a new restExcludingKeys + const nextRestExcludingKeys = isFirst + ? restExcludingKeys + : initRestExcludingKeys(left); if (nextRestExcludingKeys !== null) { nextRestExcludingKeys.push( ...growRestExcludingKeys( @@ -375,7 +381,8 @@ export function* transformPrivateKeyDestructuring( } // An object rest element must not contain a private key const property = properties[index] as t.ObjectProperty; - left = property.value as t.LVal; + // The value of ObjectProperty under ObjectPattern must be an LHS + left = property.value as LHS; const { key } = property; const computed = property.computed || From 828a9f09fba06202f4fdcec7d5fbfea8560702d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 29 Mar 2022 15:04:28 -0400 Subject: [PATCH 19/35] refactor: simplify growRestExcludingKeys --- .../src/util.ts | 35 ++++++++++--------- .../array-rest-destructuring-middle/exec.js | 10 ++++++ .../array-rest-destructuring-middle/input.js | 7 ++++ .../array-rest-destructuring-middle/output.js | 18 ++++++++++ 4 files changed, 54 insertions(+), 16 deletions(-) create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-destructuring-middle/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-destructuring-middle/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-destructuring-middle/output.js diff --git a/packages/babel-plugin-proposal-destructuring-private/src/util.ts b/packages/babel-plugin-proposal-destructuring-private/src/util.ts index 5ad74bc03109..e1842c06700b 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/util.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/util.ts @@ -34,7 +34,7 @@ function transformAssignmentPattern( ); } -function initRestExcludingKeys(pattern: t.LVal) { +function initRestExcludingKeys(pattern: t.LVal): ExcludingKey[] | null { if (pattern.type === "ObjectPattern") { const { properties } = pattern; if (properties[properties.length - 1].type === "RestElement") { @@ -45,22 +45,28 @@ function initRestExcludingKeys(pattern: t.LVal) { } /** - * grow restExcludingKeys on given properties. This routine mutates properties by + * grow `excludingKeys` from given properties. This routine mutates properties by * memoising the computed non-static keys. * + * @param {ExcludingKey[]} excludingKeys * @param {ObjectProperty[]} properties An array of object properties that should be excluded by rest element transform * @param {Scope} scope Where should we register the memoised id */ -function* growRestExcludingKeys(properties: ObjectProperty[], scope: Scope) { +function growRestExcludingKeys( + excludingKeys: ExcludingKey[], + properties: ObjectProperty[], + scope: Scope, +) { + if (excludingKeys === null) return; for (const property of properties) { const propertyKey = property.key; if (property.computed && !scope.isStatic(propertyKey)) { const tempId = scope.generateDeclaredUidIdentifier("m"); // @ts-expect-error A computed property key must not be a private name property.key = assignmentExpression("=", tempId, propertyKey); - yield { key: tempId, computed: true }; + excludingKeys.push({ key: tempId, computed: true }); } else if (propertyKey.type !== "PrivateName") { - yield property; + excludingKeys.push(property); } } } @@ -302,7 +308,7 @@ export function* transformPrivateKeyDestructuring( let { left, right } = item; const searchPrivateKey = privateKeyPathIterator(left).next(); if (searchPrivateKey.done) { - if (restExcludingKeys != null && restExcludingKeys.length > 0) { + if (restExcludingKeys?.length > 0) { // optimize out the rest element because `objectWithoutProperties` // always return a new object // `{ ...z } = babelHelpers.objectWithoutProperties(m, ["x"])` @@ -363,16 +369,13 @@ export function* transformPrivateKeyDestructuring( const nextRestExcludingKeys = isFirst ? restExcludingKeys : initRestExcludingKeys(left); - if (nextRestExcludingKeys !== null) { - nextRestExcludingKeys.push( - ...growRestExcludingKeys( - // @ts-expect-error properties[0, index] must not contain rest element - // because properties[index] contains a private key - properties.slice(0, index + 1), - scope, - ), - ); - } + growRestExcludingKeys( + nextRestExcludingKeys, + // @ts-expect-error properties[0, index] must not contain rest element + // because properties[index] contains a private key + properties.slice(0, index + 1), + scope, + ); stack.push({ left: objectPattern(properties.slice(index + 1)), right: cloneNode(right), diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-destructuring-middle/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-destructuring-middle/exec.js new file mode 100644 index 000000000000..6d1e6de5d111 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-destructuring-middle/exec.js @@ -0,0 +1,10 @@ +let x, y, z; +class C { + static #x; + static { + ([{ y }, { #x: x = y }, ...z] = [{ y: 1}, C]); + } +} + +expect(x).toBe(1); +expect(z).toStrictEqual([]); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-destructuring-middle/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-destructuring-middle/input.js new file mode 100644 index 000000000000..70615f84cae2 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-destructuring-middle/input.js @@ -0,0 +1,7 @@ +let x, y, z; +class C { + static #x; + static { + ([{ y }, { #x: x = y }, ...z] = [{ y: 1}, C]); + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-destructuring-middle/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-destructuring-middle/output.js new file mode 100644 index 000000000000..8b548adb7cc9 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-destructuring-middle/output.js @@ -0,0 +1,18 @@ +let x, y, z; + +class C {} + +var _x = { + writable: true, + value: void 0 +}; + +(() => { + var _m, _p, _p2, _m2; + + _m = [{ + y: 1 + }, C], [{ + y + }, _p, ..._p2] = _m, _m2 = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x), x = _m2 === void 0 ? y : _m2, z = _p2; +})(); From f8c059ea0af594a58e8c1283cb899e5f454b3376 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 29 Mar 2022 15:17:08 -0400 Subject: [PATCH 20/35] fix: catch param should be lowered to block scoped --- .../src/index.ts | 2 +- .../catch-param--es2015/no-shadowed-params/exec.js | 10 ++++++++++ .../catch-param--es2015/no-shadowed-params/input.js | 2 ++ .../catch-param--es2015/no-shadowed-params/output.js | 6 +++++- .../fixtures/catch-param/no-shadowed-params/exec.js | 10 ++++++++++ .../fixtures/catch-param/no-shadowed-params/input.js | 2 ++ .../fixtures/catch-param/no-shadowed-params/output.js | 6 +++++- 7 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/exec.js diff --git a/packages/babel-plugin-proposal-destructuring-private/src/index.ts b/packages/babel-plugin-proposal-destructuring-private/src/index.ts index 77b6b339e560..7b8108c0528a 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/index.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/index.ts @@ -71,7 +71,7 @@ export default declare(function ({ .get("body") .unshiftContainer( "body", - variableDeclaration("var", [variableDeclarator(node.param, ref)]), + variableDeclaration("let", [variableDeclarator(node.param, ref)]), ); node.param = cloneNode(ref); // the pattern will be handled by VariableDeclaration visitor. diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/exec.js new file mode 100644 index 000000000000..6b1df81d8ea2 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/exec.js @@ -0,0 +1,10 @@ +var x; +class C { + #x; + static { + x = "x"; + try { throw new C() } catch ({ #x: x }) { + } + } +} +expect(x).toBe("x"); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/input.js index e2ac65996b95..b8ba97ae407d 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/input.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/input.js @@ -1,6 +1,8 @@ +var x; class C { #x; static { + x = "x"; try { throw new C() } catch ({ #x: x }) { } } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/output.js index 23c3b28e7821..5688afc9281d 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/output.js @@ -1,3 +1,5 @@ +var x; + var _x = /*#__PURE__*/new WeakMap(); class C { @@ -11,9 +13,11 @@ class C { } (() => { + x = "x"; + try { throw new C(); } catch (_e) { - var x = babelHelpers.classPrivateFieldGet(_e, _x); + let x = babelHelpers.classPrivateFieldGet(_e, _x); } })(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/exec.js new file mode 100644 index 000000000000..6b1df81d8ea2 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/exec.js @@ -0,0 +1,10 @@ +var x; +class C { + #x; + static { + x = "x"; + try { throw new C() } catch ({ #x: x }) { + } + } +} +expect(x).toBe("x"); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/input.js index e2ac65996b95..b8ba97ae407d 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/input.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/input.js @@ -1,6 +1,8 @@ +var x; class C { #x; static { + x = "x"; try { throw new C() } catch ({ #x: x }) { } } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/output.js index a6e307b2cae2..45bc0a04f3ca 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/output.js @@ -1,10 +1,14 @@ +var x; + class C { #x; static { + x = "x"; + try { throw new C(); } catch (_e) { - var x = _e.#x; + let x = _e.#x; } } From 14d03091233f48934d005e159d897b51d4721db5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 29 Mar 2022 15:47:14 -0400 Subject: [PATCH 21/35] optimize: skip memoising right for ArrayPattern --- .../src/util.ts | 13 ++++++++++++- .../array-rest-destructuring-middle/output.js | 10 +++++----- .../assignment--es2015/array-rest-only/output.js | 4 ++-- .../assignment--es2015/array-rest/output.js | 4 ++-- .../member-expression/output.js | 4 ++-- .../nested-under-array-pattern/output.js | 4 ++-- .../array-rest-destructuring-middle/output.js | 10 +++++----- .../assignment/array-rest-only/output.js | 4 ++-- .../fixtures/assignment/array-rest/output.js | 4 ++-- .../assignment/member-expression/output.js | 4 ++-- .../nested-under-array-pattern/output.js | 4 ++-- .../array-rest-only/output.js | 9 ++++----- .../array-rest/output.js | 7 +++---- .../nested-under-array-pattern/output.js | 16 +++++++--------- .../array-rest-only/output.js | 9 ++++----- .../variable-declaration/array-rest/output.js | 7 +++---- .../nested-under-array-pattern/output.js | 16 +++++++--------- 17 files changed, 66 insertions(+), 63 deletions(-) diff --git a/packages/babel-plugin-proposal-destructuring-private/src/util.ts b/packages/babel-plugin-proposal-destructuring-private/src/util.ts index e1842c06700b..f3fcb2ad0627 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/util.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/util.ts @@ -274,6 +274,17 @@ type Item = { restExcludingKeys?: ExcludingKey[] | null; }; +function rightWillBeReferencedOnce(left: LHS) { + switch (left.type) { + // Skip memoising the right when left is an identifier or + // an array pattern + case "Identifier": + case "ArrayPattern": + return true; + default: + return false; + } +} /** * Transform private destructuring. It returns a generator * which yields a pair of transformed LHS and RHS, which can form VariableDeclaration or @@ -342,7 +353,7 @@ export function* transformPrivateKeyDestructuring( (index = indexPath.shift()) !== undefined || left.type === "AssignmentPattern" ) { - if (!scope.isStatic(right) && left.type !== "Identifier") { + if (!rightWillBeReferencedOnce(left) && !scope.isStatic(right)) { const tempId = scope.generateUidIdentifier("m"); if (isAssignment) { scope.push({ id: cloneNode(tempId) }); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-destructuring-middle/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-destructuring-middle/output.js index 8b548adb7cc9..15f6b3310491 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-destructuring-middle/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-destructuring-middle/output.js @@ -8,11 +8,11 @@ var _x = { }; (() => { - var _m, _p, _p2, _m2; + var _p, _p2, _m; - _m = [{ - y: 1 - }, C], [{ + [{ y - }, _p, ..._p2] = _m, _m2 = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x), x = _m2 === void 0 ? y : _m2, z = _p2; + }, _p, ..._p2] = [{ + y: 1 + }, C], _m = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x), x = _m === void 0 ? y : _m, z = _p2; })(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-only/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-only/output.js index 229a7504038c..59b36bdb85bf 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-only/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-only/output.js @@ -9,10 +9,10 @@ var _x = { }; (() => { - var _m, _p, _m2, _m3; + var _p, _m, _m2; var x, z; - _m = [C], [..._p] = _m, _m2 = _p[0], _m3 = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _x), x = _m3 === void 0 ? 1 : _m3, z = babelHelpers.objectWithoutProperties(_p, _excluded); + [..._p] = [C], _m = _p[0], _m2 = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x), x = _m2 === void 0 ? 1 : _m2, z = babelHelpers.objectWithoutProperties(_p, _excluded); result = { x, z diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/output.js index 25816df07323..837199e34665 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest/output.js @@ -8,7 +8,7 @@ var _x = { }; (() => { - var _m, _p, _p2, _m2; + var _p, _p2, _m; - _m = [C], [_p, ..._p2] = _m, _m2 = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x), x = _m2 === void 0 ? 1 : _m2, z = _p2; + [_p, ..._p2] = [C], _m = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x), x = _m === void 0 ? 1 : _m, z = _p2; })(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/output.js index 0a8f2fdf5106..37753eaca23e 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/member-expression/output.js @@ -16,8 +16,8 @@ var _z = { }; (() => { - var _m, _p, _p2; + var _p, _p2; let z; - _m = [0, C], [babelHelpers.classStaticPrivateFieldDestructureSet(C, C, _x).value, _p, ..._p2] = _m, x = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x), z = _p2; + [babelHelpers.classStaticPrivateFieldDestructureSet(C, C, _x).value, _p, ..._p2] = [0, C], x = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x), z = _p2; })(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/output.js index 1087c312042c..33b5a2e4846c 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/output.js @@ -19,8 +19,8 @@ var _z = { babelHelpers.defineProperty(C, "self", C); (() => { - var _m, _p, _p2, _p3, _m2, _m3, _p4, _m4, _m5; + var _p, _p2, _p3, _m, _p4, _m2, _m3; let x, y, z; - _m = [C, C], [_p, _p2,, _p3] = _m, _m2 = _p === void 0 ? C.self : _p, x = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _x), _m3 = babelHelpers.classStaticPrivateFieldSpecGet(_p2, C, _y), [, _p4] = _m3, _m4 = _p4 === void 0 ? C.self : _p4, _m5 = babelHelpers.classStaticPrivateFieldSpecGet(_m4, C, _z), y = _m5 === void 0 ? babelHelpers.classStaticPrivateMethodGet(C, C, _self).call(C) : _m5, z = _p3 === void 0 ? babelHelpers.classStaticPrivateFieldSpecGet(y, C, _y) : _p3; + [_p, _p2,, _p3] = [C, C], _m = _p === void 0 ? C.self : _p, x = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x), [, _p4] = babelHelpers.classStaticPrivateFieldSpecGet(_p2, C, _y), _m2 = _p4 === void 0 ? C.self : _p4, _m3 = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _z), y = _m3 === void 0 ? babelHelpers.classStaticPrivateMethodGet(C, C, _self).call(C) : _m3, z = _p3 === void 0 ? babelHelpers.classStaticPrivateFieldSpecGet(y, C, _y) : _p3; })(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-destructuring-middle/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-destructuring-middle/output.js index af1d0bf1a148..a95547193b0d 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-destructuring-middle/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-destructuring-middle/output.js @@ -3,12 +3,12 @@ let x, y, z; class C { static #x; static { - var _m, _p, _p2, _m2; + var _p, _p2, _m; - _m = [{ - y: 1 - }, C], [{ + [{ y - }, _p, ..._p2] = _m, _m2 = _p.#x, x = _m2 === void 0 ? y : _m2, z = _p2; + }, _p, ..._p2] = [{ + y: 1 + }, C], _m = _p.#x, x = _m === void 0 ? y : _m, z = _p2; } } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-only/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-only/output.js index 32ab0c98278b..2c48b1a27473 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-only/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-only/output.js @@ -4,10 +4,10 @@ let result; class C { static #x; static { - var _m, _p, _m2, _m3; + var _p, _m, _m2; var x, z; - _m = [C], [..._p] = _m, _m2 = _p[0], _m3 = _m2.#x, x = _m3 === void 0 ? 1 : _m3, z = babelHelpers.objectWithoutProperties(_p, _excluded); + [..._p] = [C], _m = _p[0], _m2 = _m.#x, x = _m2 === void 0 ? 1 : _m2, z = babelHelpers.objectWithoutProperties(_p, _excluded); result = { x, z diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/output.js index 1ca9c8ead1cf..50156224c564 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest/output.js @@ -3,8 +3,8 @@ let x, z; class C { static #x; static { - var _m, _p, _p2, _m2; + var _p, _p2, _m; - _m = [C], [_p, ..._p2] = _m, _m2 = _p.#x, x = _m2 === void 0 ? 1 : _m2, z = _p2; + [_p, ..._p2] = [C], _m = _p.#x, x = _m === void 0 ? 1 : _m, z = _p2; } } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/output.js index a7c239f9e544..738a12d73cae 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/member-expression/output.js @@ -5,9 +5,9 @@ class C { static #y; static #z; static { - var _m, _p, _p2; + var _p, _p2; let z; - _m = [0, C], [C.#x, _p, ..._p2] = _m, x = _p.#x, z = _p2; + [C.#x, _p, ..._p2] = [0, C], x = _p.#x, z = _p2; } } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/output.js index 7cde39fb5f0f..f9c62bbeb4f6 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/output.js @@ -9,9 +9,9 @@ class C { } static { - var _m, _p, _p2, _p3, _m2, _m3, _p4, _m4, _m5; + var _p, _p2, _p3, _m, _p4, _m2, _m3; let x, y, z; - _m = [this, this], [_p, _p2,, _p3] = _m, _m2 = _p === void 0 ? C.self : _p, x = _m2.#x, _m3 = _p2.#y, [, _p4] = _m3, _m4 = _p4 === void 0 ? C.self : _p4, _m5 = _m4.#z, y = _m5 === void 0 ? C.#self() : _m5, z = _p3 === void 0 ? y.#y : _p3; + [_p, _p2,, _p3] = [this, this], _m = _p === void 0 ? C.self : _p, x = _m.#x, [, _p4] = _p2.#y, _m2 = _p4 === void 0 ? C.self : _p4, _m3 = _m2.#z, y = _m3 === void 0 ? C.#self() : _m3, z = _p3 === void 0 ? y.#y : _p3; } } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/output.js index 689af34d23ad..950a15a5a50f 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/output.js @@ -9,11 +9,10 @@ var _x = { }; (() => { - var _m = [C], - [..._p] = _m, - _m2 = _p[0], - _m3 = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _x), - x = _m3 === void 0 ? 1 : _m3, + var [..._p] = [C], + _m = _p[0], + _m2 = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x), + x = _m2 === void 0 ? 1 : _m2, z = babelHelpers.objectWithoutProperties(_p, _excluded); result = { diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/output.js index 8e4a9f07f94e..8c9aeb90baa6 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/output.js @@ -6,9 +6,8 @@ var _x = { }; (() => { - var _m = [C], - [_p, ..._p2] = _m, - _m2 = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x), - x = _m2 === void 0 ? 1 : _m2, + var [_p, ..._p2] = [C], + _m = babelHelpers.classStaticPrivateFieldSpecGet(_p, C, _x), + x = _m === void 0 ? 1 : _m, z = _p2; })(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/output.js index 0f7f7a4f8f6b..b8a83d383ded 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/output.js @@ -19,14 +19,12 @@ var _z = { babelHelpers.defineProperty(C, "self", C); (() => { - var _m = [C, C], - [_p, _p2,, _p3] = _m, - _m2 = _p === void 0 ? C.self : _p, - x = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _x), - _m3 = babelHelpers.classStaticPrivateFieldSpecGet(_p2, C, _y), - [, _p4] = _m3, - _m4 = _p4 === void 0 ? C.self : _p4, - _m5 = babelHelpers.classStaticPrivateFieldSpecGet(_m4, C, _z), - y = _m5 === void 0 ? babelHelpers.classStaticPrivateMethodGet(C, C, _self).call(C) : _m5, + var [_p, _p2,, _p3] = [C, C], + _m = _p === void 0 ? C.self : _p, + x = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x), + [, _p4] = babelHelpers.classStaticPrivateFieldSpecGet(_p2, C, _y), + _m2 = _p4 === void 0 ? C.self : _p4, + _m3 = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _z), + y = _m3 === void 0 ? babelHelpers.classStaticPrivateMethodGet(C, C, _self).call(C) : _m3, z = _p3 === void 0 ? babelHelpers.classStaticPrivateFieldSpecGet(y, C, _y) : _p3; })(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/output.js index 37ad571bcd4c..4f9d2a55f976 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/output.js @@ -4,11 +4,10 @@ let result; class C { static #x; static { - var _m = [C], - [..._p] = _m, - _m2 = _p[0], - _m3 = _m2.#x, - x = _m3 === void 0 ? 1 : _m3, + var [..._p] = [C], + _m = _p[0], + _m2 = _m.#x, + x = _m2 === void 0 ? 1 : _m2, z = babelHelpers.objectWithoutProperties(_p, _excluded); result = { x, diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/output.js index 9c642bfc8702..203a6d1b1fc3 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/output.js @@ -1,10 +1,9 @@ class C { static #x; static { - var _m = [C], - [_p, ..._p2] = _m, - _m2 = _p.#x, - x = _m2 === void 0 ? 1 : _m2, + var [_p, ..._p2] = [C], + _m = _p.#x, + x = _m === void 0 ? 1 : _m, z = _p2; } } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/output.js index f487f9d2b173..f9fbd0b90bc0 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/output.js @@ -9,15 +9,13 @@ class C { } static { - var _m = [this, this], - [_p, _p2,, _p3] = _m, - _m2 = _p === void 0 ? C.self : _p, - x = _m2.#x, - _m3 = _p2.#y, - [, _p4] = _m3, - _m4 = _p4 === void 0 ? C.self : _p4, - _m5 = _m4.#z, - y = _m5 === void 0 ? C.#self() : _m5, + var [_p, _p2,, _p3] = [this, this], + _m = _p === void 0 ? C.self : _p, + x = _m.#x, + [, _p4] = _p2.#y, + _m2 = _p4 === void 0 ? C.self : _p4, + _m3 = _m2.#z, + y = _m3 === void 0 ? C.#self() : _m3, z = _p3 === void 0 ? y.#y : _p3; } From 73447bdcdfd29d0c0a9e14d63a14d474d757009a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 29 Mar 2022 18:13:12 -0400 Subject: [PATCH 22/35] optimize: don't memoise non-root right when there is only one destructuring in the left --- .../src/index.ts | 33 +++++++++++-------- .../src/util.ts | 13 +++++++- .../array-rest-only/output.js | 4 +-- .../nested-under-array-pattern/output.js | 4 +-- .../under-param-initializer/output.js | 4 +-- .../assignment/array-rest-only/output.js | 4 +-- .../nested-under-array-pattern/output.js | 4 +-- .../under-param-initializer/output.js | 4 +-- .../lhs-with-assign/output.js | 2 +- .../fixtures/for-init--es2015/lhs/output.js | 2 +- .../output.js | 4 +-- .../for-init/lhs-with-assign/output.js | 2 +- .../test/fixtures/for-init/lhs/output.js | 2 +- .../output.js | 4 +-- .../array-rest-only/output.js | 5 ++- .../nested-under-array-pattern/output.js | 8 ++--- .../non-identifier-keys/output.js | 12 +++---- .../array-rest-only/output.js | 5 ++- .../nested-under-array-pattern/output.js | 9 ++--- .../non-identifier-keys/output.js | 12 +++---- 20 files changed, 69 insertions(+), 68 deletions(-) diff --git a/packages/babel-plugin-proposal-destructuring-private/src/index.ts b/packages/babel-plugin-proposal-destructuring-private/src/index.ts index 7b8108c0528a..ec6d1d4819c6 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/index.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/index.ts @@ -125,6 +125,7 @@ export default declare(function ({ declarator.init, scope, /* isAssignment */ false, + /* shouldPreserveCompletion */ false, name => state.addHelper(name), objectRestNoSymbols, /* useBuiltIns */ true, @@ -140,12 +141,16 @@ export default declare(function ({ const { node, scope, parent } = path; if (!hasPrivateKeys(node.left)) return; const assignments = []; + const shouldPreserveCompletion = + (!isExpressionStatement(parent) && !isSequenceExpression(parent)) || + path.isCompletionRecord(); for (const { left, right } of transformPrivateKeyDestructuring( // @ts-expect-error The left of an assignment expression must not be a RestElement node.left, node.right, scope, /* isAssignment */ true, + shouldPreserveCompletion, name => state.addHelper(name), objectRestNoSymbols, /* useBuiltIns */ true, @@ -153,25 +158,25 @@ export default declare(function ({ assignments.push(assignmentExpression("=", left, right)); } // preserve completion record - if ( - (!isExpressionStatement(parent) && !isSequenceExpression(parent)) || - path.isCompletionRecord() - ) { - const { left } = assignments[0]; - if (scope.isStatic(node.right)) { + if (shouldPreserveCompletion) { + const { left, right } = assignments[0]; + // If node.right is right and left is an identifier, then the left is an effectively-constant memoised id + if (isIdentifier(left) && right === node.right) { + if ( + !isIdentifier(assignments[assignments.length - 1].right, { + name: left.name, + }) + ) { + // If the last assignment does not end with left, then we push `left` as the completion value + assignments.push(cloneNode(left)); + } + // do nothing as `left` is already at the end of assignments + } else { const tempId = scope.generateDeclaredUidIdentifier("m"); assignments.unshift( assignmentExpression("=", tempId, cloneNode(node.right)), ); assignments.push(cloneNode(tempId)); - } else if ( - !isIdentifier(assignments[assignments.length - 1].right, { - name: left.name, - }) - ) { - // If node.right is non-static and then the left is an effectively-constant memoised id - // If the last assignment does not end with left, that we can safely reuse `left` as the completion value - assignments.push(cloneNode(left)); } } diff --git a/packages/babel-plugin-proposal-destructuring-private/src/util.ts b/packages/babel-plugin-proposal-destructuring-private/src/util.ts index f3fcb2ad0627..e7b13fafe105 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/util.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/util.ts @@ -281,6 +281,8 @@ function rightWillBeReferencedOnce(left: LHS) { case "Identifier": case "ArrayPattern": return true; + case "ObjectPattern": + return left.properties.length === 1; default: return false; } @@ -302,11 +304,13 @@ export function* transformPrivateKeyDestructuring( right: t.Expression, scope: Scope, isAssignment: boolean, + shouldPreserveCompletion: boolean, addHelper: File["addHelper"], objectRestNoSymbols: boolean, useBuiltIns: boolean, ): Generator { const stack: Item[] = []; + const rootRight = right; // The stack holds patterns that we don't known whether they contain private key stack.push({ left, @@ -353,7 +357,14 @@ export function* transformPrivateKeyDestructuring( (index = indexPath.shift()) !== undefined || left.type === "AssignmentPattern" ) { - if (!rightWillBeReferencedOnce(left) && !scope.isStatic(right)) { + const isRightSafeToReuse = + // If we should preserve completion and the right is the rootRight, then the + // right is NOT safe to reuse because we will insert a new memoising statement + // in the AssignmentExpression visitor, which causes right to be referenced more + // than once + !(shouldPreserveCompletion && right === rootRight) && + (rightWillBeReferencedOnce(left) || scope.isStatic(right)); + if (!isRightSafeToReuse) { const tempId = scope.generateUidIdentifier("m"); if (isAssignment) { scope.push({ id: cloneNode(tempId) }); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-only/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-only/output.js index 59b36bdb85bf..2691e38a1b03 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-only/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/array-rest-only/output.js @@ -9,10 +9,10 @@ var _x = { }; (() => { - var _p, _m, _m2; + var _p, _m; var x, z; - [..._p] = [C], _m = _p[0], _m2 = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x), x = _m2 === void 0 ? 1 : _m2, z = babelHelpers.objectWithoutProperties(_p, _excluded); + [..._p] = [C], _m = babelHelpers.classStaticPrivateFieldSpecGet(_p[0], C, _x), x = _m === void 0 ? 1 : _m, z = babelHelpers.objectWithoutProperties(_p, _excluded); result = { x, z diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/output.js index 33b5a2e4846c..3454fb0bf7dd 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/nested-under-array-pattern/output.js @@ -19,8 +19,8 @@ var _z = { babelHelpers.defineProperty(C, "self", C); (() => { - var _p, _p2, _p3, _m, _p4, _m2, _m3; + var _p, _p2, _p3, _p4, _m; let x, y, z; - [_p, _p2,, _p3] = [C, C], _m = _p === void 0 ? C.self : _p, x = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x), [, _p4] = babelHelpers.classStaticPrivateFieldSpecGet(_p2, C, _y), _m2 = _p4 === void 0 ? C.self : _p4, _m3 = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _z), y = _m3 === void 0 ? babelHelpers.classStaticPrivateMethodGet(C, C, _self).call(C) : _m3, z = _p3 === void 0 ? babelHelpers.classStaticPrivateFieldSpecGet(y, C, _y) : _p3; + [_p, _p2,, _p3] = [C, C], x = babelHelpers.classStaticPrivateFieldSpecGet(_p === void 0 ? C.self : _p, C, _x), [, _p4] = babelHelpers.classStaticPrivateFieldSpecGet(_p2, C, _y), _m = babelHelpers.classStaticPrivateFieldSpecGet(_p4 === void 0 ? C.self : _p4, C, _z), y = _m === void 0 ? babelHelpers.classStaticPrivateMethodGet(C, C, _self).call(C) : _m, z = _p3 === void 0 ? babelHelpers.classStaticPrivateFieldSpecGet(y, C, _y) : _p3; })(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/output.js index 829bd11aa63a..d0f627623477 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/under-param-initializer/output.js @@ -5,7 +5,7 @@ let a; class C { static m(r = (_m2 = C, ({ a - } = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x)), _m2)) {} + } = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _x)), _m2)) {} } @@ -24,7 +24,7 @@ var _x = { (function f(r = (_m = C, ({ b - } = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x)), _m)) {})(); + } = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x)), _m)) {})(); })(); C.m(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-only/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-only/output.js index 2c48b1a27473..9eae325d1e7e 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-only/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/array-rest-only/output.js @@ -4,10 +4,10 @@ let result; class C { static #x; static { - var _p, _m, _m2; + var _p, _m; var x, z; - [..._p] = [C], _m = _p[0], _m2 = _m.#x, x = _m2 === void 0 ? 1 : _m2, z = babelHelpers.objectWithoutProperties(_p, _excluded); + [..._p] = [C], _m = _p[0].#x, x = _m === void 0 ? 1 : _m, z = babelHelpers.objectWithoutProperties(_p, _excluded); result = { x, z diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/output.js index f9c62bbeb4f6..3d9e614af2a0 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/nested-under-array-pattern/output.js @@ -9,9 +9,9 @@ class C { } static { - var _p, _p2, _p3, _m, _p4, _m2, _m3; + var _p, _p2, _p3, _p4, _m; let x, y, z; - [_p, _p2,, _p3] = [this, this], _m = _p === void 0 ? C.self : _p, x = _m.#x, [, _p4] = _p2.#y, _m2 = _p4 === void 0 ? C.self : _p4, _m3 = _m2.#z, y = _m3 === void 0 ? C.#self() : _m3, z = _p3 === void 0 ? y.#y : _p3; + [_p, _p2,, _p3] = [this, this], x = (_p === void 0 ? C.self : _p).#x, [, _p4] = _p2.#y, _m = (_p4 === void 0 ? C.self : _p4).#z, y = _m === void 0 ? C.#self() : _m, z = _p3 === void 0 ? y.#y : _p3; } } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/output.js index 21c71cdb74c7..e666e783149e 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/under-param-initializer/output.js @@ -14,13 +14,13 @@ class C { (function f(r = (_m = C, ({ b - } = C.#x), _m)) {})(); + } = _m.#x), _m)) {})(); } static m(r = (_m2 = C, ({ a - } = C.#x), _m2)) {} + } = _m2.#x), _m2)) {} } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/output.js index a1dbc5da395e..ba1bd3c17a15 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs-with-assign/output.js @@ -8,7 +8,7 @@ var _x = { (() => { let x, y; - for (_m = (_m2 = C, y = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), _m2), x = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x), _m;;) { + for (_m = (_m2 = C, y = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _x), _m2), x = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x), _m;;) { var _m, _m2; break; diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs/output.js index 48d4b0febd9b..12bf9e55f321 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/lhs/output.js @@ -11,7 +11,7 @@ class C { } (() => { - for (_m = C, x = babelHelpers.classPrivateFieldGet(C, _x), _m;;) { + for (_m = C, x = babelHelpers.classPrivateFieldGet(_m, _x), _m;;) { var _m; break; diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/output.js index bef362866c61..89c4773d5c46 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init--es2015/variable-declaration-with-assign/output.js @@ -8,8 +8,8 @@ var _x = { (() => { let y; - for (let _m = (_m2 = C, y = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), _m2), x = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x);;) { - var _m2; + for (let x = babelHelpers.classStaticPrivateFieldSpecGet((_m = C, y = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x), _m), C, _x);;) { + var _m; break; } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/output.js index e52600816752..fb12bc77834a 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs-with-assign/output.js @@ -3,7 +3,7 @@ class C { static { let x, y; - for (_m = (_m2 = C, y = C.#x, _m2), x = _m.#x, _m;;) { + for (_m = (_m2 = C, y = _m2.#x, _m2), x = _m.#x, _m;;) { var _m, _m2; break; diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs/output.js index e4f394a6488b..9ef3ef20cb47 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/lhs/output.js @@ -1,7 +1,7 @@ class C { #x; static { - for (_m = this, x = this.#x, _m;;) { + for (_m = this, x = _m.#x, _m;;) { var _m; break; diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/output.js index 29bb33212367..df0a155d4efa 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/for-init/variable-declaration-with-assign/output.js @@ -3,8 +3,8 @@ class C { static { let y; - for (let _m = (_m2 = C, y = C.#x, _m2), x = _m.#x;;) { - var _m2; + for (let x = (_m = C, y = _m.#x, _m).#x;;) { + var _m; break; } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/output.js index 950a15a5a50f..34b1fbe5524c 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/output.js @@ -10,9 +10,8 @@ var _x = { (() => { var [..._p] = [C], - _m = _p[0], - _m2 = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x), - x = _m2 === void 0 ? 1 : _m2, + _m = babelHelpers.classStaticPrivateFieldSpecGet(_p[0], C, _x), + x = _m === void 0 ? 1 : _m, z = babelHelpers.objectWithoutProperties(_p, _excluded); result = { diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/output.js index b8a83d383ded..6d62674ff6ca 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/output.js @@ -20,11 +20,9 @@ babelHelpers.defineProperty(C, "self", C); (() => { var [_p, _p2,, _p3] = [C, C], - _m = _p === void 0 ? C.self : _p, - x = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x), + x = babelHelpers.classStaticPrivateFieldSpecGet(_p === void 0 ? C.self : _p, C, _x), [, _p4] = babelHelpers.classStaticPrivateFieldSpecGet(_p2, C, _y), - _m2 = _p4 === void 0 ? C.self : _p4, - _m3 = babelHelpers.classStaticPrivateFieldSpecGet(_m2, C, _z), - y = _m3 === void 0 ? babelHelpers.classStaticPrivateMethodGet(C, C, _self).call(C) : _m3, + _m = babelHelpers.classStaticPrivateFieldSpecGet(_p4 === void 0 ? C.self : _p4, C, _z), + y = _m === void 0 ? babelHelpers.classStaticPrivateMethodGet(C, C, _self).call(C) : _m, z = _p3 === void 0 ? babelHelpers.classStaticPrivateFieldSpecGet(y, C, _y) : _p3; })(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/output.js index 5c6376193ae1..0579a8964deb 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/output.js @@ -2,13 +2,9 @@ class C { static #x; static { var _m = [C, C, C, C], - _m2 = _m["0"], - w = _m2.#x, - _m3 = _m[1], - x = _m3.#x, - _m4 = _m[2n], - y = _m4.#x, - _m5 = _m[3m], - z = _m5.#x; + w = _m["0"].#x, + x = _m[1].#x, + y = _m[2n].#x, + z = _m[3m].#x; } } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/output.js index 4f9d2a55f976..7930e97e528f 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/output.js @@ -5,9 +5,8 @@ class C { static #x; static { var [..._p] = [C], - _m = _p[0], - _m2 = _m.#x, - x = _m2 === void 0 ? 1 : _m2, + _m = _p[0].#x, + x = _m === void 0 ? 1 : _m, z = babelHelpers.objectWithoutProperties(_p, _excluded); result = { x, diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/output.js index f9fbd0b90bc0..634cc2709bd9 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/output.js @@ -10,13 +10,10 @@ class C { static { var [_p, _p2,, _p3] = [this, this], - _m = _p === void 0 ? C.self : _p, - x = _m.#x, + x = (_p === void 0 ? C.self : _p).#x, [, _p4] = _p2.#y, - _m2 = _p4 === void 0 ? C.self : _p4, - _m3 = _m2.#z, - y = _m3 === void 0 ? C.#self() : _m3, + _m = (_p4 === void 0 ? C.self : _p4).#z, + y = _m === void 0 ? C.#self() : _m, z = _p3 === void 0 ? y.#y : _p3; - } } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/output.js index 5c6376193ae1..0579a8964deb 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/output.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/output.js @@ -2,13 +2,9 @@ class C { static #x; static { var _m = [C, C, C, C], - _m2 = _m["0"], - w = _m2.#x, - _m3 = _m[1], - x = _m3.#x, - _m4 = _m[2n], - y = _m4.#x, - _m5 = _m[3m], - z = _m5.#x; + w = _m["0"].#x, + x = _m[1].#x, + y = _m[2n].#x, + z = _m[3m].#x; } } From 3a5848c30b5eac740c2bc6ee80d78bbae749918d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 29 Mar 2022 19:35:44 -0400 Subject: [PATCH 23/35] fix: mark the last statement in do block as completion --- .../src/index.ts | 7 +++---- .../completion-do-expression/exec.js | 9 +++++++++ .../completion-do-expression/input.js | 8 ++++++++ .../completion-do-expression/options.json | 10 ++++++++++ .../completion-do-expression/output.js | 15 +++++++++++++++ .../assignment/completion-do-expression/exec.js | 9 +++++++++ .../assignment/completion-do-expression/input.js | 8 ++++++++ .../completion-do-expression/options.json | 4 ++++ .../assignment/completion-do-expression/output.js | 11 +++++++++++ packages/babel-traverse/src/path/introspection.ts | 6 +++++- 10 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion-do-expression/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion-do-expression/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion-do-expression/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion-do-expression/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion-do-expression/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion-do-expression/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion-do-expression/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion-do-expression/output.js diff --git a/packages/babel-plugin-proposal-destructuring-private/src/index.ts b/packages/babel-plugin-proposal-destructuring-private/src/index.ts index ec6d1d4819c6..21f6b8d0f25e 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/index.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/index.ts @@ -41,20 +41,19 @@ export default declare(function ({ Function(path) { // (b, { #x: x } = I) => body // transforms to: - // (p1, p2) => { var b = p1, { #x: x } = p2 === undefined ? I : p2; body; } + // (b, p1) => { var { #x: x } = p1 === undefined ? I : p1; body; } const index = path.node.params.findIndex(param => hasPrivateKeys(param)); if (index === -1) return; // wrap function body within IIFE if any param is shadowed convertFunctionParams(path, ignoreFunctionLength, () => false, false); + // invariant: path.body is always a BlockStatement after `convertFunctionParams` const { node, scope } = path; const params = node.params; const paramsAfterIndex = params.splice(index); const { params: transformedParams, variableDeclaration } = buildVariableDeclarationFromParams(paramsAfterIndex, scope); - path - .get("body") // invariant: path.body is always a BlockStatement - .unshiftContainer("body", variableDeclaration); + path.get("body").unshiftContainer("body", variableDeclaration); params.push(...transformedParams); scope.crawl(); // the pattern will be handled by VariableDeclaration visitor. diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion-do-expression/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion-do-expression/exec.js new file mode 100644 index 000000000000..4e8130dbed03 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion-do-expression/exec.js @@ -0,0 +1,9 @@ +var result; +class C { + static #x; + static { + var x; + result = do { ({#x: x = 2} = C); } + } +} +expect(result).toBe(C); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion-do-expression/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion-do-expression/input.js new file mode 100644 index 000000000000..a929a603b588 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion-do-expression/input.js @@ -0,0 +1,8 @@ +var result; +class C { + static #x; + static { + var x; + result = do { ({#x: x = 2} = C); } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion-do-expression/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion-do-expression/options.json new file mode 100644 index 000000000000..637f74481731 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion-do-expression/options.json @@ -0,0 +1,10 @@ +{ + "plugins": [ + "proposal-do-expressions", + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties", + "proposal-private-methods", + ["proposal-object-rest-spread", { "useBuiltIns": true }] + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion-do-expression/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion-do-expression/output.js new file mode 100644 index 000000000000..6820a344d940 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/completion-do-expression/output.js @@ -0,0 +1,15 @@ +var result; + +class C {} + +var _x = { + writable: true, + value: void 0 +}; + +(() => { + var _m, _m2; + + var x; + result = (_m = C, _m2 = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x), x = _m2 === void 0 ? 2 : _m2, _m); +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion-do-expression/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion-do-expression/exec.js new file mode 100644 index 000000000000..4e8130dbed03 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion-do-expression/exec.js @@ -0,0 +1,9 @@ +var result; +class C { + static #x; + static { + var x; + result = do { ({#x: x = 2} = C); } + } +} +expect(result).toBe(C); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion-do-expression/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion-do-expression/input.js new file mode 100644 index 000000000000..a929a603b588 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion-do-expression/input.js @@ -0,0 +1,8 @@ +var result; +class C { + static #x; + static { + var x; + result = do { ({#x: x = 2} = C); } + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion-do-expression/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion-do-expression/options.json new file mode 100644 index 000000000000..fd0b65b50e67 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion-do-expression/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-do-expressions", "proposal-destructuring-private"], + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion-do-expression/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion-do-expression/output.js new file mode 100644 index 000000000000..d32ee58d5442 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/completion-do-expression/output.js @@ -0,0 +1,11 @@ +var result; + +class C { + static #x; + static { + var _m, _m2; + + var x; + result = (_m = C, _m2 = _m.#x, x = _m2 === void 0 ? 2 : _m2, _m); + } +} diff --git a/packages/babel-traverse/src/path/introspection.ts b/packages/babel-traverse/src/path/introspection.ts index 7680d6b08b91..e55c161f3ef8 100644 --- a/packages/babel-traverse/src/path/introspection.ts +++ b/packages/babel-traverse/src/path/introspection.ts @@ -149,7 +149,11 @@ export function isCompletionRecord( if (Array.isArray(container) && path.key !== container.length - 1) { return false; } - } while ((path = path.parentPath) && !path.isProgram()); + } while ( + (path = path.parentPath) && + !path.isProgram() && + !path.isDoExpression() + ); return true; } From d2b6815d9f885b593465cdf1e32b5c7305945596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 4 Apr 2022 10:36:36 -0400 Subject: [PATCH 24/35] fix: preserve function length when lowering params --- .../src/index.ts | 28 +++++++++++++++++-- .../function-params/input.js | 7 +++++ .../function-params/output.js | 26 +++++++++++++++++ .../options.json | 12 ++++++++ .../function-length/exec.js | 12 ++++++-- .../function-length/input.js | 7 +++++ .../function-length/output.js | 26 +++++++++++++++++ .../function-params/function-length/exec.js | 12 ++++++-- .../function-params/function-length/input.js | 7 +++++ .../function-params/function-length/output.js | 23 +++++++++++++++ 10 files changed, 151 insertions(+), 9 deletions(-) create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-ignoreFunctionLength/function-params/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-ignoreFunctionLength/function-params/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-ignoreFunctionLength/options.json create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/function-length/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/function-length/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/output.js diff --git a/packages/babel-plugin-proposal-destructuring-private/src/index.ts b/packages/babel-plugin-proposal-destructuring-private/src/index.ts index 21f6b8d0f25e..4779bdcef4af 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/index.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/index.ts @@ -15,6 +15,7 @@ import { types as t } from "@babel/core"; const { assignmentExpression, + assignmentPattern, cloneNode, expressionStatement, isExpressionStatement, @@ -42,19 +43,35 @@ export default declare(function ({ // (b, { #x: x } = I) => body // transforms to: // (b, p1) => { var { #x: x } = p1 === undefined ? I : p1; body; } - const index = path.node.params.findIndex(param => hasPrivateKeys(param)); - if (index === -1) return; + const firstPrivateIndex = path.node.params.findIndex(param => + hasPrivateKeys(param), + ); + if (firstPrivateIndex === -1) return; // wrap function body within IIFE if any param is shadowed convertFunctionParams(path, ignoreFunctionLength, () => false, false); // invariant: path.body is always a BlockStatement after `convertFunctionParams` const { node, scope } = path; const params = node.params; - const paramsAfterIndex = params.splice(index); + const firstAssignmentPatternIndex = ignoreFunctionLength + ? -1 + : params.findIndex(param => param.type === "AssignmentPattern"); + const paramsAfterIndex = params.splice(firstPrivateIndex); const { params: transformedParams, variableDeclaration } = buildVariableDeclarationFromParams(paramsAfterIndex, scope); path.get("body").unshiftContainer("body", variableDeclaration); params.push(...transformedParams); + // preserve function.length + // (b, p1) => {} + // transforms to + // (b, p1 = void 0) => {} + if (firstAssignmentPatternIndex >= firstPrivateIndex) { + params[firstAssignmentPatternIndex] = assignmentPattern( + // @ts-ignore The transformed assignment pattern must not be a RestElement + params[firstAssignmentPatternIndex], + scope.buildUndefinedNode(), + ); + } scope.crawl(); // the pattern will be handled by VariableDeclaration visitor. }, @@ -84,6 +101,11 @@ export default declare(function ({ // for (const { #x: x } of cls) body; // transforms to: // for (const ref of cls) { const { #x: x } = ref; body; } + // todo: the transform here assumes that any expression within + // the destructuring pattern (`{ #x: x }`), when evluated, do not interfere + // with the iterator of cls. Otherwise we have to pause the iterator and + // interleave the expressions. + // See also https://gist.github.com/nicolo-ribaudo/f8ac7916f89450f2ead77d99855b2098 const temp = scope.generateUidIdentifier("ref"); node.left = variableDeclaration(left.kind, [ variableDeclarator(temp, null), diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-ignoreFunctionLength/function-params/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-ignoreFunctionLength/function-params/input.js new file mode 100644 index 000000000000..ab2d2de13412 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-ignoreFunctionLength/function-params/input.js @@ -0,0 +1,7 @@ +class C { + static #x; + static 0(...[...{0: { #x: x }}]) {} + static 1(a, b = 1, { #x: x }, ...c) {} + static 2(a, b, { #x: x } = C) {} + static 3(a, b, { #x: x }, c = 1) {} +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-ignoreFunctionLength/function-params/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-ignoreFunctionLength/function-params/output.js new file mode 100644 index 000000000000..9db270ef7691 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-ignoreFunctionLength/function-params/output.js @@ -0,0 +1,26 @@ +class C { + static 0(..._p) { + var [..._p2] = _p, + x = babelHelpers.classStaticPrivateFieldSpecGet(_p2[0], C, _x); + } + + static 1(a, b = 1, _p3, ..._p4) { + var x = babelHelpers.classStaticPrivateFieldSpecGet(_p3, C, _x), + c = _p4; + } + + static 2(a, b, _p5) { + var x = babelHelpers.classStaticPrivateFieldSpecGet(_p5 === void 0 ? C : _p5, C, _x); + } + + static 3(a, b, _p6, _p7) { + var x = babelHelpers.classStaticPrivateFieldSpecGet(_p6, C, _x), + c = _p7 === void 0 ? 1 : _p7; + } + +} + +var _x = { + writable: true, + value: void 0 +}; diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-ignoreFunctionLength/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-ignoreFunctionLength/options.json new file mode 100644 index 000000000000..6101c1836f44 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assumption-ignoreFunctionLength/options.json @@ -0,0 +1,12 @@ +{ + "assumptions": { + "ignoreFunctionLength": true + }, + "plugins": [ + "proposal-destructuring-private", + "proposal-class-static-block", + "proposal-class-properties", + "proposal-private-methods", + ["proposal-object-rest-spread", { "useBuiltIns": true }] + ] +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/function-length/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/function-length/exec.js index 6ad25f261e99..2f89b81049d4 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/function-length/exec.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/function-length/exec.js @@ -1,5 +1,11 @@ class C { - #x; - static m(a, { #x: x }, ...b) {} + static #x; + static 0(...[...{0: { #x: x }}]) {} + static 1(a, b = 1, { #x: x }, ...c) {} + static 2(a, b, { #x: x } = C) {} + static 3(a, b, { #x: x }, c = 1) {} } -expect(C.m.length).toBe(2) +expect(C[0].length).toBe(0); +expect(C[1].length).toBe(1); +expect(C[2].length).toBe(2); +expect(C[3].length).toBe(3); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/function-length/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/function-length/input.js new file mode 100644 index 000000000000..ab2d2de13412 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/function-length/input.js @@ -0,0 +1,7 @@ +class C { + static #x; + static 0(...[...{0: { #x: x }}]) {} + static 1(a, b = 1, { #x: x }, ...c) {} + static 2(a, b, { #x: x } = C) {} + static 3(a, b, { #x: x }, c = 1) {} +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/function-length/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/function-length/output.js new file mode 100644 index 000000000000..7d3cfbe5de7c --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/function-length/output.js @@ -0,0 +1,26 @@ +class C { + static 0(..._p) { + var [..._p2] = _p, + x = babelHelpers.classStaticPrivateFieldSpecGet(_p2[0], C, _x); + } + + static 1(a, b = 1, _p3, ..._p4) { + var x = babelHelpers.classStaticPrivateFieldSpecGet(_p3, C, _x), + c = _p4; + } + + static 2(a, b, _p5 = void 0) { + var x = babelHelpers.classStaticPrivateFieldSpecGet(_p5 === void 0 ? C : _p5, C, _x); + } + + static 3(a, b, _p6, _p7 = void 0) { + var x = babelHelpers.classStaticPrivateFieldSpecGet(_p6, C, _x), + c = _p7 === void 0 ? 1 : _p7; + } + +} + +var _x = { + writable: true, + value: void 0 +}; diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/exec.js index 6ad25f261e99..2f89b81049d4 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/exec.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/exec.js @@ -1,5 +1,11 @@ class C { - #x; - static m(a, { #x: x }, ...b) {} + static #x; + static 0(...[...{0: { #x: x }}]) {} + static 1(a, b = 1, { #x: x }, ...c) {} + static 2(a, b, { #x: x } = C) {} + static 3(a, b, { #x: x }, c = 1) {} } -expect(C.m.length).toBe(2) +expect(C[0].length).toBe(0); +expect(C[1].length).toBe(1); +expect(C[2].length).toBe(2); +expect(C[3].length).toBe(3); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/input.js new file mode 100644 index 000000000000..ab2d2de13412 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/input.js @@ -0,0 +1,7 @@ +class C { + static #x; + static 0(...[...{0: { #x: x }}]) {} + static 1(a, b = 1, { #x: x }, ...c) {} + static 2(a, b, { #x: x } = C) {} + static 3(a, b, { #x: x }, c = 1) {} +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/output.js new file mode 100644 index 000000000000..8fdd49231735 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/output.js @@ -0,0 +1,23 @@ +class C { + static #x; + + static 0(..._p) { + var [..._p2] = _p, + x = _p2[0].#x; + } + + static 1(a, b = 1, _p3, ..._p4) { + var x = _p3.#x, + c = _p4; + } + + static 2(a, b, _p5 = void 0) { + var x = (_p5 === void 0 ? C : _p5).#x; + } + + static 3(a, b, _p6, _p7 = void 0) { + var x = _p6.#x, + c = _p7 === void 0 ? 1 : _p7; + } + +} From b5768c125bc100db9de06e19aacf9f22eba7e7b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 11 Apr 2022 15:35:27 -0400 Subject: [PATCH 25/35] test: more stringent ordering test --- .../src/util.ts | 1 + .../array-pattern-with-rest/exec.js | 24 ++++++++++----- .../ordering--es2015/array-pattern/exec.js | 20 ++++++++----- .../object-pattern-with-rest/exec.js | 24 ++++++++------- .../ordering--es2015/object-pattern/exec.js | 20 ++++++++----- .../fixtures/ordering--es2015/options.json | 1 + .../exec.js | 29 +++++++++++++++++++ .../ordering/array-pattern-with-rest/exec.js | 24 ++++++++++----- .../fixtures/ordering/array-pattern/exec.js | 20 ++++++++----- .../ordering/object-pattern-with-rest/exec.js | 24 ++++++++------- .../fixtures/ordering/object-pattern/exec.js | 20 ++++++++----- 11 files changed, 137 insertions(+), 70 deletions(-) create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/.array-pattern-side-effect-iterable/exec.js diff --git a/packages/babel-plugin-proposal-destructuring-private/src/util.ts b/packages/babel-plugin-proposal-destructuring-private/src/util.ts index e7b13fafe105..a8bf9ba89c75 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/util.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/util.ts @@ -430,6 +430,7 @@ export function* transformPrivateKeyDestructuring( // in RHS. Otherwise we have to pause the iterable and interleave // the expressions. // See also https://gist.github.com/nicolo-ribaudo/f8ac7916f89450f2ead77d99855b2098 + // and ordering/array-pattern-side-effect-iterable test const leftElements = left.elements; const leftElementsAfterIndex = leftElements.splice(index); const { elements, transformed } = buildAssignmentsFromPatternList( diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern-with-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern-with-rest/exec.js index 42b2f89bd467..38bf12b34320 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern-with-rest/exec.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern-with-rest/exec.js @@ -2,17 +2,25 @@ var log = []; function push(x, y = x) { log.push(x); return y; } +var log = []; + +function push(x, y = x) { log.push(x); return y; } + class C { - static #x; - static #y; + static get a() { push(1) } + static get b() { push(6) } + static get c() { push(10) } + static get d() { push(13) } + static get #x() { push(3) }; + static get #y() { return push(8, C) }; static { - var [{ [push(0)]: a = push(1), #x: { - [push(3)]: b = push(4), - #y: y = push(5), - [push(6)]: c = push(7) - } = push(2, C), [push(8)]: d = push(9) }, ...{ [push(10)]: e = push(11), ...f }] = [C]; + var [{ [push(0, "a")]: a = push(2), #x: { + [push(5, "b")]: b = push(7), + #y: y = push(-1), + [push(9, "c")]: c = push(11) + } = push(4, C), [push(12, "d")]: d = push(14) }, ...{ [push(15)]: e = push(16), ...f }] = [C]; } } -var nums = Array.from({ length: 12 }, (_, i) => i); +var nums = Array.from({ length: 17 }, (_, i) => i); expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern/exec.js index 280a7a1a424e..f459199da656 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern/exec.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern/exec.js @@ -3,16 +3,20 @@ var log = []; function push(x, y = x) { log.push(x); return y; } class C { - static #x; - static #y; + static get a() { push(1) } + static get b() { push(6) } + static get c() { push(10) } + static get d() { push(13) } + static get #x() { push(3) }; + static get #y() { return push(8, C) }; static { - var [{ [push(0)]: a = push(1), #x: { - [push(3)]: b = push(4), - #y: y = push(5), - [push(6)]: c = push(7) - } = push(2, C), [push(8)]: d = push(9) }, e = push(10)] = [C]; + var [{ [push(0, "a")]: a = push(2), #x: { + [push(5, "b")]: b = push(7), + #y: y = push(-1), + [push(9, "c")]: c = push(11) + } = push(4, C), [push(12, "d")]: d = push(14) }, e = push(15)] = [C]; } } -var nums = Array.from({ length: 11 }, (_, i) => i); +var nums = Array.from({ length: 16 }, (_, i) => i); expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern-with-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern-with-rest/exec.js index 46e9cfe5b3f6..47b7210eb0eb 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern-with-rest/exec.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern-with-rest/exec.js @@ -3,19 +3,21 @@ var log = []; function push(x, y = x) { log.push(x); return y; } class C { - static #x; - static #y; + static get a() { push(1) } + static get b() { push(6) } + static get c() { push(10) } + static get d() { push(13) } + static get #x() { push(3) }; + static get #y() { return push(8, C) }; + static get #z() { push(15) } static { - var { [push(0)]: a = push(1), #x: { - [push(3)]: b = push(4), - #y: y = push(5), - [push(6)]: c = push(7), - #x: x = push(8), - [push(9)]: d = push(10), - ...e - } = push(2, C), [push(11)]: d = push(12), #y: z = push(13), ...f } = C; + var { [push(0, "a")]: a = push(2), #x: { + [push(5, "b")]: b = push(7), + #y: y = push(-1), + [push(9, "c")]: c = push(11) + } = push(4, C), [push(12, "d")]: d = push(14), #z: z = push(16), ...f } = C; } } -var nums = Array.from({ length: 14 }, (_, i) => i); +var nums = Array.from({ length: 17 }, (_, i) => i); expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern/exec.js index bf9aedfe8ca0..e7a7d9d08e11 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern/exec.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern/exec.js @@ -3,16 +3,20 @@ var log = []; function push(x, y = x) { log.push(x); return y; } class C { - static #x; - static #y; + static get a() { push(1) } + static get b() { push(6) } + static get c() { push(10) } + static get d() { push(13) } + static get #x() { push(3) }; + static get #y() { return push(8, C) }; static { - var { [push(0)]: a = push(1), #x: { - [push(3)]: b = push(4), - #y: y = push(5), - [push(6)]: c = push(7) - } = push(2, C), [push(8)]: d = push(9) } = C; + var { [push(0, "a")]: a = push(2), #x: { + [push(5, "b")]: b = push(7), + #y: y = push(-1), + [push(9, "c")]: c = push(11) + } = push(4, C), [push(12, "d")]: d = push(14) } = C; } } -var nums = Array.from({ length: 10 }, (_, i) => i); +var nums = Array.from({ length: 15 }, (_, i) => i); expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/options.json index 8099cdd9d774..e1c5858604ed 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/options.json +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/options.json @@ -3,6 +3,7 @@ "proposal-destructuring-private", "proposal-class-static-block", "proposal-class-properties", + "proposal-private-methods", ["proposal-object-rest-spread", { "useBuiltIns": true }] ] } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/.array-pattern-side-effect-iterable/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/.array-pattern-side-effect-iterable/exec.js new file mode 100644 index 000000000000..d2b79d1a537d --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/.array-pattern-side-effect-iterable/exec.js @@ -0,0 +1,29 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + static get a() { push(2) } + static get b() { push(7) } + static get c() { push(11) } + static get d() { push(15) } + static get #x() { push(4) }; + static get #y() { return push(9, C) }; + static { + function *iterator() { + push(0); + yield C; + push(13); + yield C; + } + + var [{ [push(1, "a")]: a = push(3), #x: { + [push(6, "b")]: b = push(8), + #y: y = push(-1), + [push(10, "c")]: c = push(12) + } = push(5, C) }, { [push(14, "d")]: d = push(16) }] = iterator(); + } +} + +var nums = Array.from({ length: 16 }, (_, i) => i); +expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern-with-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern-with-rest/exec.js index 42b2f89bd467..38bf12b34320 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern-with-rest/exec.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern-with-rest/exec.js @@ -2,17 +2,25 @@ var log = []; function push(x, y = x) { log.push(x); return y; } +var log = []; + +function push(x, y = x) { log.push(x); return y; } + class C { - static #x; - static #y; + static get a() { push(1) } + static get b() { push(6) } + static get c() { push(10) } + static get d() { push(13) } + static get #x() { push(3) }; + static get #y() { return push(8, C) }; static { - var [{ [push(0)]: a = push(1), #x: { - [push(3)]: b = push(4), - #y: y = push(5), - [push(6)]: c = push(7) - } = push(2, C), [push(8)]: d = push(9) }, ...{ [push(10)]: e = push(11), ...f }] = [C]; + var [{ [push(0, "a")]: a = push(2), #x: { + [push(5, "b")]: b = push(7), + #y: y = push(-1), + [push(9, "c")]: c = push(11) + } = push(4, C), [push(12, "d")]: d = push(14) }, ...{ [push(15)]: e = push(16), ...f }] = [C]; } } -var nums = Array.from({ length: 12 }, (_, i) => i); +var nums = Array.from({ length: 17 }, (_, i) => i); expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern/exec.js index 280a7a1a424e..f459199da656 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern/exec.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern/exec.js @@ -3,16 +3,20 @@ var log = []; function push(x, y = x) { log.push(x); return y; } class C { - static #x; - static #y; + static get a() { push(1) } + static get b() { push(6) } + static get c() { push(10) } + static get d() { push(13) } + static get #x() { push(3) }; + static get #y() { return push(8, C) }; static { - var [{ [push(0)]: a = push(1), #x: { - [push(3)]: b = push(4), - #y: y = push(5), - [push(6)]: c = push(7) - } = push(2, C), [push(8)]: d = push(9) }, e = push(10)] = [C]; + var [{ [push(0, "a")]: a = push(2), #x: { + [push(5, "b")]: b = push(7), + #y: y = push(-1), + [push(9, "c")]: c = push(11) + } = push(4, C), [push(12, "d")]: d = push(14) }, e = push(15)] = [C]; } } -var nums = Array.from({ length: 11 }, (_, i) => i); +var nums = Array.from({ length: 16 }, (_, i) => i); expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern-with-rest/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern-with-rest/exec.js index 46e9cfe5b3f6..47b7210eb0eb 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern-with-rest/exec.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern-with-rest/exec.js @@ -3,19 +3,21 @@ var log = []; function push(x, y = x) { log.push(x); return y; } class C { - static #x; - static #y; + static get a() { push(1) } + static get b() { push(6) } + static get c() { push(10) } + static get d() { push(13) } + static get #x() { push(3) }; + static get #y() { return push(8, C) }; + static get #z() { push(15) } static { - var { [push(0)]: a = push(1), #x: { - [push(3)]: b = push(4), - #y: y = push(5), - [push(6)]: c = push(7), - #x: x = push(8), - [push(9)]: d = push(10), - ...e - } = push(2, C), [push(11)]: d = push(12), #y: z = push(13), ...f } = C; + var { [push(0, "a")]: a = push(2), #x: { + [push(5, "b")]: b = push(7), + #y: y = push(-1), + [push(9, "c")]: c = push(11) + } = push(4, C), [push(12, "d")]: d = push(14), #z: z = push(16), ...f } = C; } } -var nums = Array.from({ length: 14 }, (_, i) => i); +var nums = Array.from({ length: 17 }, (_, i) => i); expect(log).toStrictEqual(nums); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern/exec.js index bf9aedfe8ca0..e7a7d9d08e11 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern/exec.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern/exec.js @@ -3,16 +3,20 @@ var log = []; function push(x, y = x) { log.push(x); return y; } class C { - static #x; - static #y; + static get a() { push(1) } + static get b() { push(6) } + static get c() { push(10) } + static get d() { push(13) } + static get #x() { push(3) }; + static get #y() { return push(8, C) }; static { - var { [push(0)]: a = push(1), #x: { - [push(3)]: b = push(4), - #y: y = push(5), - [push(6)]: c = push(7) - } = push(2, C), [push(8)]: d = push(9) } = C; + var { [push(0, "a")]: a = push(2), #x: { + [push(5, "b")]: b = push(7), + #y: y = push(-1), + [push(9, "c")]: c = push(11) + } = push(4, C), [push(12, "d")]: d = push(14) } = C; } } -var nums = Array.from({ length: 10 }, (_, i) => i); +var nums = Array.from({ length: 15 }, (_, i) => i); expect(log).toStrictEqual(nums); From 39a26f1c3a4f5dbcdd3d39ea3ca979b575c706ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 2 May 2022 13:40:43 -0400 Subject: [PATCH 26/35] moves types import to plugin --- .../src/index.ts | 34 +++++++------------ 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/packages/babel-plugin-proposal-destructuring-private/src/index.ts b/packages/babel-plugin-proposal-destructuring-private/src/index.ts index 4779bdcef4af..e53fff1aabe7 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/index.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/index.ts @@ -11,29 +11,21 @@ import { unshiftForXStatementBody } from "@babel/plugin-transform-destructuring" import type { PluginPass } from "@babel/core"; import type { Visitor } from "@babel/traverse"; -import { types as t } from "@babel/core"; -const { - assignmentExpression, - assignmentPattern, - cloneNode, - expressionStatement, - isExpressionStatement, - isIdentifier, - isSequenceExpression, - sequenceExpression, - variableDeclaration, - variableDeclarator, -} = t; - -export default declare(function ({ - assertVersion, - assumption, -}: { - assumption: (string) => boolean; - assertVersion: (string) => void; -}) { +export default declare(function ({ assertVersion, assumption, types: t }) { assertVersion("^7.17.0"); + const { + assignmentExpression, + assignmentPattern, + cloneNode, + expressionStatement, + isExpressionStatement, + isIdentifier, + isSequenceExpression, + sequenceExpression, + variableDeclaration, + variableDeclarator, + } = t; const ignoreFunctionLength = assumption("ignoreFunctionLength"); const objectRestNoSymbols = assumption("objectRestNoSymbols"); From acbd8a690ea84edf578e051f0f06bd305750ddc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 2 May 2022 14:17:18 -0400 Subject: [PATCH 27/35] fix typings --- .../src/util.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/babel-plugin-proposal-destructuring-private/src/util.ts b/packages/babel-plugin-proposal-destructuring-private/src/util.ts index a8bf9ba89c75..44ca8b83d590 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/util.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/util.ts @@ -104,7 +104,13 @@ export function buildVariableDeclarationFromParams( } interface Transformed { - left: t.Identifier | t.Pattern | t.MemberExpression; + left: + | t.Identifier + | t.Pattern + | t.MemberExpression + | t.TSAsExpression + | t.TSTypeAssertion + | t.TSNonNullExpression; right: t.Expression; } @@ -257,12 +263,16 @@ export function* privateKeyPathIterator(pattern: t.LVal) { }); } +// t.LVal without t.RestElement type LHS = | t.Identifier | t.MemberExpression | t.ArrayPattern | t.ObjectPattern - | t.AssignmentPattern; + | t.AssignmentPattern + | t.TSAsExpression + | t.TSTypeAssertion + | t.TSNonNullExpression; type ExcludingKey = { key: t.ObjectProperty["key"]; From d3da6fb755f668bb05c9b7611dae54fda08dd08b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 2 May 2022 16:38:26 -0400 Subject: [PATCH 28/35] simplify typings --- .../src/util.ts | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/packages/babel-plugin-proposal-destructuring-private/src/util.ts b/packages/babel-plugin-proposal-destructuring-private/src/util.ts index 44ca8b83d590..2c7f652cce1a 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/util.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/util.ts @@ -104,13 +104,7 @@ export function buildVariableDeclarationFromParams( } interface Transformed { - left: - | t.Identifier - | t.Pattern - | t.MemberExpression - | t.TSAsExpression - | t.TSTypeAssertion - | t.TSNonNullExpression; + left: Exclude; right: t.Expression; } @@ -263,16 +257,7 @@ export function* privateKeyPathIterator(pattern: t.LVal) { }); } -// t.LVal without t.RestElement -type LHS = - | t.Identifier - | t.MemberExpression - | t.ArrayPattern - | t.ObjectPattern - | t.AssignmentPattern - | t.TSAsExpression - | t.TSTypeAssertion - | t.TSNonNullExpression; +type LHS = Exclude; type ExcludingKey = { key: t.ObjectProperty["key"]; @@ -356,7 +341,12 @@ export function* transformPrivateKeyDestructuring( ), }; } else { - yield { left, right }; + yield { + left: + // An assignment pattern will not be pushed to the stack + left as Transformed["left"], + right, + }; } } else { // now we need to split according to the indexPath; From 993febb813654725266f104926f032e1b1d2aca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 4 May 2022 08:41:15 -0400 Subject: [PATCH 29/35] Update packages/babel-plugin-proposal-destructuring-private/src/index.ts Co-authored-by: Justin Ridgewell --- .../babel-plugin-proposal-destructuring-private/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-plugin-proposal-destructuring-private/src/index.ts b/packages/babel-plugin-proposal-destructuring-private/src/index.ts index e53fff1aabe7..49acfda59a68 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/index.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/index.ts @@ -43,7 +43,7 @@ export default declare(function ({ assertVersion, assumption, types: t }) { convertFunctionParams(path, ignoreFunctionLength, () => false, false); // invariant: path.body is always a BlockStatement after `convertFunctionParams` const { node, scope } = path; - const params = node.params; + const { params } = node; const firstAssignmentPatternIndex = ignoreFunctionLength ? -1 : params.findIndex(param => param.type === "AssignmentPattern"); From eeb77b6de6033164567af5825bd71cbaec364b39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 4 May 2022 09:17:35 -0400 Subject: [PATCH 30/35] crawl after catch clause transform --- .../src/index.ts | 1 + .../object-rest-and-keys/exec.js | 16 ++++++++++ .../object-rest-and-keys/input.js | 13 ++++++++ .../object-rest-and-keys/output.js | 30 +++++++++++++++++++ .../assignment/object-rest-and-keys/exec.js | 16 ++++++++++ .../assignment/object-rest-and-keys/input.js | 13 ++++++++ .../assignment/object-rest-and-keys/output.js | 21 +++++++++++++ .../object-rest-and-keys/exec.js | 15 ++++++++++ .../object-rest-and-keys/input.js | 12 ++++++++ .../object-rest-and-keys/output.js | 26 ++++++++++++++++ .../object-rest-and-keys/exec.js | 15 ++++++++++ .../object-rest-and-keys/input.js | 12 ++++++++ .../object-rest-and-keys/output.js | 21 +++++++++++++ 13 files changed, 211 insertions(+) create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-keys/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-keys/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-keys/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-keys/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-keys/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-keys/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-keys/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-keys/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-keys/output.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-keys/exec.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-keys/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-keys/output.js diff --git a/packages/babel-plugin-proposal-destructuring-private/src/index.ts b/packages/babel-plugin-proposal-destructuring-private/src/index.ts index 49acfda59a68..f210f5146e52 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/index.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/index.ts @@ -82,6 +82,7 @@ export default declare(function ({ assertVersion, assumption, types: t }) { variableDeclaration("let", [variableDeclarator(node.param, ref)]), ); node.param = cloneNode(ref); + scope.crawl(); // the pattern will be handled by VariableDeclaration visitor. }, ForXStatement(path) { diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-keys/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-keys/exec.js new file mode 100644 index 000000000000..63e834ef0af0 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-keys/exec.js @@ -0,0 +1,16 @@ +let result; +class C { + static #x = "#x"; + static y = "y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + let x, y, z; + ({ #x: x, y, ...z } = C); + result = { x, y, z }; + } +} +expect(result).toStrictEqual({ + x: "#x", y: "y", z: { a: "a", b: "b", c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-keys/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-keys/input.js new file mode 100644 index 000000000000..f1a251c17ecb --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-keys/input.js @@ -0,0 +1,13 @@ +let result; +class C { + static #x = "#x"; + static #y = "y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + let x, y, z; + ({ #x: x, y, ...z } = C); + result = { x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-keys/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-keys/output.js new file mode 100644 index 000000000000..096518eaab16 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment--es2015/object-rest-and-keys/output.js @@ -0,0 +1,30 @@ +const _excluded = ["y"]; +let result; + +class C {} + +var _x = { + writable: true, + value: "#x" +}; +var _y = { + writable: true, + value: "y" +}; +babelHelpers.defineProperty(C, "a", "a"); +babelHelpers.defineProperty(C, "b", "b"); +babelHelpers.defineProperty(C, "c", "c"); + +(() => { + var _C; + + let x, y, z; + x = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), (_C = C, ({ + y + } = _C), z = babelHelpers.objectWithoutProperties(_C, _excluded), _C); + result = { + x, + y, + z + }; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-keys/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-keys/exec.js new file mode 100644 index 000000000000..63e834ef0af0 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-keys/exec.js @@ -0,0 +1,16 @@ +let result; +class C { + static #x = "#x"; + static y = "y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + let x, y, z; + ({ #x: x, y, ...z } = C); + result = { x, y, z }; + } +} +expect(result).toStrictEqual({ + x: "#x", y: "y", z: { a: "a", b: "b", c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-keys/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-keys/input.js new file mode 100644 index 000000000000..73d43ef46825 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-keys/input.js @@ -0,0 +1,13 @@ +let result; +class C { + static #x = "#x"; + static y = "y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + let x, y, z; + ({ #x: x, y, ...z } = C); + result = { x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-keys/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-keys/output.js new file mode 100644 index 000000000000..ae46e51f1469 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/assignment/object-rest-and-keys/output.js @@ -0,0 +1,21 @@ +let result; + +class C { + static #x = "#x"; + static y = "y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + let x, y, z; + x = C.#x, ({ + y, + ...z + } = C); + result = { + x, + y, + z + }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-keys/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-keys/exec.js new file mode 100644 index 000000000000..4c238f56809a --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-keys/exec.js @@ -0,0 +1,15 @@ +let result; +class C { + static #x = "#x"; + static y = "y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { #x: x, y, ...z } = C; + result = { x, y, z }; + } +} +expect(result).toStrictEqual({ + x: "#x", y: "y", z: { a: "a", b: "b", c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-keys/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-keys/input.js new file mode 100644 index 000000000000..d8aa33d3efd1 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-keys/input.js @@ -0,0 +1,12 @@ +let result; +class C { + static #x = "#x"; + static y = "y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { #x: x, y, ...z } = C; + result = { x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-keys/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-keys/output.js new file mode 100644 index 000000000000..f2d4bd17865d --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/object-rest-and-keys/output.js @@ -0,0 +1,26 @@ +const _excluded = ["y"]; +let result; + +class C {} + +var _x = { + writable: true, + value: "#x" +}; +babelHelpers.defineProperty(C, "y", "y"); +babelHelpers.defineProperty(C, "a", "a"); +babelHelpers.defineProperty(C, "b", "b"); +babelHelpers.defineProperty(C, "c", "c"); + +(() => { + var x = babelHelpers.classStaticPrivateFieldSpecGet(C, C, _x), + { + y + } = C, + z = babelHelpers.objectWithoutProperties(C, _excluded); + result = { + x, + y, + z + }; +})(); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-keys/exec.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-keys/exec.js new file mode 100644 index 000000000000..4c238f56809a --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-keys/exec.js @@ -0,0 +1,15 @@ +let result; +class C { + static #x = "#x"; + static y = "y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { #x: x, y, ...z } = C; + result = { x, y, z }; + } +} +expect(result).toStrictEqual({ + x: "#x", y: "y", z: { a: "a", b: "b", c: "c" } +}); diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-keys/input.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-keys/input.js new file mode 100644 index 000000000000..d8aa33d3efd1 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-keys/input.js @@ -0,0 +1,12 @@ +let result; +class C { + static #x = "#x"; + static y = "y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var { #x: x, y, ...z } = C; + result = { x, y, z }; + } +} diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-keys/output.js b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-keys/output.js new file mode 100644 index 000000000000..f213c40bd329 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/object-rest-and-keys/output.js @@ -0,0 +1,21 @@ +let result; + +class C { + static #x = "#x"; + static y = "y"; + static a = "a"; + static b = "b"; + static c = "c"; + static { + var x = C.#x, + { + y, + ...z + } = C; + result = { + x, + y, + z + }; + } +} From c8d22def98c2cc2bd57665bff1cae49d7d95ff09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 4 May 2022 10:15:33 -0400 Subject: [PATCH 31/35] review comments --- .../src/util.ts | 50 ++++++++++--------- .../options.json | 2 +- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/packages/babel-plugin-proposal-destructuring-private/src/util.ts b/packages/babel-plugin-proposal-destructuring-private/src/util.ts index 2c7f652cce1a..fe4170894217 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/util.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/util.ts @@ -152,7 +152,7 @@ function buildAssignmentsFromPatternList( /** * A DFS simplified pattern traverser. It skips computed property keys and assignment pattern - * initializers. The following-type path will be delegate to the visitor: + * initializers. The following nodes will be delegated to the visitor: * - ArrayPattern * - ArrayPattern elements * - AssignmentPattern @@ -204,8 +204,11 @@ export function* traversePattern( } break; case "TSParameterProperty": + case "TSAsExpression": + case "TSTypeAssertion": + case "TSNonNullExpression": throw new Error( - `TypeScript parameter properties must first be transformed by ` + + `TypeScript features must first be transformed by ` + `@babel/plugin-transform-typescript.\n` + `If you have already enabled that plugin (or '@babel/preset-typescript'), make sure ` + `that it runs before @babel/plugin-proposal-destructuring-private.`, @@ -217,13 +220,14 @@ export function* traversePattern( } export function hasPrivateKeys(pattern: t.LVal) { - return ( - traversePattern(pattern, function* (node) { - if (isObjectProperty(node) && isPrivateName(node.key)) { - yield; - } - }).next().done === false - ); + let result = false; + traversePattern(pattern, function* (node) { + if (isObjectProperty(node) && isPrivateName(node.key)) { + result = true; + yield; + } + }).next(); + return result; } export function hasPrivateClassElement(node: t.ClassBody): boolean { @@ -248,11 +252,11 @@ export function* privateKeyPathIterator(pattern: t.LVal) { const indexPath = []; yield* traversePattern(pattern, function* (node, index, depth) { indexPath[depth] = index; - if (isObjectProperty(node)) { - const propertyKey = node.key; - if (isPrivateName(propertyKey)) { - yield indexPath.slice(1, depth + 1); - } + if (isObjectProperty(node) && isPrivateName(node.key)) { + // The indexPath[0, depth] contains the path from root pattern to the object property + // with private key. The indexPath may have more than depth + 1 elements because we + // don't shrink the indexPath when the traverser returns to parent nodes. + yield indexPath.slice(1, depth + 1); } }); } @@ -351,12 +355,12 @@ export function* transformPrivateKeyDestructuring( } else { // now we need to split according to the indexPath; const indexPath = searchPrivateKey.value; - let index; - let isFirst = true; - while ( - (index = indexPath.shift()) !== undefined || - left.type === "AssignmentPattern" + for ( + let indexPathIndex = 0; + indexPathIndex < indexPath.length || left.type === "AssignmentPattern"; + indexPathIndex++ ) { + const index = indexPath[indexPathIndex]; const isRightSafeToReuse = // If we should preserve completion and the right is the rootRight, then the // right is NOT safe to reuse because we will insert a new memoising statement @@ -388,9 +392,10 @@ export function* transformPrivateKeyDestructuring( // for properties after `index`, push them to stack so we can process them later // inherit the restExcludingKeys on the stack if we are at // the first level, otherwise initialize a new restExcludingKeys - const nextRestExcludingKeys = isFirst - ? restExcludingKeys - : initRestExcludingKeys(left); + const nextRestExcludingKeys = + indexPathIndex === 0 + ? restExcludingKeys + : initRestExcludingKeys(left); growRestExcludingKeys( nextRestExcludingKeys, // @ts-expect-error properties[0, index] must not contain rest element @@ -453,7 +458,6 @@ export function* transformPrivateKeyDestructuring( default: break; } - isFirst = false; } stack.push({ left, diff --git a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/invalid-after-destructuring-private/options.json b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/invalid-after-destructuring-private/options.json index 2e503cc0a75f..c033e98f1b8e 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/invalid-after-destructuring-private/options.json +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/invalid-after-destructuring-private/options.json @@ -1,6 +1,6 @@ { "plugins": ["proposal-destructuring-private", "transform-typescript"], "throws": [ - "TypeScript parameter properties must first be transformed by @babel/plugin-transform-typescript." + "TypeScript features must first be transformed by @babel/plugin-transform-typescript." ] } From 2a7b8bc5c4d6fc3e3b477fb5b790434a00c41079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 4 May 2022 10:40:51 -0400 Subject: [PATCH 32/35] perf: avoid out-of-bound array read --- .../src/util.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/babel-plugin-proposal-destructuring-private/src/util.ts b/packages/babel-plugin-proposal-destructuring-private/src/util.ts index fe4170894217..1c567995a011 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/util.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/util.ts @@ -356,11 +356,12 @@ export function* transformPrivateKeyDestructuring( // now we need to split according to the indexPath; const indexPath = searchPrivateKey.value; for ( - let indexPathIndex = 0; - indexPathIndex < indexPath.length || left.type === "AssignmentPattern"; + let indexPathIndex = 0, index; + (indexPathIndex < indexPath.length && + (index = indexPath[indexPathIndex]) !== undefined) || + left.type === "AssignmentPattern"; indexPathIndex++ ) { - const index = indexPath[indexPathIndex]; const isRightSafeToReuse = // If we should preserve completion and the right is the rootRight, then the // right is NOT safe to reuse because we will insert a new memoising statement From 603192c1eedd283879a5ec7f5122bddb0327fe89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Thu, 5 May 2022 16:09:21 -0400 Subject: [PATCH 33/35] address review comment --- .../src/util.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/babel-plugin-proposal-destructuring-private/src/util.ts b/packages/babel-plugin-proposal-destructuring-private/src/util.ts index 1c567995a011..66ffbba18b53 100644 --- a/packages/babel-plugin-proposal-destructuring-private/src/util.ts +++ b/packages/babel-plugin-proposal-destructuring-private/src/util.ts @@ -3,8 +3,8 @@ import type { Scope } from "@babel/traverse"; import { types } from "@babel/core"; import type { File } from "@babel/core"; import { buildObjectExcludingKeys } from "@babel/plugin-transform-destructuring"; -import { assignmentExpression, ObjectProperty } from "@babel/types"; const { + assignmentExpression, binaryExpression, conditionalExpression, cloneNode, @@ -49,12 +49,12 @@ function initRestExcludingKeys(pattern: t.LVal): ExcludingKey[] | null { * memoising the computed non-static keys. * * @param {ExcludingKey[]} excludingKeys - * @param {ObjectProperty[]} properties An array of object properties that should be excluded by rest element transform + * @param {t.ObjectProperty[]} properties An array of object properties that should be excluded by rest element transform * @param {Scope} scope Where should we register the memoised id */ function growRestExcludingKeys( excludingKeys: ExcludingKey[], - properties: ObjectProperty[], + properties: t.ObjectProperty[], scope: Scope, ) { if (excludingKeys === null) return; @@ -224,6 +224,7 @@ export function hasPrivateKeys(pattern: t.LVal) { traversePattern(pattern, function* (node) { if (isObjectProperty(node) && isPrivateName(node.key)) { result = true; + // stop the traversal yield; } }).next(); @@ -324,7 +325,7 @@ export function* transformPrivateKeyDestructuring( if (searchPrivateKey.done) { if (restExcludingKeys?.length > 0) { // optimize out the rest element because `objectWithoutProperties` - // always return a new object + // returns a new object // `{ ...z } = babelHelpers.objectWithoutProperties(m, ["x"])` // to // `z = babelHelpers.objectWithoutProperties(m, ["x"])` From ce7d90d5856f043351d0d50afe7d0dc43b9a94f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Thu, 5 May 2022 16:54:43 -0400 Subject: [PATCH 34/35] chore: rename test case --- .../exec.js | 0 .../input.js | 0 .../options.json | 0 .../output.js | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/{for-of-shadowd-block-scoped => for-of-shadowed-block-scoped}/exec.js (100%) rename packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/{for-of-shadowd-block-scoped => for-of-shadowed-block-scoped}/input.js (100%) rename packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/{for-of-shadowd-block-scoped => for-of-shadowed-block-scoped}/options.json (100%) rename packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/{for-of-shadowd-block-scoped => for-of-shadowed-block-scoped}/output.js (100%) diff --git a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/exec.js b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-block-scoped/exec.js similarity index 100% rename from packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/exec.js rename to packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-block-scoped/exec.js diff --git a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/input.js b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-block-scoped/input.js similarity index 100% rename from packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/input.js rename to packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-block-scoped/input.js diff --git a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/options.json b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-block-scoped/options.json similarity index 100% rename from packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/options.json rename to packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-block-scoped/options.json diff --git a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/output.js b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-block-scoped/output.js similarity index 100% rename from packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowd-block-scoped/output.js rename to packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-block-scoped/output.js From e57ff743f198fb7cc4a926ddc90a3115e42b79c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Thu, 5 May 2022 17:07:07 -0400 Subject: [PATCH 35/35] fix merging errors --- packages/babel-plugin-transform-destructuring/src/index.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/babel-plugin-transform-destructuring/src/index.ts b/packages/babel-plugin-transform-destructuring/src/index.ts index a0566fea6f68..5adeda9e870f 100644 --- a/packages/babel-plugin-transform-destructuring/src/index.ts +++ b/packages/babel-plugin-transform-destructuring/src/index.ts @@ -128,10 +128,6 @@ export default declare((api, options: Options) => { destructuring.init(pattern, key); unshiftForXStatementBody(path, nodes); - - const block = node.body; - // @ts-expect-error: ensureBlock ensures that node.body is a BlockStatement - block.body = nodes.concat(block.body); scope.crawl(); },