From 1daded57126ba172eef3664c08829b83a1112cc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 17 May 2022 18:02:58 -0400 Subject: [PATCH] Transform destructuring private (#14304) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Nicolò Ribaudo Co-authored-by: Justin Ridgewell --- Gulpfile.mjs | 1 + .../src/features.ts | 2 +- packages/babel-helper-define-map/src/index.ts | 6 +- .../package.json | 2 +- .../.npmignore | 3 + .../README.md | 19 + .../package.json | 42 ++ .../src/index.ts | 214 ++++++++ .../src/util.ts | 471 ++++++++++++++++++ .../array-rest-destructuring-middle/exec.js | 10 + .../array-rest-destructuring-middle/input.js | 7 + .../array-rest-destructuring-middle/output.js | 18 + .../array-rest-only/exec.js | 10 + .../array-rest-only/input.js | 9 + .../array-rest-only/output.js | 20 + .../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 + .../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--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-keys/exec.js | 16 + .../object-rest-and-keys/input.js | 13 + .../object-rest-and-keys/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 ++ .../array-rest-destructuring-middle/exec.js | 10 + .../array-rest-destructuring-middle/input.js | 7 + .../array-rest-destructuring-middle/output.js | 14 + .../assignment/array-rest-only/exec.js | 10 + .../assignment/array-rest-only/input.js | 9 + .../assignment/array-rest-only/output.js | 16 + .../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 + .../completion-do-expression/exec.js | 9 + .../completion-do-expression/input.js | 8 + .../completion-do-expression/options.json | 4 + .../completion-do-expression/output.js | 11 + .../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 + .../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-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 + .../function-params/input.js | 7 + .../function-params/output.js | 26 + .../options.json | 12 + .../options.json | 12 + .../variable-declaration/exec.js | 15 + .../variable-declaration/input.js | 12 + .../variable-declaration/output.js | 36 ++ .../no-shadowed-params/exec.js | 10 + .../no-shadowed-params/input.js | 9 + .../no-shadowed-params/options.json | 7 + .../no-shadowed-params/output.js | 23 + .../catch-param/no-shadowed-params/exec.js | 10 + .../catch-param/no-shadowed-params/input.js | 9 + .../no-shadowed-params/options.json | 4 + .../catch-param/no-shadowed-params/output.js | 15 + .../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 + .../lhs-with-shadowed-block-scoped/exec.js | 10 + .../lhs-with-shadowed-block-scoped/input.js | 10 + .../lhs-with-shadowed-block-scoped/output.js | 21 + .../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-block-scoped/exec.js | 10 + .../input.js | 10 + .../output.js | 21 + .../variable-declaration/input.js | 6 + .../variable-declaration/output.js | 18 + .../lhs-with-shadowed-block-scoped/exec.js | 10 + .../lhs-with-shadowed-block-scoped/input.js | 10 + .../lhs-with-shadowed-block-scoped/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 + .../variable-declaration-block-scoped/exec.js | 10 + .../input.js | 10 + .../output.js | 18 + .../for-of/variable-declaration/input.js | 6 + .../for-of/variable-declaration/output.js | 10 + .../function-length/exec.js | 11 + .../function-length/input.js | 7 + .../function-length/output.js | 26 + .../no-shadowed-params/input.js | 5 + .../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 | 11 + .../function-params/function-length/input.js | 7 + .../function-params/function-length/output.js | 23 + .../no-shadowed-params/input.js | 5 + .../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 | 26 + .../ordering--es2015/array-pattern/exec.js | 22 + .../object-pattern-with-rest/exec.js | 23 + .../ordering--es2015/object-pattern/exec.js | 22 + .../fixtures/ordering--es2015/options.json | 9 + .../exec.js | 29 ++ .../ordering/array-pattern-with-rest/exec.js | 26 + .../fixtures/ordering/array-pattern/exec.js | 22 + .../ordering/object-pattern-with-rest/exec.js | 23 + .../fixtures/ordering/object-pattern/exec.js | 22 + .../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 | 9 + .../array-rest-only/exec.js | 9 + .../array-rest-only/input.js | 8 + .../array-rest-only/output.js | 21 + .../array-rest/exec.js | 9 + .../array-rest/input.js | 6 + .../array-rest/output.js | 13 + .../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 | 28 ++ .../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 | 10 + .../object-rest-and-keys/exec.js | 15 + .../object-rest-and-keys/input.js | 12 + .../object-rest-and-keys/output.js | 26 + .../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 + .../array-rest-only/exec.js | 9 + .../array-rest-only/input.js | 8 + .../array-rest-only/output.js | 16 + .../variable-declaration/array-rest/exec.js | 9 + .../variable-declaration/array-rest/input.js | 6 + .../variable-declaration/array-rest/output.js | 9 + .../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 | 19 + .../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 | 10 + .../object-rest-and-keys/exec.js | 15 + .../object-rest-and-keys/input.js | 12 + .../object-rest-and-keys/output.js | 21 + .../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 +++ .../src/index.ts | 4 +- .../package.json | 18 +- .../src/index.ts | 25 +- .../src/util.ts | 36 +- .../input.js | 2 + .../output.js | 2 + .../for-of-shadowed-block-scoped/exec.js | 8 + .../for-of-shadowed-block-scoped/input.js | 8 + .../for-of-shadowed-block-scoped/options.json | 3 + .../for-of-shadowed-block-scoped/output.js | 20 + .../src/index.ts | 1 - .../src/index.ts | 58 +-- .../parameter-properties-with-class/output.js | 12 +- packages/babel-standalone/package.json | 1 + .../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 + .../babel-traverse/src/path/introspection.ts | 10 +- packages/babel-traverse/src/scope/index.ts | 21 +- packages/babel-traverse/test/scope.js | 25 + .../src/ast-types/generated/index.ts | 9 +- .../src/builders/generated/index.ts | 9 +- .../src/converters/toComputedKey.ts | 2 +- packages/babel-types/src/definitions/core.ts | 6 + tsconfig.json | 4 + yarn.lock | 18 + 283 files changed, 4338 insertions(+), 76 deletions(-) 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-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 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--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-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--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-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--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-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 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/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-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 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-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/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-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/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/exec.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/exec.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-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/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-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-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-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/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-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 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/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--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/function-length/input.js create mode 100644 packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/output.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-side-effect-iterable/exec.js 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-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--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-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--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-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 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-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 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 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 create mode 100644 packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-block-scoped/exec.js create mode 100644 packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-block-scoped/input.js create mode 100644 packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-block-scoped/options.json create mode 100644 packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-block-scoped/output.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-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-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-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-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..2da4b4c798bb --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/package.json @@ -0,0 +1,42 @@ +{ + "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)", + "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..f210f5146e52 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/src/index.ts @@ -0,0 +1,214 @@ +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 { unshiftForXStatementBody } from "@babel/plugin-transform-destructuring"; + +import type { PluginPass } from "@babel/core"; +import type { Visitor } from "@babel/traverse"; + +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"); + + const privateKeyDestructuringVisitor: Visitor = { + Function(path) { + // (b, { #x: x } = I) => body + // transforms to: + // (b, p1) => { var { #x: x } = p1 === undefined ? I : p1; body; } + 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; + 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. + }, + 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("let", [variableDeclarator(node.param, ref)]), + ); + node.param = cloneNode(ref); + scope.crawl(); + // 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; } + // 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), + ]); + left.declarations[0].init = cloneNode(temp); + unshiftForXStatementBody(path, [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 temp = scope.generateUidIdentifier("ref"); + node.left = variableDeclaration("const", [ + variableDeclarator(temp, null), + ]); + const assignExpr = expressionStatement( + assignmentExpression("=", leftPath.node, cloneNode(temp)), + ); + unshiftForXStatementBody(path, [assignExpr]); + scope.crawl(); + } + }, + 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( + // @ts-expect-error The id of a variable declarator must not be a RestElement + declarator.id, + declarator.init, + scope, + /* isAssignment */ false, + /* shouldPreserveCompletion */ 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 = []; + 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, + )) { + assignments.push(assignmentExpression("=", left, right)); + } + // preserve completion record + 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)); + } + } + + 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..66ffbba18b53 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/src/util.ts @@ -0,0 +1,471 @@ +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"; +const { + assignmentExpression, + 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): ExcludingKey[] | null { + if (pattern.type === "ObjectPattern") { + const { properties } = pattern; + if (properties[properties.length - 1].type === "RestElement") { + return []; + } + } + return null; +} + +/** + * grow `excludingKeys` from given properties. This routine mutates properties by + * memoising the computed non-static keys. + * + * @param {ExcludingKey[]} excludingKeys + * @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: t.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); + excludingKeys.push({ key: tempId, computed: true }); + } else if (propertyKey.type !== "PrivateName") { + excludingKeys.push(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: Exclude; + 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 nodes will be delegated 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": + case "TSAsExpression": + case "TSTypeAssertion": + case "TSNonNullExpression": + throw new Error( + `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.`, + ); + default: + break; + } + } +} + +export function hasPrivateKeys(pattern: t.LVal) { + let result = false; + traversePattern(pattern, function* (node) { + if (isObjectProperty(node) && isPrivateName(node.key)) { + result = true; + // stop the traversal + yield; + } + }).next(); + return result; +} + +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) && 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); + } + }); +} + +type LHS = Exclude; + +type ExcludingKey = { + key: t.ObjectProperty["key"]; + computed: t.ObjectProperty["computed"]; +}; +type Item = { + left: LHS; + right: t.Expression; + 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; + case "ObjectPattern": + return left.properties.length === 1; + default: + return false; + } +} +/** + * Transform private destructuring. It returns a generator + * which yields a pair of transformed LHS and RHS, which can form VariableDeclaration or + * AssignmentExpression later. + * + * @export + * @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: LHS, + 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, + 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?.length > 0) { + // optimize out the rest element because `objectWithoutProperties` + // returns a new object + // `{ ...z } = babelHelpers.objectWithoutProperties(m, ["x"])` + // to + // `z = babelHelpers.objectWithoutProperties(m, ["x"])` + const { properties } = left as t.ObjectPattern; + if (properties.length === 1) { + // 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, + right: buildObjectExcludingKeys( + restExcludingKeys, + right, + scope, + addHelper, + objectRestNoSymbols, + useBuiltIns, + ), + }; + } else { + 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; + const indexPath = searchPrivateKey.value; + for ( + let indexPathIndex = 0, index; + (indexPathIndex < indexPath.length && + (index = indexPath[indexPathIndex]) !== undefined) || + left.type === "AssignmentPattern"; + 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 + // 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) }); + } + 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; + if (index > 0) { + // properties[0, index) must not contain private keys + const propertiesSlice = properties.slice(0, index); + 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 + // inherit the restExcludingKeys on the stack if we are at + // the first level, otherwise initialize a new restExcludingKeys + const nextRestExcludingKeys = + indexPathIndex === 0 + ? restExcludingKeys + : initRestExcludingKeys(left); + 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), + restExcludingKeys: nextRestExcludingKeys, + }); + } + // An object rest element must not contain a private key + const property = properties[index] as t.ObjectProperty; + // The value of ObjectProperty under ObjectPattern must be an LHS + left = property.value as LHS; + 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 + // and ordering/array-pattern-side-effect-iterable test + const leftElements = left.elements; + const leftElementsAfterIndex = leftElements.splice(index); + const { elements, transformed } = buildAssignmentsFromPatternList( + leftElementsAfterIndex, + scope, + isAssignment, + ); + leftElements.push(...elements); + yield { left, right: cloneNode(right) }; + // for elements after `index`, push them to stack so we can process them later + for (let i = transformed.length - 1; i > 0; i--) { + // skipping array holes + if (transformed[i] !== null) { + stack.push(transformed[i]); + } + } + ({ left, right } = transformed[0]); + break; + } + default: + break; + } + } + stack.push({ + left, + right, + restExcludingKeys: initRestExcludingKeys(left), + }); + } + } +} 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..15f6b3310491 --- /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 _p, _p2, _m; + + [{ + y + }, _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/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..2691e38a1b03 --- /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 _p, _m; + + var x, z; + [..._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/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..837199e34665 --- /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 _p, _p2, _m; + + [_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/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-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--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..37753eaca23e --- /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 _p, _p2; + + let z; + [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/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..3454fb0bf7dd --- /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 _p, _p2, _p3, _p4, _m; + + let x, y, z; + [_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/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-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--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..d0f627623477 --- /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(_m2, C, _x)), _m2)) {} + +} + +var _x = { + writable: true, + value: { + a: 1, + b: 2 + } +}; + +(() => { + var _m; + + let b; + + (function f(r = (_m = C, ({ + b + } = babelHelpers.classStaticPrivateFieldSpecGet(_m, C, _x)), _m)) {})(); +})(); + +C.m(); 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..a95547193b0d --- /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 _p, _p2, _m; + + [{ + y + }, _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/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..9eae325d1e7e --- /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 _p, _m; + + var x, z; + [..._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/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..50156224c564 --- /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 _p, _p2, _m; + + [_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/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-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-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..738a12d73cae --- /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 _p, _p2; + + let z; + [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/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..3d9e614af2a0 --- /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 _p, _p2, _p3, _p4, _m; + + let x, y, z; + [_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/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-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/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..e666e783149e --- /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 + } = _m.#x), _m)) {})(); + + } + + static m(r = (_m2 = C, ({ + a + } = _m2.#x), _m2)) {} + +} + +C.m(); 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/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/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 new file mode 100644 index 000000000000..b8ba97ae407d --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/input.js @@ -0,0 +1,9 @@ +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/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..5688afc9281d --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param--es2015/no-shadowed-params/output.js @@ -0,0 +1,23 @@ +var x; + +var _x = /*#__PURE__*/new WeakMap(); + +class C { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _x, { + writable: true, + value: void 0 + }); + } + +} + +(() => { + x = "x"; + + try { + throw new C(); + } catch (_e) { + 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 new file mode 100644 index 000000000000..b8ba97ae407d --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/input.js @@ -0,0 +1,9 @@ +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/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..45bc0a04f3ca --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/catch-param/no-shadowed-params/output.js @@ -0,0 +1,15 @@ +var x; + +class C { + #x; + static { + x = "x"; + + try { + throw new C(); + } catch (_e) { + let 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..ba1bd3c17a15 --- /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(_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/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..12bf9e55f321 --- /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(_m, _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..89c4773d5c46 --- /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 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--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..fb12bc77834a --- /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 = _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/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..9ef3ef20cb47 --- /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 = _m.#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..df0a155d4efa --- /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 x = (_m = C, y = _m.#x, _m).#x;;) { + var _m; + + 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-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/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-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-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-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/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-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"; + } + } + + } +} 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..2f89b81049d4 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/function-length/exec.js @@ -0,0 +1,11 @@ +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) {} +} +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--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..6d39f49343ca --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params--es2015/no-shadowed-params/input.js @@ -0,0 +1,5 @@ +class C { + #x; + 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 new file mode 100644 index 000000000000..657643b209fe --- /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(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/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..196951aa94ef --- /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 = 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 new file mode 100644 index 000000000000..e6c11d5e6ab6 --- /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(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/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..2f89b81049d4 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/function-length/exec.js @@ -0,0 +1,11 @@ +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) {} +} +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; + } + +} 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..6d39f49343ca --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/function-params/no-shadowed-params/input.js @@ -0,0 +1,5 @@ +class C { + #x; + 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 new file mode 100644 index 000000000000..d5263e0ac70f --- /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(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/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..196951aa94ef --- /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 = 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 new file mode 100644 index 000000000000..a4d9eb4c525a --- /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(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/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..38bf12b34320 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern-with-rest/exec.js @@ -0,0 +1,26 @@ +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 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")]: 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: 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 new file mode 100644 index 000000000000..f459199da656 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/array-pattern/exec.js @@ -0,0 +1,22 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + 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")]: 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: 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 new file mode 100644 index 000000000000..47b7210eb0eb --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern-with-rest/exec.js @@ -0,0 +1,23 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + 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")]: 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: 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 new file mode 100644 index 000000000000..e7a7d9d08e11 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--es2015/object-pattern/exec.js @@ -0,0 +1,22 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + 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")]: 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: 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 new file mode 100644 index 000000000000..e1c5858604ed --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering--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/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 new file mode 100644 index 000000000000..38bf12b34320 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern-with-rest/exec.js @@ -0,0 +1,26 @@ +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 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")]: 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: 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 new file mode 100644 index 000000000000..f459199da656 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/array-pattern/exec.js @@ -0,0 +1,22 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + 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")]: 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: 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 new file mode 100644 index 000000000000..47b7210eb0eb --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern-with-rest/exec.js @@ -0,0 +1,23 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + 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")]: 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: 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 new file mode 100644 index 000000000000..e7a7d9d08e11 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/ordering/object-pattern/exec.js @@ -0,0 +1,22 @@ +var log = []; + +function push(x, y = x) { log.push(x); return y; } + +class C { + 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")]: 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: 15 }, (_, 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..c033e98f1b8e --- /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 features 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..f7222cd31345 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/typescript/valid-before-destructuring-private/output.js @@ -0,0 +1,9 @@ +class C { + #x; + + constructor(foo, _p) { + var x = _p.#x; + this.foo = foo; + } + +} 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..34b1fbe5524c --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest-only/output.js @@ -0,0 +1,21 @@ +const _excluded = ["0"]; +let result; + +class C {} + +var _x = { + writable: true, + value: void 0 +}; + +(() => { + var [..._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/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..8c9aeb90baa6 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/array-rest/output.js @@ -0,0 +1,13 @@ +class C {} + +var _x = { + writable: true, + value: void 0 +}; + +(() => { + 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/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..6d62674ff6ca --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/nested-under-array-pattern/output.js @@ -0,0 +1,28 @@ +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 [_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/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..0579a8964deb --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration--es2015/non-identifier-keys/output.js @@ -0,0 +1,10 @@ +class C { + static #x; + static { + var _m = [C, C, C, C], + 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--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--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-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..7930e97e528f --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest-only/output.js @@ -0,0 +1,16 @@ +const _excluded = ["0"]; +let result; + +class C { + static #x; + static { + var [..._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/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..203a6d1b1fc3 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/array-rest/output.js @@ -0,0 +1,9 @@ +class C { + static #x; + static { + 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/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..634cc2709bd9 --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/nested-under-array-pattern/output.js @@ -0,0 +1,19 @@ +class C { + static #x = "x"; + static #y = []; + static #z; + static self = C; + + static #self() { + return C; + } + + static { + var [_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/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..0579a8964deb --- /dev/null +++ b/packages/babel-plugin-proposal-destructuring-private/test/fixtures/variable-declaration/non-identifier-keys/output.js @@ -0,0 +1,10 @@ +class C { + static #x; + static { + var _m = [C, C, C, C], + 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/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 + }; + } +} 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/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-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" + } } diff --git a/packages/babel-plugin-transform-destructuring/src/index.ts b/packages/babel-plugin-transform-destructuring/src/index.ts index 0726446b0053..5adeda9e870f 100644 --- a/packages/babel-plugin-transform-destructuring/src/index.ts +++ b/packages/babel-plugin-transform-destructuring/src/index.ts @@ -4,7 +4,9 @@ import { DestructuringTransformer, convertVariableDeclaration, convertAssignmentExpression, + unshiftForXStatementBody, } from "./util"; +export { buildObjectExcludingKeys, unshiftForXStatementBody } from "./util"; /** * Test if a VariableDeclaration's declarations contains any Patterns. @@ -80,17 +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 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()) { - statementBody.unshift( - t.expressionStatement(scope.buildUndefinedNode()), - ); + nodes.unshift(t.expressionStatement(scope.buildUndefinedNode())); } - statementBody.unshift( - t.expressionStatement(t.assignmentExpression("=", left, temp)), + nodes.unshift( + t.expressionStatement( + t.assignmentExpression("=", left, t.cloneNode(temp)), + ), ); + unshiftForXStatementBody(path, nodes); scope.crawl(); return; } @@ -120,11 +127,7 @@ export default declare((api, options: Options) => { destructuring.init(pattern, key); - path.ensureBlock(); - - const block = node.body; - // @ts-expect-error: ensureBlock ensures that node.body is a BlockStatement - block.body = nodes.concat(block.body); + unshiftForXStatementBody(path, nodes); scope.crawl(); }, diff --git a/packages/babel-plugin-transform-destructuring/src/util.ts b/packages/babel-plugin-transform-destructuring/src/util.ts index a973b5600904..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. */ @@ -290,7 +311,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 +654,7 @@ export function convertAssignmentExpression( objectRestNoSymbols: boolean, useBuiltIns: boolean, ) { - const { node, scope } = path; + const { node, scope, parentPath } = path; const nodes = []; @@ -646,7 +670,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 +689,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; diff --git a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-block-scoped/exec.js b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-block-scoped/exec.js new file mode 100644 index 000000000000..e3f8f6f30ea2 --- /dev/null +++ b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-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-shadowed-block-scoped/input.js b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-block-scoped/input.js new file mode 100644 index 000000000000..e3f8f6f30ea2 --- /dev/null +++ b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-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-shadowed-block-scoped/options.json b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-block-scoped/options.json new file mode 100644 index 000000000000..84e28c07f51e --- /dev/null +++ b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-block-scoped/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["transform-destructuring"] +} diff --git a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-block-scoped/output.js b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-block-scoped/output.js new file mode 100644 index 000000000000..23908dee29eb --- /dev/null +++ b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/for-of-shadowed-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"; + } +} 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-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; +}); 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/packages/babel-traverse/src/path/introspection.ts b/packages/babel-traverse/src/path/introspection.ts index e5f7e7b49368..e55c161f3ef8 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; } @@ -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; } 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); + }); }); }); diff --git a/packages/babel-types/src/ast-types/generated/index.ts b/packages/babel-types/src/ast-types/generated/index.ts index 6083ec271003..ac7c27b4f7b8 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; })(), 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" ], 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:^"