From 05611f659a4b547959ac8f70920775eb18da3dc5 Mon Sep 17 00:00:00 2001 From: Chris Garrett Date: Mon, 24 Jan 2022 17:02:16 -0500 Subject: [PATCH] Add new decorators transform (#14004) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Nicolò Ribaudo Co-authored-by: Huáng Jùnliàng --- Makefile | 18 +- babel.config.js | 5 +- .../src/decorators.ts | 5 +- .../src/fields.ts | 7 +- .../src/index.ts | 7 +- .../babel-helpers/src/helpers-generated.ts | 4 + packages/babel-helpers/src/helpers.ts | 6 + .../babel-helpers/src/helpers/applyDecs.js | 731 ++++++++++++ .../CONTRIB.md | 396 +++++++ .../package.json | 4 +- .../src/index.ts | 54 +- .../src/transformer-2021-12.ts | 1029 +++++++++++++++++ .../2021-12-accessors--to-es2015/options.json | 11 + .../private/exec.js | 54 + .../private/input.js | 7 + .../private/output.js | 59 + .../public/exec.js | 75 ++ .../public/input.js | 10 + .../public/output.js | 58 + .../static-private/exec.js | 52 + .../static-private/input.js | 7 + .../static-private/output.js | 58 + .../static-public/exec.js | 70 ++ .../static-public/input.js | 10 + .../static-public/output.js | 52 + .../undecorated-private/exec.js | 31 + .../undecorated-private/input.js | 5 + .../undecorated-private/output.js | 45 + .../undecorated-public/exec.js | 27 + .../undecorated-public/input.js | 7 + .../undecorated-public/output.js | 47 + .../undecorated-static-private/exec.js | 29 + .../undecorated-static-private/input.js | 5 + .../undecorated-static-private/output.js | 34 + .../undecorated-static-public/exec.js | 22 + .../undecorated-static-public/input.js | 7 + .../undecorated-static-public/output.js | 39 + .../fixtures/2021-12-accessors/options.json | 8 + .../2021-12-accessors/private/input.js | 7 + .../2021-12-accessors/private/output.js | 35 + .../2021-12-accessors/public/input.js | 10 + .../2021-12-accessors/public/output.js | 42 + .../2021-12-accessors/static-private/input.js | 7 + .../static-private/output.js | 38 + .../2021-12-accessors/static-public/input.js | 10 + .../2021-12-accessors/static-public/output.js | 45 + .../undecorated-private/input.js | 5 + .../undecorated-private/output.js | 22 + .../undecorated-public/input.js | 7 + .../undecorated-public/output.js | 32 + .../undecorated-static-private/input.js | 5 + .../undecorated-static-private/output.js | 22 + .../undecorated-static-public/input.js | 7 + .../undecorated-static-public/output.js | 32 + .../options.json | 11 + .../input.js | 6 + .../output.js | 18 + .../super-in-private-accessor/input.js | 6 + .../super-in-private-accessor/output.js | 20 + .../super-in-private-method/input.js | 6 + .../super-in-private-method/output.js | 17 + .../expressions/input.js | 11 + .../expressions/output.js | 51 + .../inheritance/exec.js | 18 + .../inheritance/input.js | 5 + .../inheritance/output.js | 25 + .../initializers/exec.js | 41 + .../initializers/input.js | 13 + .../initializers/output.js | 37 + .../2021-12-classes--to-es2015/options.json | 11 + .../exec.js | 33 + .../input.js | 15 + .../output.js | 32 + .../replacement-static-this/exec.js | 19 + .../replacement-static-this/input.js | 10 + .../replacement-static-this/output.js | 22 + .../replacement-with-expr/exec.js | 17 + .../replacement-with-expr/input.js | 6 + .../replacement-with-expr/output.js | 13 + .../replacement/exec.js | 19 + .../replacement/input.js | 6 + .../replacement/output.js | 19 + .../2021-12-classes/expressions/input.js | 11 + .../2021-12-classes/expressions/output.js | 87 ++ .../2021-12-classes/inheritance/input.js | 5 + .../2021-12-classes/inheritance/output.js | 25 + .../2021-12-classes/initializers/input.js | 13 + .../2021-12-classes/initializers/output.js | 41 + .../fixtures/2021-12-classes/options.json | 8 + .../input.js | 15 + .../output.js | 33 + .../replacement-static-this/input.js | 10 + .../replacement-static-this/output.js | 24 + .../replacement-with-expr/input.js | 6 + .../replacement-with-expr/output.js | 13 + .../2021-12-classes/replacement/input.js | 6 + .../2021-12-classes/replacement/output.js | 21 + .../computed-keys-same-ast/exec.js | 33 + .../computed-keys-same-ast/input.js | 11 + .../computed-keys-same-ast/output.js | 25 + .../computed-keys-same-value/exec.js | 29 + .../computed-keys-same-value/input.js | 11 + .../computed-keys-same-value/output.js | 25 + .../method-and-field/exec.js | 23 + .../method-and-field/input.js | 9 + .../method-and-field/output.js | 16 + .../methods-with-same-key/exec.js | 19 + .../methods-with-same-key/input.js | 11 + .../methods-with-same-key/output.js | 20 + .../options.json | 11 + .../computed-keys-same-ast/input.js | 11 + .../computed-keys-same-ast/output.js | 25 + .../computed-keys-same-value/input.js | 11 + .../computed-keys-same-value/output.js | 25 + .../method-and-field/input.js | 9 + .../method-and-field/output.js | 13 + .../methods-with-same-key/input.js | 11 + .../methods-with-same-key/output.js | 20 + .../2021-12-duplicated-keys/options.json | 8 + .../2021-12-fields--to-es2015/options.json | 11 + .../2021-12-fields--to-es2015/private/exec.js | 41 + .../private/input.js | 7 + .../private/output.js | 31 + .../2021-12-fields--to-es2015/public/exec.js | 62 + .../2021-12-fields--to-es2015/public/input.js | 10 + .../public/output.js | 19 + .../static-private/exec.js | 39 + .../static-private/input.js | 7 + .../static-private/output.js | 24 + .../static-public/exec.js | 57 + .../static-public/input.js | 10 + .../static-public/output.js | 16 + .../test/fixtures/2021-12-fields/options.json | 8 + .../fixtures/2021-12-fields/private/input.js | 7 + .../fixtures/2021-12-fields/private/output.js | 17 + .../fixtures/2021-12-fields/public/input.js | 10 + .../fixtures/2021-12-fields/public/output.js | 15 + .../2021-12-fields/static-private/input.js | 7 + .../2021-12-fields/static-private/output.js | 17 + .../2021-12-fields/static-public/input.js | 10 + .../2021-12-fields/static-public/output.js | 15 + .../2021-12-getters--to-es2015/options.json | 11 + .../private/exec.js | 39 + .../private/input.js | 12 + .../private/output.js | 30 + .../2021-12-getters--to-es2015/public/exec.js | 50 + .../public/input.js | 13 + .../public/output.js | 26 + .../static-private/exec.js | 38 + .../static-private/input.js | 12 + .../static-private/output.js | 27 + .../static-public/exec.js | 48 + .../static-public/input.js | 13 + .../static-public/output.js | 24 + .../options.json | 11 + .../private/exec.js | 67 ++ .../private/input.js | 21 + .../private/output.js | 40 + .../public/exec.js | 88 ++ .../public/input.js | 23 + .../public/output.js | 37 + .../static-private/exec.js | 65 ++ .../static-private/input.js | 21 + .../static-private/output.js | 37 + .../static-public/exec.js | 87 ++ .../static-public/input.js | 23 + .../static-public/output.js | 35 + .../2021-12-getters-and-setters/options.json | 8 + .../private/input.js | 21 + .../private/output.js | 34 + .../public/input.js | 23 + .../public/output.js | 37 + .../static-private/input.js | 21 + .../static-private/output.js | 32 + .../static-public/input.js | 23 + .../static-public/output.js | 35 + .../fixtures/2021-12-getters/options.json | 8 + .../fixtures/2021-12-getters/private/input.js | 12 + .../2021-12-getters/private/output.js | 24 + .../fixtures/2021-12-getters/public/input.js | 13 + .../fixtures/2021-12-getters/public/output.js | 26 + .../2021-12-getters/static-private/input.js | 12 + .../2021-12-getters/static-private/output.js | 22 + .../2021-12-getters/static-public/input.js | 13 + .../2021-12-getters/static-public/output.js | 24 + .../2021-12-metadata--to-es2015/class/exec.js | 10 + .../getting-previously-set-metadata/exec.js | 30 + .../inheritance-private/exec.js | 16 + .../inheritance-public/exec.js | 18 + .../non-symbol-keys/exec.js | 10 + .../2021-12-metadata--to-es2015/options.json | 11 + .../proto-private/exec.js | 11 + .../proto-public/exec.js | 11 + .../static-private/exec.js | 11 + .../static-public/exec.js | 11 + .../2021-12-methods--to-es2015/options.json | 11 + .../private/exec.js | 41 + .../private/input.js | 12 + .../private/output.js | 26 + .../2021-12-methods--to-es2015/public/exec.js | 50 + .../public/input.js | 13 + .../public/output.js | 26 + .../static-private/exec.js | 39 + .../static-private/input.js | 12 + .../static-private/output.js | 22 + .../static-public/exec.js | 48 + .../static-public/input.js | 13 + .../static-public/output.js | 24 + .../fixtures/2021-12-methods/options.json | 8 + .../fixtures/2021-12-methods/private/input.js | 12 + .../2021-12-methods/private/output.js | 21 + .../fixtures/2021-12-methods/public/input.js | 13 + .../fixtures/2021-12-methods/public/output.js | 26 + .../2021-12-methods/static-private/input.js | 12 + .../2021-12-methods/static-private/output.js | 19 + .../2021-12-methods/static-public/input.js | 13 + .../2021-12-methods/static-public/output.js | 24 + .../exec.js | 28 + .../input.js | 10 + .../output.js | 18 + .../initializer-timing/exec.js | 34 + .../2021-12-misc--to-es2015/options.json | 11 + .../valid-expression-formats/input.js | 22 + .../valid-expression-formats/output.js | 40 + .../2021-12-misc/all-decorators/input.js | 26 + .../2021-12-misc/all-decorators/output.js | 113 ++ .../input.js | 23 + .../output.js | 33 + .../input.js | 10 + .../output.js | 18 + .../test/fixtures/2021-12-misc/options.json | 8 + .../private-keys-in-enclosing-class/input.js | 10 + .../private-keys-in-enclosing-class/output.js | 19 + .../input.js | 7 + .../options.json | 3 + .../input.js | 7 + .../options.json | 3 + .../input.js | 7 + .../options.json | 3 + .../setting-private-method-via-rest/input.js | 7 + .../options.json | 3 + .../input.js | 7 + .../options.json | 3 + .../setting-private-method/input.js | 7 + .../setting-private-method/options.json | 3 + .../input.js | 6 + .../output.js | 18 + .../super-in-private-accessor/input.js | 6 + .../super-in-private-accessor/output.js | 20 + .../super-in-private-method/input.js | 6 + .../super-in-private-method/output.js | 17 + .../valid-expression-formats/input.js | 22 + .../valid-expression-formats/output.js | 33 + .../2021-12-setters--to-es2015/options.json | 11 + .../private/exec.js | 40 + .../private/input.js | 12 + .../private/output.js | 30 + .../2021-12-setters--to-es2015/public/exec.js | 50 + .../public/input.js | 13 + .../public/output.js | 26 + .../static-private/exec.js | 38 + .../static-private/input.js | 12 + .../static-private/output.js | 27 + .../static-public/exec.js | 49 + .../static-public/input.js | 13 + .../static-public/output.js | 24 + .../fixtures/2021-12-setters/options.json | 8 + .../fixtures/2021-12-setters/private/input.js | 12 + .../2021-12-setters/private/output.js | 24 + .../fixtures/2021-12-setters/public/input.js | 13 + .../fixtures/2021-12-setters/public/output.js | 26 + .../2021-12-setters/static-private/input.js | 12 + .../2021-12-setters/static-private/output.js | 22 + .../2021-12-setters/static-public/input.js | 13 + .../2021-12-setters/static-public/output.js | 24 + .../src/index.ts | 26 +- packages/babel-runtime-corejs2/package.json | 18 + packages/babel-runtime-corejs3/package.json | 18 + packages/babel-runtime/package.json | 18 + .../babel-standalone/src/preset-stage-0.ts | 4 +- .../babel-standalone/src/preset-stage-1.ts | 4 +- .../babel-standalone/src/preset-stage-2.ts | 6 +- .../test/preset-stage-1.test.js | 15 + .../src/ast-types/generated/index.ts | 2 + .../src/builders/generated/index.ts | 2 + packages/babel-types/src/definitions/core.ts | 2 + yarn.lock | 2 + 287 files changed, 8136 insertions(+), 63 deletions(-) create mode 100644 packages/babel-helpers/src/helpers/applyDecs.js create mode 100644 packages/babel-plugin-proposal-decorators/CONTRIB.md create mode 100644 packages/babel-plugin-proposal-decorators/src/transformer-2021-12.ts create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/private/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/public/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-private/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-public/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-private/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-public/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-private/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-public/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/static-private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/static-private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/static-public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/static-public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-static-private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-static-private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-static-public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-static-public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-nested-constructor-expression/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-nested-constructor-expression/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-private-accessor/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-private-accessor/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-private-method/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-private-method/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/expressions/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/expressions/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/inheritance/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/inheritance/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/inheritance/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/initializers/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/initializers/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/initializers/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-installed-on-correct-class/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-installed-on-correct-class/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-installed-on-correct-class/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-this/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-this/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-this/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-with-expr/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-with-expr/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-with-expr/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/expressions/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/expressions/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/inheritance/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/inheritance/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/initializers/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/initializers/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-static-installed-on-correct-class/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-static-installed-on-correct-class/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-static-this/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-static-this/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-with-expr/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-with-expr/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-ast/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-ast/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-ast/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-value/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-value/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-value/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/method-and-field/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/method-and-field/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/method-and-field/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/methods-with-same-key/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/methods-with-same-key/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/methods-with-same-key/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/computed-keys-same-ast/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/computed-keys-same-ast/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/computed-keys-same-value/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/computed-keys-same-value/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/method-and-field/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/method-and-field/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/methods-with-same-key/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/methods-with-same-key/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/private/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/public/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-private/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-public/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/static-private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/static-private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/static-public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/static-public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/private/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/public/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-private/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-public/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/private/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/public/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-private/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-public/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/static-private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/static-private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/static-public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/static-public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/static-private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/static-private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/static-public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/static-public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/class/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/getting-previously-set-metadata/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/inheritance-private/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/inheritance-public/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/non-symbol-keys/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/proto-private/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/proto-public/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/static-private/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/static-public/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/private/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/public/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-private/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-public/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/static-private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/static-private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/static-public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/static-public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/initProto-existing-derived-constructor/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/initProto-existing-derived-constructor/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/initProto-existing-derived-constructor/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/initializer-timing/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/valid-expression-formats/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/valid-expression-formats/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/all-decorators/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/all-decorators/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor-multiple-super/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor-multiple-super/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/private-keys-in-enclosing-class/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/private-keys-in-enclosing-class/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-array-pattern/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-array-pattern/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-for-of/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-for-of/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-object-pattern/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-object-pattern/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-rest/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-rest/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-update/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-update/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-nested-constructor-expression/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-nested-constructor-expression/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-private-accessor/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-private-accessor/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-private-method/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-private-method/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/valid-expression-formats/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/valid-expression-formats/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/private/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/public/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-private/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-public/exec.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/public/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/static-private/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/static-private/output.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/static-public/input.js create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/static-public/output.js diff --git a/Makefile b/Makefile index a314bf79d22b..13380d88033c 100644 --- a/Makefile +++ b/Makefile @@ -187,15 +187,15 @@ prepublish: IS_PUBLISH=true $(MAKE) test new-version-checklist: - # @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" - # @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" - # @echo "!!!!!! !!!!!!" - # @echo "!!!!!! Write any message that should !!!!!!" - # @echo "!!!!!! block the release here !!!!!!" - # @echo "!!!!!! !!!!!!" - # @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" - # @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" - # @exit 1 + @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + @echo "!!!!!! !!!!!!" + @echo "!!!!!! Update the version of the "identity" !!!!!!" + @echo "!!!!!! and "applyDecs" helpers to 7.17.0 !!!!!!" + @echo "!!!!!! !!!!!!" + @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + @exit 1 new-version: $(MAKE) new-version-checklist diff --git a/babel.config.js b/babel.config.js index cacd451a7489..890f6c9a6e23 100644 --- a/babel.config.js +++ b/babel.config.js @@ -194,7 +194,10 @@ module.exports = function (api) { assumptions: parserAssumptions, }, { - test: ["packages/babel-generator"].map(normalize), + test: [ + "packages/babel-generator", + "packages/babel-plugin-proposal-decorators", + ].map(normalize), plugins: ["babel-plugin-transform-charcodes"], }, convertESM && { diff --git a/packages/babel-helper-create-class-features-plugin/src/decorators.ts b/packages/babel-helper-create-class-features-plugin/src/decorators.ts index d4175e68a395..432439aed912 100644 --- a/packages/babel-helper-create-class-features-plugin/src/decorators.ts +++ b/packages/babel-helper-create-class-features-plugin/src/decorators.ts @@ -80,7 +80,10 @@ function extractElementDescriptor( const properties: t.ObjectExpression["properties"] = [ prop("kind", t.stringLiteral(t.isClassMethod(node) ? node.kind : "field")), prop("decorators", takeDecorators(node as Decorable)), - prop("static", node.static && t.booleanLiteral(true)), + prop( + "static", + !t.isStaticBlock(node) && node.static && t.booleanLiteral(true), + ), prop("key", getKey(node)), ].filter(Boolean); diff --git a/packages/babel-helper-create-class-features-plugin/src/fields.ts b/packages/babel-helper-create-class-features-plugin/src/fields.ts index cbaee3f1ca2d..bebb6a107cc9 100644 --- a/packages/babel-helper-create-class-features-plugin/src/fields.ts +++ b/packages/babel-helper-create-class-features-plugin/src/fields.ts @@ -911,7 +911,7 @@ function replaceThisContext( getSuperRef, getObjectRef() { state.needsClassRef = true; - return isStaticBlock || path.node.static + return t.isStaticBlock(path.node) || path.node.static ? ref : t.memberExpression(ref, t.identifier("prototype")); }, @@ -931,7 +931,8 @@ function replaceThisContext( export type PropNode = | t.ClassProperty | t.ClassPrivateMethod - | t.ClassPrivateProperty; + | t.ClassPrivateProperty + | t.StaticBlock; export type PropPath = NodePath; export function buildFieldsInitNodes( @@ -963,7 +964,7 @@ export function buildFieldsInitNodes( for (const prop of props) { prop.isClassProperty() && ts.assertFieldTransformed(prop); - const isStatic = prop.node.static; + const isStatic = !t.isStaticBlock(prop.node) && prop.node.static; const isInstance = !isStatic; const isPrivate = prop.isPrivate(); const isPublic = !isPrivate; diff --git a/packages/babel-helper-create-class-features-plugin/src/index.ts b/packages/babel-helper-create-class-features-plugin/src/index.ts index 052ef00c47aa..2c67ed06ac55 100644 --- a/packages/babel-helper-create-class-features-plugin/src/index.ts +++ b/packages/babel-helper-create-class-features-plugin/src/index.ts @@ -33,6 +33,7 @@ interface Options { name: string; feature: number; loose?: boolean; + inherits?: (api: any, options: any) => any; // same as PluginObject.manipulateOptions manipulateOptions: (options: unknown, parserOpts: ParserOptions) => void; // TODO(flow->ts): change to babel api @@ -46,6 +47,7 @@ export function createClassFeaturePlugin({ manipulateOptions, // TODO(Babel 8): Remove the default value api = { assumption: () => void 0 }, + inherits, }: Options) { const setPublicClassFields = api.assumption("setPublicClassFields"); const privateFieldsAsProperties = api.assumption("privateFieldsAsProperties"); @@ -80,6 +82,7 @@ export function createClassFeaturePlugin({ return { name, manipulateOptions, + inherits, pre() { enableFeature(this.file, feature, loose); @@ -163,7 +166,7 @@ export function createClassFeaturePlugin({ path.isPrivate() || path.isStaticBlock?.() ) { - props.push(path); + props.push(path as PropPath); } } } @@ -239,7 +242,7 @@ export function createClassFeaturePlugin({ (referenceVisitor, state) => { if (isDecorated) return; for (const prop of props) { - if (prop.node.static) continue; + if (t.isStaticBlock(prop.node) || prop.node.static) continue; prop.traverse(referenceVisitor, state); } }, diff --git a/packages/babel-helpers/src/helpers-generated.ts b/packages/babel-helpers/src/helpers-generated.ts index 1f148f411cae..0ebee43d1889 100644 --- a/packages/babel-helpers/src/helpers-generated.ts +++ b/packages/babel-helpers/src/helpers-generated.ts @@ -13,6 +13,10 @@ function helper(minVersion, source) { } export default Object.freeze({ + applyDecs: helper( + "7.16.6", + 'function createMetadataMethodsForProperty(metadataMap,kind,property){return{getMetadata(key){if("symbol"!=typeof key)throw new TypeError("Metadata keys must be symbols, received: "+key);var metadataForKey=metadataMap[key];if(void 0!==metadataForKey)if(1===kind){var pub=metadataForKey.public;if(void 0!==pub)return pub[property]}else if(2===kind){var priv=metadataForKey.private;if(void 0!==priv)return priv.get(property)}else if(Object.hasOwnProperty.call(metadataForKey,"constructor"))return metadataForKey.constructor},setMetadata(key,value){if("symbol"!=typeof key)throw new TypeError("Metadata keys must be symbols, received: "+key);var metadataForKey=metadataMap[key];if(void 0===metadataForKey&&(metadataForKey=metadataMap[key]={}),1===kind){var pub=metadataForKey.public;void 0===pub&&(pub=metadataForKey.public=Object.create(null)),pub[property]=value}else if(2===kind){var priv=metadataForKey.priv;void 0===priv&&(priv=metadataForKey.private=new Map),priv.set(property,value)}else metadataForKey.constructor=value}}}function convertMetadataMapToFinal(obj,metadataMap){var parentMetadataMap=obj[Symbol.metadata||Symbol.for("Symbol.metadata")],metadataKeys=Object.getOwnPropertySymbols(metadataMap);if(0!==metadataKeys.length){for(var i=0;i3,isStatic=kind>=5;if(isStatic?(base=Class,metadataMap=staticMetadataMap,kind-=5,staticInitializers||(staticInitializers=[]),initializers=staticInitializers):(base=Class.prototype,metadataMap=protoMetadataMap,protoInitializers||(protoInitializers=[]),initializers=protoInitializers),0!==kind&&!isPrivate){var existingNonFields=isStatic?existingStaticNonFields:existingProtoNonFields,existingKind=existingNonFields.get(name)||0;if(!0===existingKind||3===existingKind&&4!==kind||4===existingKind&&3!==kind)throw new Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: "+name);!existingKind&&kind>2?existingNonFields.set(name,kind):existingNonFields.set(name,!0)}applyMemberDec(ret,base,decInfo,name,kind,isStatic,isPrivate,metadataMap,initializers)}}protoInitializers&&pushInitializers(ret,protoInitializers),staticInitializers&&pushInitializers(ret,staticInitializers)}function pushInitializers(ret,initializers){initializers.length>0?(initializers=initializers.slice(),ret.push((function(instance){for(var i=0;i0?ret.push((function(){for(var i=0;i 3; + + var isStatic = kind >= 5; /* STATIC */ + var base; + var metadataMap; + var initializers; + + if (isStatic) { + base = Class; + metadataMap = staticMetadataMap; + kind = kind - 5 /* STATIC */; + + if (!staticInitializers) { + staticInitializers = []; + } + + initializers = staticInitializers; + } else { + base = Class.prototype; + metadataMap = protoMetadataMap; + + if (!protoInitializers) { + protoInitializers = []; + } + + initializers = protoInitializers; + } + + if (kind !== 0 /* FIELD */ && !isPrivate) { + var existingNonFields = isStatic + ? existingStaticNonFields + : existingProtoNonFields; + + var existingKind = existingNonFields.get(name) || 0; + + if ( + existingKind === true || + (existingKind === 3 /* GETTER */ && kind !== 4) /* SETTER */ || + (existingKind === 4 /* SETTER */ && kind !== 3) /* GETTER */ + ) { + throw new Error( + "Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: " + + name + ); + } else if (!existingKind && kind > 2 /* METHOD */) { + existingNonFields.set(name, kind); + } else { + existingNonFields.set(name, true); + } + } + + applyMemberDec( + ret, + base, + decInfo, + name, + kind, + isStatic, + isPrivate, + metadataMap, + initializers + ); + } + + if (protoInitializers) { + pushInitializers(ret, protoInitializers); + } + + if (staticInitializers) { + pushInitializers(ret, staticInitializers); + } +} + +function pushInitializers(ret, initializers) { + if (initializers.length > 0) { + // Slice the array, which means that `addInitializer` can no longer add + // additional initializers to the array + initializers = initializers.slice(); + + ret.push(function (instance) { + for (var i = 0; i < initializers.length; i++) { + initializers[i].call(instance, instance); + } + return instance; + }); + } else { + ret.push(function (instance) { + return instance; + }); + } +} + +function applyClassDecs(ret, targetClass, metadataMap, classDecs) { + var initializers = []; + var newClass = targetClass; + + var name = targetClass.name; + var ctx = Object.assign( + { + kind: "class", + name: name, + addInitializer: createAddInitializerMethod(initializers), + }, + createMetadataMethodsForProperty(metadataMap, 0 /* CONSTRUCTOR */, name) + ); + + for (var i = 0; i < classDecs.length; i++) { + newClass = classDecs[i](newClass, ctx) || newClass; + } + + ret.push(newClass); + + if (initializers.length > 0) { + ret.push(function () { + for (var i = 0; i < initializers.length; i++) { + initializers[i].call(newClass, newClass); + } + }); + } else { + ret.push(function () {}); + } +} + +/** + Basic usage: + + applyDecs( + Class, + [ + // member decorators + [ + dec, // dec or array of decs + 0, // kind of value being decorated + 'prop', // name of public prop on class containing the value being decorated, + '#p', // the name of the private property (if is private, void 0 otherwise), + ] + ], + [ + // class decorators + dec1, dec2 + ] + ) + ``` + + Fully transpiled example: + + ```js + @dec + class Class { + @dec + a = 123; + + @dec + #a = 123; + + @dec + @dec2 + accessor b = 123; + + @dec + accessor #b = 123; + + @dec + c() { console.log('c'); } + + @dec + #c() { console.log('privC'); } + + @dec + get d() { console.log('d'); } + + @dec + get #d() { console.log('privD'); } + + @dec + set e(v) { console.log('e'); } + + @dec + set #e(v) { console.log('privE'); } + } + + + // becomes + let initializeInstance; + let initializeClass; + + let initA; + let initPrivA; + + let initB; + let initPrivB, getPrivB, setPrivB; + + let privC; + let privD; + let privE; + + let Class; + class _Class { + static { + let ret = applyDecs( + this, + [ + [dec, 0, 'a'], + [dec, 0, 'a', (i) => i.#a, (i, v) => i.#a = v], + [[dec, dec2], 1, 'b'], + [dec, 1, 'b', (i) => i.#privBData, (i, v) => i.#privBData = v], + [dec, 2, 'c'], + [dec, 2, 'c', () => console.log('privC')], + [dec, 3, 'd'], + [dec, 3, 'd', () => console.log('privD')], + [dec, 4, 'e'], + [dec, 4, 'e', () => console.log('privE')], + ], + [ + dec + ] + ) + + initA = ret[0]; + + initPrivA = ret[1]; + + initB = ret[2]; + + initPrivB = ret[3]; + getPrivB = ret[4]; + setPrivB = ret[5]; + + privC = ret[6]; + + privD = ret[7]; + + privE = ret[8]; + + initializeInstance = ret[9]; + + Class = ret[10] + + initializeClass = ret[11]; + } + + a = (initializeInstance(this), initA(this, 123)); + + #a = initPrivA(this, 123); + + #bData = initB(this, 123); + get b() { return this.#bData } + set b(v) { this.#bData = v } + + #privBData = initPrivB(this, 123); + get #b() { return getPrivB(this); } + set #b(v) { setPrivB(this, v); } + + c() { console.log('c'); } + + #c(...args) { return privC(this, ...args) } + + get d() { console.log('d'); } + + get #d() { return privD(this); } + + set e(v) { console.log('e'); } + + set #e(v) { privE(this, v); } + } + + initializeClass(Class); + */ +export default function applyDecs(targetClass, memberDecs, classDecs) { + var ret = []; + var staticMetadataMap = {}; + + if (memberDecs) { + var protoMetadataMap = {}; + + applyMemberDecs( + ret, + targetClass, + protoMetadataMap, + staticMetadataMap, + memberDecs + ); + + convertMetadataMapToFinal(targetClass.prototype, protoMetadataMap); + } + + if (classDecs) { + applyClassDecs(ret, targetClass, staticMetadataMap, classDecs); + } + + convertMetadataMapToFinal(targetClass, staticMetadataMap); + + return ret; +} diff --git a/packages/babel-plugin-proposal-decorators/CONTRIB.md b/packages/babel-plugin-proposal-decorators/CONTRIB.md new file mode 100644 index 000000000000..33b8fb85da17 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/CONTRIB.md @@ -0,0 +1,396 @@ +These are notes about the implementation of the 2021-12 decorators transform. +The implementation's goals are (in descending order): + +1. Being accurate to the actual proposal (e.g. not defining additional + properties unless required, matching semantics exactly, etc.). This includes + being able to work properly with private fields and methods. +2. Transpiling to a very minimal and minifiable output. This transform will + affect each and every decorated class, so ensuring that the output is not 10x + the size of the original is important. +3. Having good runtime performance. Decoration output has the potential to + drastically impact startup performance, since it runs whenever a decorated + class is defined. In addition, every instance of a decorated class may be + impacted for certain types of decorators. + +All of these goals come somewhat at the expense of readability and can make the +implementation difficult to understand, so these notes are meant to document the +motivations behind the design. + +## Overview + +Given a simple decorated class like this one: + +```js +@dec +class Class { + @dec a = 123; + + @dec static #b() { + console.log('foo'); + } + + [someVal]() {} + + @dec + @dec2 + accessor #c = 456; +} +``` + +It's output would be something like the following: + +```js +import { applyDecs } from '@babel/helpers'; + +let _initInstance, _initClass, _initStatic, _init_a, _call_b, _computedKey, _init_c, _get_c, _set_c; + +let _dec = dec, + _dec2 = dec, + _computedKey = someVal, + _dec3 = dec, + _dec4 = dec2; + +let _Class; +class Class { + static { + [ + _init_a, + _call_b, + _init_c, + _get_c, + _set_c, + _Class, + _initClass, + _initProto, + _initStatic, + ] = _applyDecs(Class, + [ + [_dec, 0, "a"], + [ + _dec2, + 7, + "b", + function () { + console.log('foo'); + } + ], + [ + [_dec4, _dec5], + 1, + "c", + function () { + return this.#a; + }, + function (value) { + this.#a = value; + } + ] + ], + [dec] + ); + + _initStatic(Class); + } + + a = (initInstance(this), _init_a(this, 123)); + + static #b(...args) { + _call_b(this, args); + } + + [_computedKey]() {} + + #a = _init_c(this, 123); + get #c() { + return _get_c(this); + } + set #c(v) { + _set_c(this, v); + } + + static { + initClass(C); + } +} +``` + +Let's break this output down a bit: + +```js +let initInstance, initClass, _init_a, _call_b, _init_c, _get_c, _set_c; +``` + +First, we need to setup some local variables outside of the class. These are +for: + +- Decorated class field/accessor initializers +- Extra initializer functions added by `addInitializers` +- Private class methods + +These are essentially all values that cannot be defined on the class itself via +`Object.defineProperty`, so we have to insert them into the class manually, +ahead of time and populate them when we run our decorators. + +```js +let _dec = dec, + _dec2 = dec, + _computedKey = someVal, + _dec3 = dec, + _dec4 = dec2; +``` + +Next up, we define and evaluate the decorator expressions. The reason we +do this _before_ defining the class is because we must interleave decorator +expressions with computed property key expressions, since computed properties +and decorators can run arbitrary code which can modify the runtime of subsequent +decorators or computed property keys. + +```js +let _Class; +class Class { +``` + +This class is being decorated directly, which means that the decorator may +replace the class itself. Class bindings are not mutable, so we need to create a +new `let` variable for the decorated class. + + +```js + static { + [ + _init_a, + _call_b, + _init_c, + _get_c, + _set_c, + _Class, + _initClass, + _initProto, + _initStatic, + ] = _applyDecs(Class, + [ + [_dec, 0, "a"], + [ + _dec2, + 7, + "b", + function () { + console.log('foo'); + } + ], + [ + [_dec4, _dec5], + 1, + "c", + function () { + return this.#a; + }, + function (value) { + this.#a = value; + } + ] + ], + [dec] + ); + + _initStatic(Class); + } +``` + +Next, we immediately define a `static` block which actually applies the +decorators. This is important because we must apply the decorators _after_ the +class prototype has been fully setup, but _before_ static fields are run, since +static fields should only see the decorated version of the class. + +We apply the decorators to class elements and the class itself, and the +application returns an array of values that are used to populate all of the +local variables we defined earlier. The array's order is fully deterministic, so +we can assign the values based on an index we can calculate ahead of time. + +Another important thing to note here is that we're passing some functions here. +These are for private methods and accessors, which cannot be replaced directly +so we have to extract their code so it can be decorated. Because we define these +within the static block, they can access any private identifiers which were +defined within the class, so it's not an issue that we're extracting the method +logic here. + +We'll come back to `applyDecs` in a bit to dig into what its format is exactly, +but now let's dig into the new definitions of our class elements. + +```js + a = (_initInstance(this), _init_a(this, 123)); +``` + +Alright, so previously this was a simple class field. Since it's the first field +on the class, we've updated it to immediately call `initInstance` in its +initializer. This calls any initializers added with `addInitializer` for all of +the per-class values (methods and accessors), which should all be setup on the +instance before class fields are assigned. Then, it calls `_init_a` to get the +initial value of the field, which allows initializers returned from the +decorator to intercept and decorate it. It's important that the initial value +is used/defined _within_ the class body, because initializers can now refer to +private class fields, e.g. `a = this.#b` is a valid field initializer and would +become `a = _init_a(this, this.#b)`, which would also be valid. We cannot +extract initializer code, or any other code, from the class body because of +this. + +Overall, this decoration is pretty straightforward other than the fact that we +have to reference `_init_a` externally. + +```js + static #b(...args) { + _call_b(this, args); + } +``` + +Next up, we have a private static class method `#b`. This one is a bit more +complex, as our definition has been broken out into 2 parts: + +1. `static #b`: This is the method itself, which being a private method we + cannot overwrite with `defineProperty`. We also can't convert it into a + private field because that would change its semantics (would make it + writable). So, we instead have it proxy to the locally scoped `_call_b` + variable, which will be populated with the fully decorated method. +2. The definition of the method, kept in `_call_b`. As we mentioned above, the + original method's code is moved during the decoration process, and the wrapped + version is populated in `_call_b` and called whenever the private method is + called. + +```js + [_computedKey]() {} +``` + +Next is the undecorated method with a computed key. This uses the previously +calculated and stored computed key. + +```js + #a = _init_c(this, 123); + get #c() { + return _get_c(this); + } + set #c(v) { + _set_c(this, v); + } +``` + +Next up, we have the output for `accessor #c`. This is the most complicated +case, since we have to transpile the decorators, the `accessor` keyword, and +target a private field. Breaking it down piece by piece: + +```js + #a = _init_c(this, 123); +``` + +`accessor #c` desugars to a getter and setter which are backed by a new private +field, `#a`. Like before, the name of this field doesn't really matter, we'll +just generate a short, unique name. We call the decorated initializer for `#c` +and return that value to assign to the field. + +```js + get #c() { + return _get_c(this); + } + set #c(v) { + _set_c(this, v); + } +``` + +Next, we have the getter and setter for `#c` itself. These methods defer to +the `_get_c` and `_set_c` local variables, which will be the decorated versions +of the two getter functions that we passed for decoration in the static block +above. Those two functions are essentially just accessors for the private `#a` +field, but the decorator may add additional logic to them. + +```js + static { + _initClass(_Class); + } +``` + +Finally, we call `_initClass` in another static block, running any class and +static method initializers on the final class. This is done in a static block +for convenience with class expressions, but it could run immediately after the +class is defined. + +Ok, so now that we understand the general output, let's go back to `applyDecs`: + +```js +[ + _init_a, + _call_b, + _init_c, + _get_c, + _set_c, + _Class, + _initClass, + _initProto, + _initStatic, +] = _applyDecs(Class, + [ + [_dec, 0, "a"], + [ + _dec2, + 7, + "b", + function () { + console.log('foo'); + } + ], + [ + [_dec4, _dec5], + 1, + "c", + function () { + return this.#a; + }, + function (value) { + this.#a = value; + } + ] + ], + [dec] +); +``` + +`applyDecs` takes all of the decorators for the class and applies them. It +receives the following arguments: + +1. The class itself +2. Decorators to apply to class elements +3. Decorators to apply to the class itself + +The format of the data is designed to be as minimal as possible. Here's an +annotated version of the member descriptors: + +```js +[ + // List of decorators to apply to the field. Array if multiple decorators, + // otherwise just the single decorator itself. + dec, + + // The type of the decorator, represented as an enum. Static-ness is also + // encoded by adding 5 to the values + // 0 === FIELD + // 1 === ACCESSOR + // 2 === METHOD + // 3 === GETTER + // 4 === SETTER + // 5 === FIELD + STATIC + // 6 === ACCESSOR + STATIC + // 7 === METHOD + STATIC + // 8 === GETTER + STATIC + // 9 === SETTER + STATIC + 1, + + // The name of the member + 'y', + + // Optional fourth and fifth values, these are functions passed for private + // decorators + function() {} +], +``` + +Static and prototype decorators are all described like this. For class +decorators, it's just the list of decorators since no other context +is necessary. diff --git a/packages/babel-plugin-proposal-decorators/package.json b/packages/babel-plugin-proposal-decorators/package.json index 6e5f4128b1b4..565234dd6fa8 100644 --- a/packages/babel-plugin-proposal-decorators/package.json +++ b/packages/babel-plugin-proposal-decorators/package.json @@ -22,7 +22,9 @@ "dependencies": { "@babel/helper-create-class-features-plugin": "workspace:^", "@babel/helper-plugin-utils": "workspace:^", - "@babel/plugin-syntax-decorators": "workspace:^" + "@babel/helper-replace-supers": "workspace:^", + "@babel/plugin-syntax-decorators": "workspace:^", + "charcodes": "^0.2.0" }, "peerDependencies": { "@babel/core": "^7.0.0-0" diff --git a/packages/babel-plugin-proposal-decorators/src/index.ts b/packages/babel-plugin-proposal-decorators/src/index.ts index a7e142769038..25acc312ae0c 100644 --- a/packages/babel-plugin-proposal-decorators/src/index.ts +++ b/packages/babel-plugin-proposal-decorators/src/index.ts @@ -7,36 +7,14 @@ import { FEATURES, } from "@babel/helper-create-class-features-plugin"; import legacyVisitor from "./transformer-legacy"; +import transformer2021_12 from "./transformer-2021-12"; export default declare((api, options) => { api.assertVersion(7); - const { legacy = false } = options; - if (typeof legacy !== "boolean") { - throw new Error("'legacy' must be a boolean."); - } - - const { decoratorsBeforeExport } = options; - if (decoratorsBeforeExport === undefined) { - if (!legacy) { - throw new Error( - "The decorators plugin requires a 'decoratorsBeforeExport' option," + - " whose value must be a boolean. If you want to use the legacy" + - " decorators semantics, you can set the 'legacy: true' option.", - ); - } - } else { - if (legacy) { - throw new Error( - "'decoratorsBeforeExport' can't be used with legacy decorators.", - ); - } - if (typeof decoratorsBeforeExport !== "boolean") { - throw new Error("'decoratorsBeforeExport' must be a boolean."); - } - } + const { legacy, decoratorsBeforeExport, version } = options; - if (legacy) { + if (legacy || version === "legacy") { return { name: "proposal-decorators", inherits: syntaxDecorators, @@ -45,18 +23,20 @@ export default declare((api, options) => { }, visitor: legacyVisitor, }; - } - - return createClassFeaturePlugin({ - name: "proposal-decorators", + } else if (version === "2021-12") { + return transformer2021_12(api, options); + } else { + return createClassFeaturePlugin({ + name: "proposal-decorators", - api, - feature: FEATURES.decorators, - // loose: options.loose, Not supported + api, + feature: FEATURES.decorators, + inherits: syntaxDecorators, + // loose: options.loose, Not supported - manipulateOptions({ generatorOpts, parserOpts }) { - parserOpts.plugins.push(["decorators", { decoratorsBeforeExport }]); - generatorOpts.decoratorsBeforeExport = decoratorsBeforeExport; - }, - }); + manipulateOptions({ generatorOpts }) { + generatorOpts.decoratorsBeforeExport = decoratorsBeforeExport; + }, + }); + } }); diff --git a/packages/babel-plugin-proposal-decorators/src/transformer-2021-12.ts b/packages/babel-plugin-proposal-decorators/src/transformer-2021-12.ts new file mode 100644 index 000000000000..054d1e253b2b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/src/transformer-2021-12.ts @@ -0,0 +1,1029 @@ +import type { NodePath } from "@babel/traverse"; +import { types as t, template } from "@babel/core"; +import syntaxDecorators from "@babel/plugin-syntax-decorators"; +import ReplaceSupers from "@babel/helper-replace-supers"; +import * as charCodes from "charcodes"; + +type ClassDecoratableElement = + | t.ClassMethod + | t.ClassPrivateMethod + | t.ClassProperty + | t.ClassPrivateProperty + | t.ClassAccessorProperty; + +type ClassElement = + | ClassDecoratableElement + | t.TSDeclareMethod + | t.TSIndexSignature + | t.StaticBlock; + +function incrementId(id: number[], idx = id.length - 1): void { + // If index is -1, id needs an additional character, unshift A + if (idx === -1) { + id.unshift(charCodes.uppercaseA); + return; + } + + const current = id[idx]; + + if (current === charCodes.uppercaseZ) { + // if current is Z, skip to a + id[idx] = charCodes.lowercaseA; + } else if (current === charCodes.lowercaseZ) { + // if current is z, reset to A and carry the 1 + id[idx] = charCodes.uppercaseA; + incrementId(id, idx - 1); + } else { + // else, increment by one + id[idx] = current + 1; + } +} + +/** + * Generates a new private name that is unique to the given class. This can be + * used to create extra class fields and methods for the implementation, while + * keeping the length of those names as small as possible. This is important for + * minification purposes (though private names can generally be minified, + * transpilations and polyfills cannot yet). + */ +function createPrivateUidGeneratorForClass( + classPath: NodePath, +): () => t.PrivateName { + const currentPrivateId = []; + const privateNames = new Set(); + + classPath.traverse({ + PrivateName(path) { + privateNames.add(path.node.id.name); + }, + }); + + return (): t.PrivateName => { + let reifiedId; + do { + incrementId(currentPrivateId); + reifiedId = String.fromCharCode(...currentPrivateId); + } while (privateNames.has(reifiedId)); + + return t.privateName(t.identifier(reifiedId)); + }; +} + +/** + * Wraps the above generator function so that it's run lazily the first time + * it's actually required. Several types of decoration do not require this, so it + * saves iterating the class elements an additional time and allocating the space + * for the Sets of element names. + */ +function createLazyPrivateUidGeneratorForClass( + classPath: NodePath, +): () => t.PrivateName { + let generator: () => t.PrivateName; + + return (): t.PrivateName => { + if (!generator) { + generator = createPrivateUidGeneratorForClass(classPath); + } + + return generator(); + }; +} + +/** + * Takes a class definition and replaces it with an equivalent class declaration + * which is then assigned to a local variable. This allows us to reassign the + * local variable with the decorated version of the class. The class definition + * retains its original name so that `toString` is not affected, other + * references to the class are renamed instead. + */ +function replaceClassWithVar( + path: NodePath, +): [t.Identifier, NodePath] { + if (path.type === "ClassDeclaration") { + const varId = path.scope.generateUidIdentifierBasedOnNode(path.node.id); + const classId = t.identifier(path.node.id.name); + + path.scope.rename(classId.name, varId.name); + + path.insertBefore( + t.variableDeclaration("let", [t.variableDeclarator(varId)]), + ); + path.get("id").replaceWith(classId); + + return [t.cloneNode(varId), path]; + } else { + let className: string; + let varId: t.Identifier; + + if (path.node.id) { + className = path.node.id.name; + varId = path.scope.parent.generateDeclaredUidIdentifier(className); + path.scope.rename(className, varId.name); + } else if ( + path.parentPath.node.type === "VariableDeclarator" && + path.parentPath.node.id.type === "Identifier" + ) { + className = path.parentPath.node.id.name; + varId = path.scope.parent.generateDeclaredUidIdentifier(className); + } else { + varId = + path.scope.parent.generateDeclaredUidIdentifier("decorated_class"); + } + + const newClassExpr = t.classExpression( + className && t.identifier(className), + path.node.superClass, + path.node.body, + ); + + const [newPath] = path.replaceWith( + t.sequenceExpression([newClassExpr, varId]), + ); + + return [t.cloneNode(varId), newPath.get("expressions.0")]; + } +} + +function generateClassProperty( + key: t.PrivateName | t.Identifier, + value: t.Expression | undefined, + isStatic: boolean, +): t.ClassPrivateProperty | t.ClassProperty { + if (key.type === "PrivateName") { + return t.classPrivateProperty(key, value, undefined, isStatic); + } else { + return t.classProperty(key, value, undefined, undefined, isStatic); + } +} + +function addProxyAccessorsFor( + element: NodePath, + originalKey: t.PrivateName | t.Expression, + targetKey: t.PrivateName, + isComputed = false, +): void { + const { static: isStatic } = element.node; + + const getterBody = t.blockStatement([ + t.returnStatement( + t.memberExpression(t.thisExpression(), t.cloneNode(targetKey)), + ), + ]); + + const setterBody = t.blockStatement([ + t.expressionStatement( + t.assignmentExpression( + "=", + t.memberExpression(t.thisExpression(), t.cloneNode(targetKey)), + t.identifier("v"), + ), + ), + ]); + + let getter: t.ClassMethod | t.ClassPrivateMethod, + setter: t.ClassMethod | t.ClassPrivateMethod; + + if (originalKey.type === "PrivateName") { + getter = t.classPrivateMethod( + "get", + t.cloneNode(originalKey), + [], + getterBody, + isStatic, + ); + setter = t.classPrivateMethod( + "set", + t.cloneNode(originalKey), + [t.identifier("v")], + setterBody, + isStatic, + ); + } else { + getter = t.classMethod( + "get", + t.cloneNode(originalKey), + [], + getterBody, + isComputed, + isStatic, + ); + setter = t.classMethod( + "set", + t.cloneNode(originalKey), + [t.identifier("v")], + setterBody, + isComputed, + isStatic, + ); + } + + element.insertAfter(setter); + element.insertAfter(getter); +} + +function extractProxyAccessorsFor( + targetKey: t.PrivateName, +): t.FunctionExpression[] { + return [ + t.functionExpression( + undefined, + [], + t.blockStatement([ + t.returnStatement( + t.memberExpression(t.thisExpression(), t.cloneNode(targetKey)), + ), + ]), + ), + t.functionExpression( + undefined, + [t.identifier("value")], + t.blockStatement([ + t.expressionStatement( + t.assignmentExpression( + "=", + t.memberExpression(t.thisExpression(), t.cloneNode(targetKey)), + t.identifier("value"), + ), + ), + ]), + ), + ]; +} + +const FIELD = 0; +const ACCESSOR = 1; +const METHOD = 2; +const GETTER = 3; +const SETTER = 4; + +const STATIC = 5; + +function getElementKind(element: NodePath): number { + switch (element.node.type) { + case "ClassProperty": + case "ClassPrivateProperty": + return FIELD; + case "ClassAccessorProperty": + return ACCESSOR; + case "ClassMethod": + case "ClassPrivateMethod": + if (element.node.kind === "get") { + return GETTER; + } else if (element.node.kind === "set") { + return SETTER; + } else { + return METHOD; + } + } +} + +// Information about the decorators applied to an element +interface DecoratorInfo { + // The expressions of the decorators themselves + decorators: t.Expression[]; + + // The kind of the decorated value, matches the kind value passed to applyDecs + kind: number; + + // whether or not the field is static + isStatic: boolean; + + // The name of the decorator + name: t.StringLiteral | t.Expression; + + privateMethods: t.FunctionExpression | t.FunctionExpression[] | undefined; + + // The names of local variables that will be used/returned from the decoration + locals: t.Identifier | t.Identifier[] | undefined; +} + +// Information about a computed property key. These must be evaluated +// interspersed with decorator expressions, which is why they get added to the +// array of DecoratorInfos later on. +interface ComputedPropInfo { + localComputedNameId: t.Identifier; + keyNode: t.Expression; +} + +function isDecoratorInfo( + info: DecoratorInfo | ComputedPropInfo, +): info is DecoratorInfo { + return "decorators" in info; +} + +function generateDecorationExprs( + info: (DecoratorInfo | ComputedPropInfo)[], +): t.ArrayExpression { + return t.arrayExpression( + info.filter(isDecoratorInfo).map(el => { + const decs = + el.decorators.length > 1 + ? t.arrayExpression(el.decorators) + : el.decorators[0]; + + const kind = el.isStatic ? el.kind + STATIC : el.kind; + + const decInfo = [decs, t.numericLiteral(kind), el.name]; + + const { privateMethods } = el; + + if (Array.isArray(privateMethods)) { + decInfo.push(...privateMethods); + } else if (privateMethods) { + decInfo.push(privateMethods); + } + + return t.arrayExpression(decInfo); + }), + ); +} + +function extractElementLocalAssignments( + decorationInfo: (DecoratorInfo | ComputedPropInfo)[], +) { + const locals: t.Identifier[] = []; + + for (const el of decorationInfo) { + if ("locals" in el && el.locals) { + if (Array.isArray(el.locals)) { + locals.push(...el.locals); + } else { + locals.push(el.locals); + } + } + } + + return locals; +} + +function addCallAccessorsFor( + element: NodePath, + key: t.PrivateName, + getId: t.Identifier, + setId: t.Identifier, +) { + element.insertAfter( + t.classPrivateMethod( + "get", + t.cloneNode(key), + [], + t.blockStatement([ + t.expressionStatement( + t.callExpression(t.cloneNode(getId), [t.thisExpression()]), + ), + ]), + ), + ); + + element.insertAfter( + t.classPrivateMethod( + "set", + t.cloneNode(key), + [t.identifier("v")], + t.blockStatement([ + t.expressionStatement( + t.callExpression(t.cloneNode(setId), [ + t.thisExpression(), + t.identifier("v"), + ]), + ), + ]), + ), + ); +} + +function isNotTsParameter( + node: t.Identifier | t.Pattern | t.RestElement | t.TSParameterProperty, +): node is t.Identifier | t.Pattern | t.RestElement { + return node.type !== "TSParameterProperty"; +} + +function movePrivateAccessor( + element: NodePath, + key: t.PrivateName, + methodLocalVar: t.Identifier, + isStatic: boolean, +) { + let params: (t.Identifier | t.RestElement)[]; + let block: t.Statement[]; + + if (element.node.kind === "set") { + params = [t.identifier("v")]; + block = [ + t.expressionStatement( + t.callExpression(methodLocalVar, [ + t.thisExpression(), + t.identifier("v"), + ]), + ), + ]; + } else { + params = []; + block = [ + t.returnStatement(t.callExpression(methodLocalVar, [t.thisExpression()])), + ]; + } + + element.replaceWith( + t.classPrivateMethod( + element.node.kind, + t.cloneNode(key), + params, + t.blockStatement(block), + isStatic, + ), + ); +} + +function isClassDecoratableElementPath( + path: NodePath, +): path is NodePath { + const { type } = path; + + return ( + type !== "TSDeclareMethod" && + type !== "TSIndexSignature" && + type !== "StaticBlock" + ); +} + +function staticBlockToIIFE(block: t.StaticBlock) { + return t.callExpression( + t.arrowFunctionExpression([], t.blockStatement(block.body)), + [], + ); +} + +function maybeSequenceExpression(exprs: t.Expression[]) { + if (exprs.length === 0) return t.unaryExpression("void", t.numericLiteral(0)); + if (exprs.length === 1) return exprs[0]; + return t.sequenceExpression(exprs); +} + +function transformClass( + path: NodePath, + state: any, + constantSuper: boolean, +): NodePath { + const body = path.get("body.body"); + + const classDecorators = path.node.decorators; + let hasElementDecorators = false; + + const generateClassPrivateUid = createLazyPrivateUidGeneratorForClass(path); + + // Iterate over the class to see if we need to decorate it, and also to + // transform simple auto accessors which are not decorated + for (const element of body) { + if (!isClassDecoratableElementPath(element)) { + continue; + } + + if (element.node.decorators && element.node.decorators.length > 0) { + hasElementDecorators = true; + } else if (element.node.type === "ClassAccessorProperty") { + const { key, value, static: isStatic } = element.node; + + const newId = generateClassPrivateUid(); + + const valueNode = value ? t.cloneNode(value) : undefined; + + const newField = generateClassProperty(newId, valueNode, isStatic); + + const [newPath] = element.replaceWith(newField); + addProxyAccessorsFor(newPath, key, newId, element.node.computed); + } + } + + // If nothing is decorated, return + if (!classDecorators && !hasElementDecorators) return; + + const elementDecoratorInfo: (DecoratorInfo | ComputedPropInfo)[] = []; + + let firstFieldPath: + | NodePath + | undefined; + let constructorPath: NodePath | undefined; + let requiresProtoInit = false; + let requiresStaticInit = false; + let hasComputedProps = false; + const decoratedPrivateMethods = new Set(); + + let protoInitLocal: t.Identifier, + staticInitLocal: t.Identifier, + classInitLocal: t.Identifier, + classLocal: t.Identifier; + + if (classDecorators) { + classInitLocal = + path.scope.parent.generateDeclaredUidIdentifier("initClass"); + + const [localId, classPath] = replaceClassWithVar(path); + path = classPath; + classLocal = localId; + + path.node.decorators = null; + } else { + if (!path.node.id) { + path.node.id = path.scope.generateUidIdentifier("Class"); + } + classLocal = t.cloneNode(path.node.id); + } + + if (hasElementDecorators) { + for (const element of body) { + if (!isClassDecoratableElementPath(element)) { + continue; + } + + let { key } = element.node; + const kind = getElementKind(element); + const decorators = element.get("decorators"); + + const isPrivate = key.type === "PrivateName"; + const isComputed = + "computed" in element.node && element.node.computed === true; + const isStatic = !!element.node.static; + + let name = "computedKey"; + + if (isPrivate) { + name = (key as t.PrivateName).id.name; + } else if (key.type === "Identifier") { + name = key.name; + } + + if (element.isClassMethod({ kind: "constructor" })) { + constructorPath = element; + } + + if (isComputed) { + const keyPath = element.get("key"); + const localComputedNameId = + keyPath.scope.parent.generateDeclaredUidIdentifier(name); + keyPath.replaceWith(localComputedNameId); + + elementDecoratorInfo.push({ + localComputedNameId: t.cloneNode(localComputedNameId), + keyNode: t.cloneNode(key as t.Expression), + }); + + key = localComputedNameId; + hasComputedProps = true; + } + + if (Array.isArray(decorators) && decorators.length > 0) { + let locals: t.Identifier | t.Identifier[]; + let privateMethods: t.FunctionExpression | t.FunctionExpression[]; + + if (kind === ACCESSOR) { + const { value } = element.node as t.ClassAccessorProperty; + + const params: t.Expression[] = [t.thisExpression()]; + + if (value) { + params.push(t.cloneNode(value)); + } + + const newId = generateClassPrivateUid(); + const newFieldInitId = + element.scope.parent.generateDeclaredUidIdentifier(`init_${name}`); + const newValue = t.callExpression( + t.cloneNode(newFieldInitId), + params, + ); + + const newField = generateClassProperty(newId, newValue, isStatic); + const [newPath] = element.replaceWith(newField); + + if (isPrivate) { + privateMethods = extractProxyAccessorsFor(newId); + + const getId = newPath.scope.parent.generateDeclaredUidIdentifier( + `get_${name}`, + ); + const setId = newPath.scope.parent.generateDeclaredUidIdentifier( + `set_${name}`, + ); + + addCallAccessorsFor(newPath, key as t.PrivateName, getId, setId); + + locals = [newFieldInitId, getId, setId]; + } else { + addProxyAccessorsFor(newPath, key, newId, isComputed); + locals = newFieldInitId; + } + } else if (kind === FIELD) { + const initId = element.scope.parent.generateDeclaredUidIdentifier( + `init_${name}`, + ); + const valuePath = ( + element as NodePath + ).get("value"); + + valuePath.replaceWith( + t.callExpression( + t.cloneNode(initId), + [t.thisExpression(), valuePath.node].filter(v => v), + ), + ); + + locals = initId; + + if (isPrivate) { + privateMethods = extractProxyAccessorsFor(key as t.PrivateName); + } + } else if (isPrivate) { + locals = element.scope.parent.generateDeclaredUidIdentifier( + `call_${name}`, + ) as t.Identifier; + + const replaceSupers = new ReplaceSupers({ + constantSuper, + methodPath: element, + objectRef: classLocal, + superRef: path.node.superClass, + file: state, + refToPreserve: classLocal, + }); + + replaceSupers.replace(); + + const { + params, + body, + async: isAsync, + } = element.node as t.ClassPrivateMethod; + + privateMethods = t.functionExpression( + undefined, + params.filter(isNotTsParameter), + body, + isAsync, + ); + + if (kind === GETTER || kind === SETTER) { + movePrivateAccessor( + element as NodePath, + t.cloneNode(key as t.PrivateName), + t.cloneNode(locals), + isStatic, + ); + } else { + const node = element.node as t.ClassPrivateMethod; + + // Unshift + path.node.body.body.unshift( + t.classPrivateProperty( + key as t.PrivateName, + t.cloneNode(locals), + [], + node.static, + ), + ); + + decoratedPrivateMethods.add((key as t.PrivateName).id.name); + + element.remove(); + } + } + + let nameExpr: t.Expression; + + if (isComputed) { + nameExpr = t.cloneNode(key as t.Expression); + } else if (key.type === "PrivateName") { + nameExpr = t.stringLiteral(key.id.name); + } else if (key.type === "Identifier") { + nameExpr = t.stringLiteral(key.name); + } else { + nameExpr = t.cloneNode(key as t.Expression); + } + + elementDecoratorInfo.push({ + kind, + decorators: decorators.map(d => d.node.expression), + name: nameExpr, + isStatic, + privateMethods, + locals, + }); + + if (kind !== FIELD) { + if (isStatic) { + requiresStaticInit = true; + } else { + requiresProtoInit = true; + } + } + + if (element.node) { + element.node.decorators = null; + } + + if (!firstFieldPath && (kind === FIELD || kind === ACCESSOR)) { + firstFieldPath = element as NodePath< + t.ClassProperty | t.ClassPrivateProperty + >; + } + } + } + } + + if (hasComputedProps) { + const assignments: t.AssignmentExpression[] = []; + + for (const info of elementDecoratorInfo) { + if (isDecoratorInfo(info)) { + const { decorators } = info; + const newDecorators: t.Identifier[] = []; + + for (const decorator of decorators) { + const localComputedNameId = + path.scope.parent.generateDeclaredUidIdentifier("dec"); + assignments.push( + t.assignmentExpression("=", localComputedNameId, decorator), + ); + newDecorators.push(t.cloneNode(localComputedNameId)); + } + + info.decorators = newDecorators; + } else { + assignments.push( + t.assignmentExpression("=", info.localComputedNameId, info.keyNode), + ); + } + } + + path.insertBefore(assignments); + } + + const elementDecorations = generateDecorationExprs(elementDecoratorInfo); + const classDecorations = t.arrayExpression( + (classDecorators || []).map(d => d.expression), + ); + + const locals: t.Identifier[] = + extractElementLocalAssignments(elementDecoratorInfo); + + if (classDecorators) { + locals.push(classLocal, classInitLocal); + } + + if (requiresProtoInit) { + protoInitLocal = + path.scope.parent.generateDeclaredUidIdentifier("initProto"); + locals.push(protoInitLocal); + + const protoInitCall = t.callExpression(t.cloneNode(protoInitLocal), [ + t.thisExpression(), + ]); + + if (firstFieldPath) { + const value = firstFieldPath.get("value"); + const body: t.Expression[] = [protoInitCall]; + + if (value.node) { + body.push(value.node); + } + + value.replaceWith(t.sequenceExpression(body)); + } else if (constructorPath) { + if (path.node.superClass) { + path.traverse({ + CallExpression: { + exit(path) { + if (!path.get("callee").isSuper()) return; + + path.replaceWith( + t.callExpression(t.cloneNode(protoInitLocal), [path.node]), + ); + + path.skip(); + }, + }, + }); + } else { + constructorPath.node.body.body.unshift( + t.expressionStatement(protoInitCall), + ); + } + } else { + const body: t.Statement[] = [t.expressionStatement(protoInitCall)]; + + if (path.node.superClass) { + body.unshift( + t.expressionStatement( + t.callExpression(t.super(), [ + t.spreadElement(t.identifier("args")), + ]), + ), + ); + } + + path.node.body.body.unshift( + t.classMethod( + "constructor", + t.identifier("constructor"), + [t.restElement(t.identifier("args"))], + t.blockStatement(body), + ), + ); + } + } + + if (requiresStaticInit) { + staticInitLocal = + path.scope.parent.generateDeclaredUidIdentifier("initStatic"); + locals.push(staticInitLocal); + } + + if (decoratedPrivateMethods.size > 0) { + path.traverse({ + PrivateName(path) { + if (!decoratedPrivateMethods.has(path.node.id.name)) return; + + const parentPath = path.parentPath; + const parentParentPath = parentPath.parentPath; + + if ( + // this.bar().#x = 123; + (parentParentPath.node.type === "AssignmentExpression" && + parentParentPath.node.left === parentPath.node) || + // this.#x++; + parentParentPath.node.type === "UpdateExpression" || + // ([...this.#x] = foo); + parentParentPath.node.type === "RestElement" || + // ([this.#x] = foo); + parentParentPath.node.type === "ArrayPattern" || + // ({ a: this.#x } = bar); + (parentParentPath.node.type === "ObjectProperty" && + parentParentPath.node.value === parentPath.node && + parentParentPath.parentPath.type === "ObjectPattern") || + // for (this.#x of []); + (parentParentPath.node.type === "ForOfStatement" && + parentParentPath.node.left === parentPath.node) + ) { + throw path.buildCodeFrameError( + `Decorated private methods are not updatable, but "#${path.node.id.name}" is updated via this expression.`, + ); + } + }, + }); + } + + let classInitInjected = false; + const classInitCall = + classInitLocal && t.callExpression(t.cloneNode(classInitLocal), []); + + const originalClass = path.node; + + if (classDecorators) { + const statics = []; + let staticBlocks: t.StaticBlock[] = []; + path.get("body.body").forEach(element => { + // Static blocks cannot be compiled to "instance blocks", but we can inline + // them as IIFEs in the next property. + if (element.isStaticBlock()) { + staticBlocks.push(element.node); + element.remove(); + return; + } + + const isProperty = + element.isClassProperty() || element.isClassPrivateProperty(); + + if ( + (isProperty || element.isClassPrivateMethod()) && + element.node.static + ) { + if (isProperty && staticBlocks.length > 0) { + const allValues: t.Expression[] = staticBlocks.map(staticBlockToIIFE); + if (element.node.value) allValues.push(element.node.value); + element.node.value = maybeSequenceExpression(allValues); + staticBlocks = []; + } + + element.node.static = false; + statics.push(element.node); + element.remove(); + } + }); + + if (statics.length > 0 || staticBlocks.length > 0) { + const staticsClass = template.expression.ast` + class extends ${state.addHelper("identity")} {} + ` as t.ClassExpression; + staticsClass.body.body = [ + t.staticBlock([t.toStatement(path.node, false)]), + ...statics, + ]; + + const constructorBody: t.Expression[] = []; + + const newExpr = t.newExpression(staticsClass, []); + + if (staticBlocks.length > 0) { + constructorBody.push(...staticBlocks.map(staticBlockToIIFE)); + } + if (classInitCall) { + classInitInjected = true; + constructorBody.push(classInitCall); + } + if (constructorBody.length > 0) { + constructorBody.unshift( + t.callExpression(t.super(), [t.cloneNode(classLocal)]), + ); + + staticsClass.body.body.push( + t.classMethod( + "constructor", + t.identifier("constructor"), + [], + t.blockStatement([ + t.expressionStatement(t.sequenceExpression(constructorBody)), + ]), + ), + ); + } else { + newExpr.arguments.push(t.cloneNode(classLocal)); + } + + path.replaceWith(newExpr); + } + } + if (!classInitInjected && classInitCall) { + path.node.body.body.push( + t.staticBlock([t.expressionStatement(classInitCall)]), + ); + } + + originalClass.body.body.unshift( + t.staticBlock( + [ + t.expressionStatement( + t.assignmentExpression( + "=", + t.arrayPattern(locals), + t.callExpression(state.addHelper("applyDecs"), [ + t.thisExpression(), + elementDecorations, + classDecorations, + ]), + ), + ), + requiresStaticInit && + t.expressionStatement( + t.callExpression(t.cloneNode(staticInitLocal), [ + t.thisExpression(), + ]), + ), + ].filter(Boolean), + ), + ); + + // Recrawl the scope to make sure new identifiers are properly synced + path.scope.crawl(); + + return path; +} + +export default function ( + { assertVersion, assumption }, + { decoratorsBeforeExport, loose }, +) { + assertVersion("^7.16.0"); + + const VISITED = new WeakSet(); + const constantSuper = assumption("constantSuper") ?? loose; + + return { + name: "proposal-decorators", + inherits: syntaxDecorators, + manipulateOptions({ generatorOpts }) { + generatorOpts.decoratorsBeforeExport = decoratorsBeforeExport; + }, + + visitor: { + ClassDeclaration(path: NodePath, state: any) { + if (VISITED.has(path)) return; + + const newPath = transformClass(path, state, constantSuper); + + if (newPath) { + VISITED.add(newPath); + } + }, + + ClassExpression(path: NodePath, state: any) { + if (VISITED.has(path)) return; + + const newPath = transformClass(path, state, constantSuper); + + if (newPath) { + VISITED.add(newPath); + } + }, + }, + }; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/options.json new file mode 100644 index 000000000000..69f3e599cf2d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "proposal-decorators", + { "version": "2021-12", "decoratorsBeforeExport": false } + ], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/private/exec.js new file mode 100644 index 000000000000..d3161fa6ab3d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/private/exec.js @@ -0,0 +1,54 @@ +function dec({ get, set }, context) { + context.addInitializer((instance) => { + instance[context.name + 'Context'] = context; + }); + + return { + get() { + return get.call(this) + 1; + }, + + set(v) { + set.call(this, v + 1); + }, + + initializer(v) { + return v ? v : 1; + } + } +} + +class Foo { + @dec + accessor #a; + + @dec + accessor #b = 123; +} + +let foo = new Foo(); + +const aContext = foo['#aContext']; +const bContext = foo['#bContext']; + +expect(aContext.access.get.call(foo)).toBe(2); +aContext.access.set.call(foo, 123); +expect(aContext.access.get.call(foo)).toBe(125); +expect(aContext.name).toBe('#a'); +expect(aContext.kind).toBe('accessor'); +expect(aContext.isStatic).toBe(false); +expect(aContext.isPrivate).toBe(true); +expect(typeof aContext.addInitializer).toBe('function'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); + +expect(bContext.access.get.call(foo)).toBe(124); +bContext.access.set.call(foo, 123); +expect(bContext.access.get.call(foo)).toBe(125); +expect(bContext.name).toBe('#b'); +expect(bContext.kind).toBe('accessor'); +expect(bContext.isStatic).toBe(false); +expect(bContext.isPrivate).toBe(true); +expect(typeof bContext.addInitializer).toBe('function'); +expect(typeof bContext.setMetadata).toBe('function'); +expect(typeof bContext.getMetadata).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/private/input.js new file mode 100644 index 000000000000..be4f348923bc --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/private/input.js @@ -0,0 +1,7 @@ +class Foo { + @dec + accessor #a; + + @dec + accessor #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/private/output.js new file mode 100644 index 000000000000..69673b7af6d2 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/private/output.js @@ -0,0 +1,59 @@ +var _init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initProto; + +var _A = /*#__PURE__*/new WeakMap(); + +var _a = /*#__PURE__*/new WeakMap(); + +var _B = /*#__PURE__*/new WeakMap(); + +var _b = /*#__PURE__*/new WeakMap(); + +class Foo { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _b, { + get: _get_b2, + set: _set_b2 + }); + babelHelpers.classPrivateFieldInitSpec(this, _a, { + get: _get_a2, + set: _set_a2 + }); + babelHelpers.classPrivateFieldInitSpec(this, _A, { + writable: true, + value: (_initProto(this), _init_a(this)) + }); + babelHelpers.classPrivateFieldInitSpec(this, _B, { + writable: true, + value: _init_b(this, 123) + }); + } + +} + +function _set_a2(v) { + _set_a(this, v); +} + +function _get_a2() { + _get_a(this); +} + +function _set_b2(v) { + _set_b(this, v); +} + +function _get_b2() { + _get_b(this); +} + +(() => { + [_init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initProto] = babelHelpers.applyDecs(Foo, [[dec, 1, "a", function () { + return babelHelpers.classPrivateFieldGet(this, _A); + }, function (value) { + babelHelpers.classPrivateFieldSet(this, _A, value); + }], [dec, 1, "b", function () { + return babelHelpers.classPrivateFieldGet(this, _B); + }, function (value) { + babelHelpers.classPrivateFieldSet(this, _B, value); + }]], []); +})(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/public/exec.js new file mode 100644 index 000000000000..7951130866a2 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/public/exec.js @@ -0,0 +1,75 @@ +function dec({ get, set }, context) { + context.addInitializer((instance) => { + instance[context.name + 'Context'] = context; + }); + + return { + get() { + return get.call(this) + 1; + }, + + set(v) { + set.call(this, v + 1); + }, + + initializer(v) { + return v ? v : 1; + } + } +} + +class Foo { + @dec + accessor a; + + @dec + accessor b = 123; + + @dec + accessor ['c'] = 456; +} + +let foo = new Foo(); + +const aContext = foo['aContext']; +const bContext = foo['bContext']; +const cContext = foo['cContext']; + +expect(foo.a).toBe(2); +foo.a = 123; +expect(foo.a).toBe(125); +expect(aContext.name).toBe('a'); +expect(aContext.kind).toBe('accessor'); +expect(aContext.isStatic).toBe(false); +expect(aContext.isPrivate).toBe(false); +expect(typeof aContext.addInitializer).toBe('function'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); +expect(foo.hasOwnProperty('a')).toBe(false); +expect(Foo.prototype.hasOwnProperty('a')).toBe(true); + +expect(foo.b).toBe(124); +foo.b = 123; +expect(foo.b).toBe(125); +expect(bContext.name).toBe('b'); +expect(bContext.kind).toBe('accessor'); +expect(bContext.isStatic).toBe(false); +expect(bContext.isPrivate).toBe(false); +expect(typeof bContext.addInitializer).toBe('function'); +expect(typeof bContext.setMetadata).toBe('function'); +expect(typeof bContext.getMetadata).toBe('function'); +expect(foo.hasOwnProperty('b')).toBe(false); +expect(Foo.prototype.hasOwnProperty('b')).toBe(true); + +expect(foo.c).toBe(457); +foo.c = 456; +expect(foo.c).toBe(458); +expect(cContext.name).toBe('c'); +expect(cContext.kind).toBe('accessor'); +expect(cContext.isStatic).toBe(false); +expect(cContext.isPrivate).toBe(false); +expect(typeof cContext.addInitializer).toBe('function'); +expect(typeof cContext.setMetadata).toBe('function'); +expect(typeof cContext.getMetadata).toBe('function'); +expect(foo.hasOwnProperty('c')).toBe(false); +expect(Foo.prototype.hasOwnProperty('c')).toBe(true); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/public/input.js new file mode 100644 index 000000000000..5945d3196f1d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/public/input.js @@ -0,0 +1,10 @@ +class Foo { + @dec + accessor a; + + @dec + accessor b = 123; + + @dec + accessor ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/public/output.js new file mode 100644 index 000000000000..2a6057e84caa --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/public/output.js @@ -0,0 +1,58 @@ +var _init_a, _init_b, _computedKey, _init_computedKey, _dec, _dec2, _dec3, _initProto; + +_dec = dec +_dec2 = dec +_computedKey = 'c' +_dec3 = dec + +var _A = /*#__PURE__*/new WeakMap(); + +var _B = /*#__PURE__*/new WeakMap(); + +var _C = /*#__PURE__*/new WeakMap(); + +class Foo { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _A, { + writable: true, + value: (_initProto(this), _init_a(this)) + }); + babelHelpers.classPrivateFieldInitSpec(this, _B, { + writable: true, + value: _init_b(this, 123) + }); + babelHelpers.classPrivateFieldInitSpec(this, _C, { + writable: true, + value: _init_computedKey(this, 456) + }); + } + + get a() { + return babelHelpers.classPrivateFieldGet(this, _A); + } + + set a(v) { + babelHelpers.classPrivateFieldSet(this, _A, v); + } + + get b() { + return babelHelpers.classPrivateFieldGet(this, _B); + } + + set b(v) { + babelHelpers.classPrivateFieldSet(this, _B, v); + } + + get [_computedKey]() { + return babelHelpers.classPrivateFieldGet(this, _C); + } + + set [_computedKey](v) { + babelHelpers.classPrivateFieldSet(this, _C, v); + } + +} + +(() => { + [_init_a, _init_b, _init_computedKey, _initProto] = babelHelpers.applyDecs(Foo, [[_dec, 1, "a"], [_dec2, 1, "b"], [_dec3, 1, _computedKey]], []); +})(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-private/exec.js new file mode 100644 index 000000000000..06946cc2f26d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-private/exec.js @@ -0,0 +1,52 @@ +function dec({ get, set }, context) { + context.addInitializer((instance) => { + instance[context.name + 'Context'] = context; + }); + + return { + get() { + return get.call(this) + 1; + }, + + set(v) { + set.call(this, v + 1); + }, + + initializer(v) { + return v ? v : 1; + } + } +} + +class Foo { + @dec + static accessor #a; + + @dec + static accessor #b = 123; +} + +const aContext = Foo['#aContext']; +const bContext = Foo['#bContext']; + +expect(aContext.access.get.call(Foo)).toBe(2); +aContext.access.set.call(Foo, 123); +expect(aContext.access.get.call(Foo)).toBe(125); +expect(aContext.name).toBe('#a'); +expect(aContext.kind).toBe('accessor'); +expect(aContext.isStatic).toBe(true); +expect(aContext.isPrivate).toBe(true); +expect(typeof aContext.addInitializer).toBe('function'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); + +expect(bContext.access.get.call(Foo)).toBe(124); +bContext.access.set.call(Foo, 123); +expect(bContext.access.get.call(Foo)).toBe(125); +expect(bContext.name).toBe('#b'); +expect(bContext.kind).toBe('accessor'); +expect(bContext.isStatic).toBe(true); +expect(bContext.isPrivate).toBe(true); +expect(typeof bContext.addInitializer).toBe('function'); +expect(typeof bContext.setMetadata).toBe('function'); +expect(typeof bContext.getMetadata).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-private/input.js new file mode 100644 index 000000000000..ebb88a741572 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-private/input.js @@ -0,0 +1,7 @@ +class Foo { + @dec + static accessor #a; + + @dec + static accessor #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-private/output.js new file mode 100644 index 000000000000..b0ec49e5726f --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-private/output.js @@ -0,0 +1,58 @@ +var _init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initStatic; + +var _a = /*#__PURE__*/new WeakMap(); + +var _b = /*#__PURE__*/new WeakMap(); + +class Foo { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _b, { + get: _get_b2, + set: _set_b2 + }); + babelHelpers.classPrivateFieldInitSpec(this, _a, { + get: _get_a2, + set: _set_a2 + }); + } + +} + +function _set_a2(v) { + _set_a(this, v); +} + +function _get_a2() { + _get_a(this); +} + +function _set_b2(v) { + _set_b(this, v); +} + +function _get_b2() { + _get_b(this); +} + +(() => { + [_init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initStatic] = babelHelpers.applyDecs(Foo, [[dec, 6, "a", function () { + return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _A); + }, function (value) { + babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _A, value); + }], [dec, 6, "b", function () { + return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _B); + }, function (value) { + babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _B, value); + }]], []); + + _initStatic(Foo); +})(); + +var _A = { + writable: true, + value: _init_a(Foo) +}; +var _B = { + writable: true, + value: _init_b(Foo, 123) +}; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-public/exec.js new file mode 100644 index 000000000000..21973ce25629 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-public/exec.js @@ -0,0 +1,70 @@ +function dec({ get, set }, context) { + context.addInitializer((instance) => { + instance[context.name + 'Context'] = context; + }); + + return { + get() { + return get.call(this) + 1; + }, + + set(v) { + set.call(this, v + 1); + }, + + initializer(v) { + return v ? v : 1; + } + } +} + +class Foo { + @dec + static accessor a; + + @dec + static accessor b = 123; + + @dec + static accessor ['c'] = 456; +} + +const aContext = Foo['aContext']; +const bContext = Foo['bContext']; +const cContext = Foo['cContext']; + +expect(Foo.a).toBe(2); +Foo.a = 123; +expect(Foo.a).toBe(125); +expect(aContext.name).toBe('a'); +expect(aContext.kind).toBe('accessor'); +expect(aContext.isStatic).toBe(true); +expect(aContext.isPrivate).toBe(false); +expect(typeof aContext.addInitializer).toBe('function'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); +expect(Foo.hasOwnProperty('a')).toBe(true); + +expect(Foo.b).toBe(124); +Foo.b = 123; +expect(Foo.b).toBe(125); +expect(bContext.name).toBe('b'); +expect(bContext.kind).toBe('accessor'); +expect(bContext.isStatic).toBe(true); +expect(bContext.isPrivate).toBe(false); +expect(typeof bContext.addInitializer).toBe('function'); +expect(typeof bContext.setMetadata).toBe('function'); +expect(typeof bContext.getMetadata).toBe('function'); +expect(Foo.hasOwnProperty('b')).toBe(true); + +expect(Foo.c).toBe(457); +Foo.c = 456; +expect(Foo.c).toBe(458); +expect(cContext.name).toBe('c'); +expect(cContext.kind).toBe('accessor'); +expect(cContext.isStatic).toBe(true); +expect(cContext.isPrivate).toBe(false); +expect(typeof cContext.addInitializer).toBe('function'); +expect(typeof cContext.setMetadata).toBe('function'); +expect(typeof cContext.getMetadata).toBe('function'); +expect(Foo.hasOwnProperty('c')).toBe(true); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-public/input.js new file mode 100644 index 000000000000..edf06478601f --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-public/input.js @@ -0,0 +1,10 @@ +class Foo { + @dec + static accessor a; + + @dec + static accessor b = 123; + + @dec + static accessor ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-public/output.js new file mode 100644 index 000000000000..c5a6ac47eba2 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-public/output.js @@ -0,0 +1,52 @@ +var _init_a, _init_b, _computedKey, _init_computedKey, _dec, _dec2, _dec3, _initStatic; + +_dec = dec +_dec2 = dec +_computedKey = 'c' +_dec3 = dec + +class Foo { + static get a() { + return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _A); + } + + static set a(v) { + babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _A, v); + } + + static get b() { + return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _B); + } + + static set b(v) { + babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _B, v); + } + + static get [_computedKey]() { + return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _C); + } + + static set [_computedKey](v) { + babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _C, v); + } + +} + +(() => { + [_init_a, _init_b, _init_computedKey, _initStatic] = babelHelpers.applyDecs(Foo, [[_dec, 6, "a"], [_dec2, 6, "b"], [_dec3, 6, _computedKey]], []); + + _initStatic(Foo); +})(); + +var _A = { + writable: true, + value: _init_a(Foo) +}; +var _B = { + writable: true, + value: _init_b(Foo, 123) +}; +var _C = { + writable: true, + value: _init_computedKey(Foo, 456) +}; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-private/exec.js new file mode 100644 index 000000000000..51715be9f173 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-private/exec.js @@ -0,0 +1,31 @@ +class Foo { + accessor #a; + + accessor #b = 123; + + getA() { + return this.#a; + } + + setA(v) { + this.#a = v; + } + + getB() { + return this.#b; + } + + setB(v) { + this.#b = v; + } +} + +let foo = new Foo(); + +expect(foo.getA()).toBe(undefined); +foo.setA(123) +expect(foo.getA()).toBe(123); + +expect(foo.getB()).toBe(123); +foo.setB(456) +expect(foo.getB()).toBe(456); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-private/input.js new file mode 100644 index 000000000000..4afc45261337 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-private/input.js @@ -0,0 +1,5 @@ +class Foo { + accessor #a; + + accessor #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-private/output.js new file mode 100644 index 000000000000..29d3fbd66965 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-private/output.js @@ -0,0 +1,45 @@ +var _A = /*#__PURE__*/new WeakMap(); + +var _a = /*#__PURE__*/new WeakMap(); + +var _B = /*#__PURE__*/new WeakMap(); + +var _b = /*#__PURE__*/new WeakMap(); + +class Foo { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _b, { + get: _get_b, + set: _set_b + }); + babelHelpers.classPrivateFieldInitSpec(this, _a, { + get: _get_a, + set: _set_a + }); + babelHelpers.classPrivateFieldInitSpec(this, _A, { + writable: true, + value: void 0 + }); + babelHelpers.classPrivateFieldInitSpec(this, _B, { + writable: true, + value: 123 + }); + } + +} + +function _get_a() { + return babelHelpers.classPrivateFieldGet(this, _A); +} + +function _set_a(v) { + babelHelpers.classPrivateFieldSet(this, _A, v); +} + +function _get_b() { + return babelHelpers.classPrivateFieldGet(this, _B); +} + +function _set_b(v) { + babelHelpers.classPrivateFieldSet(this, _B, v); +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-public/exec.js new file mode 100644 index 000000000000..5ccd5dcce44d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-public/exec.js @@ -0,0 +1,27 @@ +class Foo { + accessor a; + + accessor b = 123; + + accessor ['c'] = 456; +} + +let foo = new Foo(); + +expect(foo.a).toBe(undefined); +foo.a = 123; +expect(foo.a).toBe(123); +expect(foo.hasOwnProperty('a')).toBe(false); +expect(Foo.prototype.hasOwnProperty('a')).toBe(true); + +expect(foo.b).toBe(123); +foo.b = 456 +expect(foo.b).toBe(456); +expect(foo.hasOwnProperty('b')).toBe(false); +expect(Foo.prototype.hasOwnProperty('b')).toBe(true); + +expect(foo.c).toBe(456); +foo.c = 789 +expect(foo.c).toBe(789); +expect(foo.hasOwnProperty('c')).toBe(false); +expect(Foo.prototype.hasOwnProperty('c')).toBe(true); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-public/input.js new file mode 100644 index 000000000000..ad27f18d5ed3 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-public/input.js @@ -0,0 +1,7 @@ +class Foo { + accessor a; + + accessor b = 123; + + accessor ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-public/output.js new file mode 100644 index 000000000000..6ec6f9255a06 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-public/output.js @@ -0,0 +1,47 @@ +var _A = /*#__PURE__*/new WeakMap(); + +var _B = /*#__PURE__*/new WeakMap(); + +var _C = /*#__PURE__*/new WeakMap(); + +class Foo { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _A, { + writable: true, + value: void 0 + }); + babelHelpers.classPrivateFieldInitSpec(this, _B, { + writable: true, + value: 123 + }); + babelHelpers.classPrivateFieldInitSpec(this, _C, { + writable: true, + value: 456 + }); + } + + get a() { + return babelHelpers.classPrivateFieldGet(this, _A); + } + + set a(v) { + babelHelpers.classPrivateFieldSet(this, _A, v); + } + + get b() { + return babelHelpers.classPrivateFieldGet(this, _B); + } + + set b(v) { + babelHelpers.classPrivateFieldSet(this, _B, v); + } + + get 'c'() { + return babelHelpers.classPrivateFieldGet(this, _C); + } + + set 'c'(v) { + babelHelpers.classPrivateFieldSet(this, _C, v); + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-private/exec.js new file mode 100644 index 000000000000..21defb3f24d8 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-private/exec.js @@ -0,0 +1,29 @@ +class Foo { + static accessor #a; + + static accessor #b = 123; + + static getA() { + return this.#a; + } + + static setA(v) { + this.#a = v; + } + + static getB() { + return this.#b; + } + + static setB(v) { + this.#b = v; + } +} + +expect(Foo.getA()).toBe(undefined); +Foo.setA(123) +expect(Foo.getA()).toBe(123); + +expect(Foo.getB()).toBe(123); +Foo.setB(456) +expect(Foo.getB()).toBe(456); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-private/input.js new file mode 100644 index 000000000000..2e516a961de9 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-private/input.js @@ -0,0 +1,5 @@ +class Foo { + static accessor #a; + + static accessor #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-private/output.js new file mode 100644 index 000000000000..51de21b149b2 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-private/output.js @@ -0,0 +1,34 @@ +class Foo {} + +function _get_a() { + return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _A); +} + +function _set_a(v) { + babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _A, v); +} + +function _get_b() { + return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _B); +} + +function _set_b(v) { + babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _B, v); +} + +var _b = { + get: _get_b, + set: _set_b +}; +var _a = { + get: _get_a, + set: _set_a +}; +var _A = { + writable: true, + value: void 0 +}; +var _B = { + writable: true, + value: 123 +}; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-public/exec.js new file mode 100644 index 000000000000..d22a4e710dd8 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-public/exec.js @@ -0,0 +1,22 @@ +class Foo { + static accessor a; + + static accessor b = 123; + + static accessor ['c'] = 456; +} + +expect(Foo.a).toBe(undefined); +Foo.a = 123; +expect(Foo.a).toBe(123); +expect(Foo.hasOwnProperty('a')).toBe(true); + +expect(Foo.b).toBe(123); +Foo.b = 456 +expect(Foo.b).toBe(456); +expect(Foo.hasOwnProperty('b')).toBe(true); + +expect(Foo.c).toBe(456); +Foo.c = 789 +expect(Foo.c).toBe(789); +expect(Foo.hasOwnProperty('c')).toBe(true); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-public/input.js new file mode 100644 index 000000000000..160e72cd531c --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-public/input.js @@ -0,0 +1,7 @@ +class Foo { + static accessor a; + + static accessor b = 123; + + static accessor ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-public/output.js new file mode 100644 index 000000000000..4fe37de40ebf --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/undecorated-static-public/output.js @@ -0,0 +1,39 @@ +class Foo { + static get a() { + return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _A); + } + + static set a(v) { + babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _A, v); + } + + static get b() { + return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _B); + } + + static set b(v) { + babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _B, v); + } + + static get 'c'() { + return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _C); + } + + static set 'c'(v) { + babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _C, v); + } + +} + +var _A = { + writable: true, + value: void 0 +}; +var _B = { + writable: true, + value: 123 +}; +var _C = { + writable: true, + value: 456 +}; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/options.json new file mode 100644 index 000000000000..18aa205580db --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + [ + "proposal-decorators", + { "version": "2021-12", "decoratorsBeforeExport": false } + ] + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/private/input.js new file mode 100644 index 000000000000..be4f348923bc --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/private/input.js @@ -0,0 +1,7 @@ +class Foo { + @dec + accessor #a; + + @dec + accessor #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/private/output.js new file mode 100644 index 000000000000..c054a288032a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/private/output.js @@ -0,0 +1,35 @@ +var _init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initProto; + +class Foo { + static { + [_init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initProto] = babelHelpers.applyDecs(this, [[dec, 1, "a", function () { + return this.#A; + }, function (value) { + this.#A = value; + }], [dec, 1, "b", function () { + return this.#B; + }, function (value) { + this.#B = value; + }]], []); + } + #A = (_initProto(this), _init_a(this)); + + set #a(v) { + _set_a(this, v); + } + + get #a() { + _get_a(this); + } + + #B = _init_b(this, 123); + + set #b(v) { + _set_b(this, v); + } + + get #b() { + _get_b(this); + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/public/input.js new file mode 100644 index 000000000000..5945d3196f1d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/public/input.js @@ -0,0 +1,10 @@ +class Foo { + @dec + accessor a; + + @dec + accessor b = 123; + + @dec + accessor ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/public/output.js new file mode 100644 index 000000000000..0fb94194b1db --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/public/output.js @@ -0,0 +1,42 @@ +var _init_a, _init_b, _computedKey, _init_computedKey, _dec, _dec2, _dec3, _initProto; + +_dec = dec +_dec2 = dec +_computedKey = 'c' +_dec3 = dec + +class Foo { + static { + [_init_a, _init_b, _init_computedKey, _initProto] = babelHelpers.applyDecs(this, [[_dec, 1, "a"], [_dec2, 1, "b"], [_dec3, 1, _computedKey]], []); + } + #A = (_initProto(this), _init_a(this)); + + get a() { + return this.#A; + } + + set a(v) { + this.#A = v; + } + + #B = _init_b(this, 123); + + get b() { + return this.#B; + } + + set b(v) { + this.#B = v; + } + + #C = _init_computedKey(this, 456); + + get [_computedKey]() { + return this.#C; + } + + set [_computedKey](v) { + this.#C = v; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/static-private/input.js new file mode 100644 index 000000000000..ebb88a741572 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/static-private/input.js @@ -0,0 +1,7 @@ +class Foo { + @dec + static accessor #a; + + @dec + static accessor #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/static-private/output.js new file mode 100644 index 000000000000..ad30531a425e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/static-private/output.js @@ -0,0 +1,38 @@ +var _init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initStatic; + +class Foo { + static { + [_init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initStatic] = babelHelpers.applyDecs(this, [[dec, 6, "a", function () { + return this.#A; + }, function (value) { + this.#A = value; + }], [dec, 6, "b", function () { + return this.#B; + }, function (value) { + this.#B = value; + }]], []); + + _initStatic(this); + + } + static #A = _init_a(this); + + set #a(v) { + _set_a(this, v); + } + + get #a() { + _get_a(this); + } + + static #B = _init_b(this, 123); + + set #b(v) { + _set_b(this, v); + } + + get #b() { + _get_b(this); + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/static-public/input.js new file mode 100644 index 000000000000..edf06478601f --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/static-public/input.js @@ -0,0 +1,10 @@ +class Foo { + @dec + static accessor a; + + @dec + static accessor b = 123; + + @dec + static accessor ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/static-public/output.js new file mode 100644 index 000000000000..471adf36f9c9 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/static-public/output.js @@ -0,0 +1,45 @@ +var _init_a, _init_b, _computedKey, _init_computedKey, _dec, _dec2, _dec3, _initStatic; + +_dec = dec +_dec2 = dec +_computedKey = 'c' +_dec3 = dec + +class Foo { + static { + [_init_a, _init_b, _init_computedKey, _initStatic] = babelHelpers.applyDecs(this, [[_dec, 6, "a"], [_dec2, 6, "b"], [_dec3, 6, _computedKey]], []); + + _initStatic(this); + + } + static #A = _init_a(this); + + static get a() { + return this.#A; + } + + static set a(v) { + this.#A = v; + } + + static #B = _init_b(this, 123); + + static get b() { + return this.#B; + } + + static set b(v) { + this.#B = v; + } + + static #C = _init_computedKey(this, 456); + + static get [_computedKey]() { + return this.#C; + } + + static set [_computedKey](v) { + this.#C = v; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-private/input.js new file mode 100644 index 000000000000..4afc45261337 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-private/input.js @@ -0,0 +1,5 @@ +class Foo { + accessor #a; + + accessor #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-private/output.js new file mode 100644 index 000000000000..9eb7268e4131 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-private/output.js @@ -0,0 +1,22 @@ +class Foo { + #A; + + get #a() { + return this.#A; + } + + set #a(v) { + this.#A = v; + } + + #B = 123; + + get #b() { + return this.#B; + } + + set #b(v) { + this.#B = v; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-public/input.js new file mode 100644 index 000000000000..ad27f18d5ed3 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-public/input.js @@ -0,0 +1,7 @@ +class Foo { + accessor a; + + accessor b = 123; + + accessor ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-public/output.js new file mode 100644 index 000000000000..f6269be736f1 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-public/output.js @@ -0,0 +1,32 @@ +class Foo { + #A; + + get a() { + return this.#A; + } + + set a(v) { + this.#A = v; + } + + #B = 123; + + get b() { + return this.#B; + } + + set b(v) { + this.#B = v; + } + + #C = 456; + + get 'c'() { + return this.#C; + } + + set 'c'(v) { + this.#C = v; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-static-private/input.js new file mode 100644 index 000000000000..2e516a961de9 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-static-private/input.js @@ -0,0 +1,5 @@ +class Foo { + static accessor #a; + + static accessor #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-static-private/output.js new file mode 100644 index 000000000000..04cc0ef030d0 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-static-private/output.js @@ -0,0 +1,22 @@ +class Foo { + static #A; + + static get #a() { + return this.#A; + } + + static set #a(v) { + this.#A = v; + } + + static #B = 123; + + static get #b() { + return this.#B; + } + + static set #b(v) { + this.#B = v; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-static-public/input.js new file mode 100644 index 000000000000..160e72cd531c --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-static-public/input.js @@ -0,0 +1,7 @@ +class Foo { + static accessor a; + + static accessor b = 123; + + static accessor ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-static-public/output.js new file mode 100644 index 000000000000..187ad5214a67 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors/undecorated-static-public/output.js @@ -0,0 +1,32 @@ +class Foo { + static #A; + + static get a() { + return this.#A; + } + + static set a(v) { + this.#A = v; + } + + static #B = 123; + + static get b() { + return this.#B; + } + + static set b(v) { + this.#B = v; + } + + static #C = 456; + + static get 'c'() { + return this.#C; + } + + static set 'c'(v) { + this.#C = v; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/options.json new file mode 100644 index 000000000000..9629f07e45ec --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "proposal-decorators", + { "version": "2021-12", "decoratorsBeforeExport": false } + ] + ], + "assumptions": { + "constantSuper": true + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-nested-constructor-expression/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-nested-constructor-expression/input.js new file mode 100644 index 000000000000..ebd79d22da06 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-nested-constructor-expression/input.js @@ -0,0 +1,6 @@ +@dec +class Foo extends Bar { + constructor() { + let foo = super(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-nested-constructor-expression/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-nested-constructor-expression/output.js new file mode 100644 index 000000000000..dcf5a0fae917 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-nested-constructor-expression/output.js @@ -0,0 +1,18 @@ +var _initClass; + +let _Foo; + +class Foo extends Bar { + static { + [_Foo, _initClass] = babelHelpers.applyDecs(this, [], [dec]); + } + + constructor() { + let foo = super(); + } + + static { + _initClass(); + + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-private-accessor/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-private-accessor/input.js new file mode 100644 index 000000000000..4be504d54e70 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-private-accessor/input.js @@ -0,0 +1,6 @@ +class Foo extends Bar { + @dec + get #x() { + return super.foo(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-private-accessor/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-private-accessor/output.js new file mode 100644 index 000000000000..17cd5aa63e11 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-private-accessor/output.js @@ -0,0 +1,20 @@ +var _call_x, _initProto; + +class Foo extends Bar { + static { + [_call_x, _initProto] = babelHelpers.applyDecs(this, [[dec, 3, "x", function () { + return Bar.prototype.foo.call(this); + }]], []); + } + + constructor(...args) { + super(...args); + + _initProto(this); + } + + get #x() { + return _call_x(this); + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-private-method/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-private-method/input.js new file mode 100644 index 000000000000..73200f5f673d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-private-method/input.js @@ -0,0 +1,6 @@ +class Foo extends Bar { + @dec + #x() { + return super.foo(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-private-method/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-private-method/output.js new file mode 100644 index 000000000000..91246a1f387a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-assumption-constantSuper/super-in-private-method/output.js @@ -0,0 +1,17 @@ +var _call_x, _initProto; + +class Foo extends Bar { + static { + [_call_x, _initProto] = babelHelpers.applyDecs(this, [[dec, 2, "x", function () { + return Bar.prototype.foo.call(this); + }]], []); + } + + constructor(...args) { + super(...args); + + _initProto(this); + } + + #x = _call_x; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/expressions/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/expressions/input.js new file mode 100644 index 000000000000..542bf870c079 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/expressions/input.js @@ -0,0 +1,11 @@ +const A = @dec class A {} +const B = @dec class C {} +const D = @dec class {} +const E = (@dec class {}, 123); +const F = [@dec class G {}, @dec class {}]; +const H = @dec class extends I {}; +const J = @dec class K extends L {}; + +function classFactory() { + return @dec class {} +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/expressions/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/expressions/output.js new file mode 100644 index 000000000000..b0cce043afbe --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/expressions/output.js @@ -0,0 +1,51 @@ +var _initClass, _A, _class, _temp, _initClass2, _C, _class2, _temp2, _initClass3, _D, _class3, _temp3, _initClass4, _decorated_class, _class4, _temp4, _initClass5, _G, _class5, _temp5, _initClass6, _decorated_class2, _class6, _temp6, _initClass7, _H, _class7, _temp7, _initClass8, _K, _class8, _temp8; + +const A = ((_temp = _class = class A {}, (() => { + [_A, _initClass] = babelHelpers.applyDecs(_class, [], [dec]); +})(), (() => { + _initClass(); +})(), _temp), _A); +const B = ((_temp2 = _class2 = class C {}, (() => { + [_C, _initClass2] = babelHelpers.applyDecs(_class2, [], [dec]); +})(), (() => { + _initClass2(); +})(), _temp2), _C); +const D = ((_temp3 = _class3 = class D {}, (() => { + [_D, _initClass3] = babelHelpers.applyDecs(_class3, [], [dec]); +})(), (() => { + _initClass3(); +})(), _temp3), _D); +const E = (((_temp4 = _class4 = class {}, (() => { + [_decorated_class, _initClass4] = babelHelpers.applyDecs(_class4, [], [dec]); +})(), (() => { + _initClass4(); +})(), _temp4), _decorated_class), 123); +const F = [((_temp5 = _class5 = class G {}, (() => { + [_G, _initClass5] = babelHelpers.applyDecs(_class5, [], [dec]); +})(), (() => { + _initClass5(); +})(), _temp5), _G), ((_temp6 = _class6 = class {}, (() => { + [_decorated_class2, _initClass6] = babelHelpers.applyDecs(_class6, [], [dec]); +})(), (() => { + _initClass6(); +})(), _temp6), _decorated_class2)]; +const H = ((_temp7 = _class7 = class H extends I {}, (() => { + [_H, _initClass7] = babelHelpers.applyDecs(_class7, [], [dec]); +})(), (() => { + _initClass7(); +})(), _temp7), _H); +const J = ((_temp8 = _class8 = class K extends L {}, (() => { + [_K, _initClass8] = babelHelpers.applyDecs(_class8, [], [dec]); +})(), (() => { + _initClass8(); +})(), _temp8), _K); + +function classFactory() { + var _initClass9, _decorated_class3, _class9, _temp9; + + return (_temp9 = _class9 = class {}, (() => { + [_decorated_class3, _initClass9] = babelHelpers.applyDecs(_class9, [], [dec]); + })(), (() => { + _initClass9(); + })(), _temp9), _decorated_class3; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/inheritance/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/inheritance/exec.js new file mode 100644 index 000000000000..889e8c102cf4 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/inheritance/exec.js @@ -0,0 +1,18 @@ +let count = 0; + +function dec1(Klass) { + expect(++count).toBe(1); + expect(Klass.name).toBe('Bar'); +} + +@dec1 +class Bar {} + +function dec2(Klass) { + expect(++count).toBe(2); + expect(Klass.name).toBe('Foo'); + expect(Object.getPrototypeOf(Klass)).toBe(Bar); +} + +@dec2 +class Foo extends Bar {} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/inheritance/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/inheritance/input.js new file mode 100644 index 000000000000..7a4a6684555a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/inheritance/input.js @@ -0,0 +1,5 @@ +@dec1 +class Bar {} + +@dec2 +class Foo extends Bar {} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/inheritance/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/inheritance/output.js new file mode 100644 index 000000000000..605270eadeca --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/inheritance/output.js @@ -0,0 +1,25 @@ +var _initClass, _initClass2; + +let _Bar; + +class Bar {} + +(() => { + [_Bar, _initClass] = babelHelpers.applyDecs(Bar, [], [dec1]); +})(); + +(() => { + _initClass(); +})(); + +let _Foo; + +class Foo extends _Bar {} + +(() => { + [_Foo, _initClass2] = babelHelpers.applyDecs(Foo, [], [dec2]); +})(); + +(() => { + _initClass2(); +})(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/initializers/exec.js new file mode 100644 index 000000000000..aefc3ba5b88c --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/initializers/exec.js @@ -0,0 +1,41 @@ +function dec1(Foo, { addInitializer }) { + expect(Foo.field).toBe(undefined); + addInitializer(() => { + Foo.initField = 123; + }); +} + +@dec1 +class Foo { + static { + expect(this.initField).toBe(undefined); + } + + static field = 123; +} + +expect(Foo.initField).toBe(123); +expect(Foo.field).toBe(123); + +function dec2(Bar, { addInitializer }) { + expect(Bar.field).toBe(123); + expect(Bar.otherField).toBe(undefined); + expect(Bar.initField).toBe(123); + addInitializer(() => { + Bar.initField = 456; + }); +} + +@dec2 +class Bar extends Foo { + static { + expect(this.initField).toBe(123); + this.otherField = 456; + } + + static field = 456; +} + +expect(Bar.initField).toBe(456); +expect(Bar.field).toBe(456); +expect(Bar.otherField).toBe(456); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/initializers/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/initializers/input.js new file mode 100644 index 000000000000..deeb483448f7 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/initializers/input.js @@ -0,0 +1,13 @@ +@dec +class Foo { + static field = 123; +} + +@dec +class Bar extends Foo { + static { + this.otherField = 456; + } + + static field = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/initializers/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/initializers/output.js new file mode 100644 index 000000000000..3ed28458cab0 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/initializers/output.js @@ -0,0 +1,37 @@ +var _initClass, _temp2, _initClass2, _temp4; + +let _Foo; + +new (_temp2 = class extends babelHelpers.identity { + constructor() { + var _temp; + + (_temp = super(_Foo), babelHelpers.defineProperty(this, "field", 123), _temp), _initClass(); + } + +}, (() => { + class Foo {} + + (() => { + [_Foo, _initClass] = babelHelpers.applyDecs(Foo, [], [dec]); + })(); +})(), _temp2)(); + +let _Bar; + +new (_temp4 = class extends babelHelpers.identity { + constructor() { + var _temp3; + + (_temp3 = super(_Bar), babelHelpers.defineProperty(this, "field", ((() => { + this.otherField = 456; + })(), 123)), _temp3), _initClass2(); + } + +}, (() => { + class Bar extends _Foo {} + + (() => { + [_Bar, _initClass2] = babelHelpers.applyDecs(Bar, [], [dec]); + })(); +})(), _temp4)(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/options.json new file mode 100644 index 000000000000..69f3e599cf2d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "proposal-decorators", + { "version": "2021-12", "decoratorsBeforeExport": false } + ], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-installed-on-correct-class/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-installed-on-correct-class/exec.js new file mode 100644 index 000000000000..2f4e342e3eee --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-installed-on-correct-class/exec.js @@ -0,0 +1,33 @@ +let hasX, hasM, OriginalFoo; + +class Bar {} + +function dec(Foo) { + OriginalFoo = Foo; + return Bar; +} + +@dec +class Foo { + static #x; + static #m() {} + + static x; + static m() {} + + static { + hasX = o => #x in o; + hasM = o => #m in o; + } +} + +expect(hasX(Bar)).toBe(true); +expect(hasM(Bar)).toBe(true); +expect(hasX(OriginalFoo)).toBe(false); +expect(hasM(OriginalFoo)).toBe(false); + +expect(Bar.hasOwnProperty("x")).toBe(true); +expect(OriginalFoo.hasOwnProperty("x")).toBe(false); + +expect(Bar.hasOwnProperty("m")).toBe(false); +expect(OriginalFoo.hasOwnProperty("m")).toBe(true); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-installed-on-correct-class/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-installed-on-correct-class/input.js new file mode 100644 index 000000000000..daf74ddd2d36 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-installed-on-correct-class/input.js @@ -0,0 +1,15 @@ +let hasX, hasM; + +@dec +class Foo { + static #x; + static #m() {} + + static x; + static m() {} + + static { + hasX = o => #x in o; + hasM = o => #m in o; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-installed-on-correct-class/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-installed-on-correct-class/output.js new file mode 100644 index 000000000000..f11cf07517d3 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-installed-on-correct-class/output.js @@ -0,0 +1,32 @@ +var _initClass, _x, _m, _temp2; + +let hasX, hasM; + +let _Foo; + +new (_temp2 = (_x = /*#__PURE__*/new WeakMap(), _m = /*#__PURE__*/new WeakSet(), class extends babelHelpers.identity { + constructor() { + var _temp; + + (_temp = super(_Foo), babelHelpers.classPrivateMethodInitSpec(this, _m), babelHelpers.classPrivateFieldInitSpec(this, _x, { + writable: true, + value: void 0 + }), babelHelpers.defineProperty(this, "x", void 0), _temp), (() => { + hasX = o => _x.has(o); + + hasM = o => _m.has(o); + })(), _initClass(); + } + +}), (() => { + class Foo { + static m() {} + + } + + (() => { + [_Foo, _initClass] = babelHelpers.applyDecs(Foo, [], [dec]); + })(); +})(), _temp2)(); + +function _m2() {} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-this/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-this/exec.js new file mode 100644 index 000000000000..6b55df43edc8 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-this/exec.js @@ -0,0 +1,19 @@ + +class Bar {} + +let _this, _this2, _this3; + +@(() => Bar) +class Foo { + static { + _this = this; + } + static field = (_this2 = this); + static { + _this3 = this; + } +} + +expect(_this).toBe(Bar); +expect(_this2).toBe(Bar); +expect(_this3).toBe(Bar); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-this/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-this/input.js new file mode 100644 index 000000000000..51bc09c85652 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-this/input.js @@ -0,0 +1,10 @@ +@dec +class Foo { + static { + this + } + static field = this; + static { + this + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-this/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-this/output.js new file mode 100644 index 000000000000..fe77f96f5af9 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-this/output.js @@ -0,0 +1,22 @@ +var _initClass, _temp2; + +let _Foo; + +new (_temp2 = class extends babelHelpers.identity { + constructor() { + var _temp; + + (_temp = super(_Foo), babelHelpers.defineProperty(this, "field", ((() => { + this; + })(), this)), _temp), (() => { + this; + })(), _initClass(); + } + +}, (() => { + class Foo {} + + (() => { + [_Foo, _initClass] = babelHelpers.applyDecs(Foo, [], [dec]); + })(); +})(), _temp2)(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-with-expr/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-with-expr/exec.js new file mode 100644 index 000000000000..739e33a05be5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-with-expr/exec.js @@ -0,0 +1,17 @@ +let replaced; + +function dec(Klass) { + replaced = class extends Klass {}; + + return replaced; +} + +const Foo = @dec class Bar { + static bar = new Bar(); +}; + +const foo = new Foo(); + +expect(Foo).toBe(replaced); +expect(Foo.bar).toBeInstanceOf(replaced); +expect(foo).toBeInstanceOf(replaced); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-with-expr/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-with-expr/input.js new file mode 100644 index 000000000000..fde7feb1abd7 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-with-expr/input.js @@ -0,0 +1,6 @@ +const Foo = @dec class Bar { + bar = new Bar(); +}; + +const foo = new Foo(); + diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-with-expr/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-with-expr/output.js new file mode 100644 index 000000000000..f1f8cd9df38d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-with-expr/output.js @@ -0,0 +1,13 @@ +var _initClass, _Bar, _class, _temp; + +const Foo = ((_temp = _class = class Bar { + constructor() { + babelHelpers.defineProperty(this, "bar", new _Bar()); + } + +}, (() => { + [_Bar, _initClass] = babelHelpers.applyDecs(_class, [], [dec]); +})(), (() => { + _initClass(); +})(), _temp), _Bar); +const foo = new Foo(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement/exec.js new file mode 100644 index 000000000000..c8dc49c0d85e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement/exec.js @@ -0,0 +1,19 @@ + +let replaced; + +function dec(Klass) { + replaced = class extends Klass {}; + + return replaced; +} + +@dec +class Foo { + static foo = new Foo(); +} + +const foo = new Foo(); + +expect(Foo).toBe(replaced); +expect(Foo.foo).toBeInstanceOf(replaced); +expect(foo).toBeInstanceOf(replaced); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement/input.js new file mode 100644 index 000000000000..e6da5000db62 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement/input.js @@ -0,0 +1,6 @@ +@dec +class Foo { + static foo = new Foo(); +} + +const foo = new Foo(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement/output.js new file mode 100644 index 000000000000..f302d19a6ecb --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement/output.js @@ -0,0 +1,19 @@ +var _initClass, _temp2; + +let _Foo; + +new (_temp2 = class extends babelHelpers.identity { + constructor() { + var _temp; + + (_temp = super(_Foo), babelHelpers.defineProperty(this, "foo", new _Foo()), _temp), _initClass(); + } + +}, (() => { + class Foo {} + + (() => { + [_Foo, _initClass] = babelHelpers.applyDecs(Foo, [], [dec]); + })(); +})(), _temp2)(); +const foo = new _Foo(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/expressions/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/expressions/input.js new file mode 100644 index 000000000000..542bf870c079 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/expressions/input.js @@ -0,0 +1,11 @@ +const A = @dec class A {} +const B = @dec class C {} +const D = @dec class {} +const E = (@dec class {}, 123); +const F = [@dec class G {}, @dec class {}]; +const H = @dec class extends I {}; +const J = @dec class K extends L {}; + +function classFactory() { + return @dec class {} +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/expressions/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/expressions/output.js new file mode 100644 index 000000000000..146cdb17ca70 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/expressions/output.js @@ -0,0 +1,87 @@ +var _initClass, _A, _initClass2, _C, _initClass3, _D, _initClass4, _decorated_class, _initClass5, _G, _initClass6, _decorated_class2, _initClass7, _H, _initClass8, _K; + +const A = (class A { + static { + [_A, _initClass] = babelHelpers.applyDecs(this, [], [dec]); + } + static { + _initClass(); + + } +}, _A); +const B = (class C { + static { + [_C, _initClass2] = babelHelpers.applyDecs(this, [], [dec]); + } + static { + _initClass2(); + + } +}, _C); +const D = (class D { + static { + [_D, _initClass3] = babelHelpers.applyDecs(this, [], [dec]); + } + static { + _initClass3(); + + } +}, _D); +const E = ((class { + static { + [_decorated_class, _initClass4] = babelHelpers.applyDecs(this, [], [dec]); + } + static { + _initClass4(); + + } +}, _decorated_class), 123); +const F = [(class G { + static { + [_G, _initClass5] = babelHelpers.applyDecs(this, [], [dec]); + } + static { + _initClass5(); + + } +}, _G), (class { + static { + [_decorated_class2, _initClass6] = babelHelpers.applyDecs(this, [], [dec]); + } + static { + _initClass6(); + + } +}, _decorated_class2)]; +const H = (class H extends I { + static { + [_H, _initClass7] = babelHelpers.applyDecs(this, [], [dec]); + } + static { + _initClass7(); + + } +}, _H); +const J = (class K extends L { + static { + [_K, _initClass8] = babelHelpers.applyDecs(this, [], [dec]); + } + static { + _initClass8(); + + } +}, _K); + +function classFactory() { + var _initClass9, _decorated_class3; + + return class { + static { + [_decorated_class3, _initClass9] = babelHelpers.applyDecs(this, [], [dec]); + } + static { + _initClass9(); + + } + }, _decorated_class3; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/inheritance/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/inheritance/input.js new file mode 100644 index 000000000000..7a4a6684555a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/inheritance/input.js @@ -0,0 +1,5 @@ +@dec1 +class Bar {} + +@dec2 +class Foo extends Bar {} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/inheritance/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/inheritance/output.js new file mode 100644 index 000000000000..d7ea9b35166e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/inheritance/output.js @@ -0,0 +1,25 @@ +var _initClass, _initClass2; + +let _Bar; + +class Bar { + static { + [_Bar, _initClass] = babelHelpers.applyDecs(this, [], [dec1]); + } + static { + _initClass(); + + } +} + +let _Foo; + +class Foo extends _Bar { + static { + [_Foo, _initClass2] = babelHelpers.applyDecs(this, [], [dec2]); + } + static { + _initClass2(); + + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/initializers/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/initializers/input.js new file mode 100644 index 000000000000..deeb483448f7 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/initializers/input.js @@ -0,0 +1,13 @@ +@dec +class Foo { + static field = 123; +} + +@dec +class Bar extends Foo { + static { + this.otherField = 456; + } + + static field = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/initializers/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/initializers/output.js new file mode 100644 index 000000000000..982a63a72f28 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/initializers/output.js @@ -0,0 +1,41 @@ +var _initClass, _initClass2; + +let _Foo; + +new class extends babelHelpers.identity { + static { + class Foo { + static { + [_Foo, _initClass] = babelHelpers.applyDecs(this, [], [dec]); + } + } + + } + field = 123; + + constructor() { + super(_Foo), _initClass(); + } + +}(); + +let _Bar; + +new class extends babelHelpers.identity { + static { + class Bar extends _Foo { + static { + [_Bar, _initClass2] = babelHelpers.applyDecs(this, [], [dec]); + } + } + + } + field = ((() => { + this.otherField = 456; + })(), 123); + + constructor() { + super(_Bar), _initClass2(); + } + +}(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/options.json new file mode 100644 index 000000000000..18aa205580db --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + [ + "proposal-decorators", + { "version": "2021-12", "decoratorsBeforeExport": false } + ] + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-static-installed-on-correct-class/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-static-installed-on-correct-class/input.js new file mode 100644 index 000000000000..daf74ddd2d36 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-static-installed-on-correct-class/input.js @@ -0,0 +1,15 @@ +let hasX, hasM; + +@dec +class Foo { + static #x; + static #m() {} + + static x; + static m() {} + + static { + hasX = o => #x in o; + hasM = o => #m in o; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-static-installed-on-correct-class/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-static-installed-on-correct-class/output.js new file mode 100644 index 000000000000..8f36ec034422 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-static-installed-on-correct-class/output.js @@ -0,0 +1,33 @@ +var _initClass; + +let hasX, hasM; + +let _Foo; + +new class extends babelHelpers.identity { + static { + class Foo { + static { + [_Foo, _initClass] = babelHelpers.applyDecs(this, [], [dec]); + } + + static m() {} + + } + + } + #x; + + #m() {} + + x; + + constructor() { + super(_Foo), (() => { + hasX = o => #x in o; + + hasM = o => #m in o; + })(), _initClass(); + } + +}(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-static-this/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-static-this/input.js new file mode 100644 index 000000000000..51bc09c85652 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-static-this/input.js @@ -0,0 +1,10 @@ +@dec +class Foo { + static { + this + } + static field = this; + static { + this + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-static-this/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-static-this/output.js new file mode 100644 index 000000000000..5ca20ff8d696 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-static-this/output.js @@ -0,0 +1,24 @@ +var _initClass; + +let _Foo; + +new class extends babelHelpers.identity { + static { + class Foo { + static { + [_Foo, _initClass] = babelHelpers.applyDecs(this, [], [dec]); + } + } + + } + field = ((() => { + this; + })(), this); + + constructor() { + super(_Foo), (() => { + this; + })(), _initClass(); + } + +}(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-with-expr/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-with-expr/input.js new file mode 100644 index 000000000000..fde7feb1abd7 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-with-expr/input.js @@ -0,0 +1,6 @@ +const Foo = @dec class Bar { + bar = new Bar(); +}; + +const foo = new Foo(); + diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-with-expr/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-with-expr/output.js new file mode 100644 index 000000000000..4c37edebb1dd --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement-with-expr/output.js @@ -0,0 +1,13 @@ +var _initClass, _Bar; + +const Foo = (class Bar { + static { + [_Bar, _initClass] = babelHelpers.applyDecs(this, [], [dec]); + } + bar = new _Bar(); + static { + _initClass(); + + } +}, _Bar); +const foo = new Foo(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement/input.js new file mode 100644 index 000000000000..e6da5000db62 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement/input.js @@ -0,0 +1,6 @@ +@dec +class Foo { + static foo = new Foo(); +} + +const foo = new Foo(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement/output.js new file mode 100644 index 000000000000..42a115d10f2b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes/replacement/output.js @@ -0,0 +1,21 @@ +var _initClass; + +let _Foo; + +new class extends babelHelpers.identity { + static { + class Foo { + static { + [_Foo, _initClass] = babelHelpers.applyDecs(this, [], [dec]); + } + } + + } + foo = new _Foo(); + + constructor() { + super(_Foo), _initClass(); + } + +}(); +const foo = new _Foo(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-ast/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-ast/exec.js new file mode 100644 index 000000000000..49fad189f4e2 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-ast/exec.js @@ -0,0 +1,33 @@ +var i = 0; + +function getKey() { + return (i++).toString(); +} + +let elements = []; + +function dec(fn, context) { + elements.push({ fn, context }); +} + +class Foo { + @dec + [getKey()]() { + return 1; + } + + @dec + [getKey()]() { + return 2; + } +} + +expect(elements).toHaveLength(2); + +expect(elements[0].context.name).toBe("0"); +expect(elements[0].fn()).toBe(1); + +expect(elements[1].context.name).toBe("1"); +expect(elements[1].fn()).toBe(2); + +expect(i).toBe(2); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-ast/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-ast/input.js new file mode 100644 index 000000000000..2ca438188867 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-ast/input.js @@ -0,0 +1,11 @@ +class Foo { + @dec + [getKey()]() { + return 1; + } + + @dec + [getKey()]() { + return 2; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-ast/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-ast/output.js new file mode 100644 index 000000000000..a70d24de54c9 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-ast/output.js @@ -0,0 +1,25 @@ +var _computedKey, _computedKey2, _dec, _dec2, _initProto; + +_computedKey = getKey() +_dec = dec +_computedKey2 = getKey() +_dec2 = dec + +class Foo { + constructor(...args) { + _initProto(this); + } + + [_computedKey]() { + return 1; + } + + [_computedKey2]() { + return 2; + } + +} + +(() => { + [_initProto] = babelHelpers.applyDecs(Foo, [[_dec, 2, _computedKey], [_dec2, 2, _computedKey2]], []); +})(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-value/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-value/exec.js new file mode 100644 index 000000000000..c97e2293ac96 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-value/exec.js @@ -0,0 +1,29 @@ +expect(() => { + var i = 0; + var j = 0; + + function getKeyI() { + return (i++).toString(); + } + function getKeyJ() { + return (j++).toString(); + } + + let elements = []; + + function dec(fn, context) { + elements.push({ fn, context }); + } + + class Foo { + @dec + [getKeyI()]() { + return 1; + } + + @dec + [getKeyJ()]() { + return 2; + } + } +}).toThrow("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: 0") diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-value/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-value/input.js new file mode 100644 index 000000000000..8aeb57759b76 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-value/input.js @@ -0,0 +1,11 @@ +class Foo { + @dec + [getKeyI()]() { + return 1; + } + + @dec + [getKeyJ()]() { + return 2; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-value/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-value/output.js new file mode 100644 index 000000000000..66e2ac318d83 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/computed-keys-same-value/output.js @@ -0,0 +1,25 @@ +var _computedKey, _computedKey2, _dec, _dec2, _initProto; + +_computedKey = getKeyI() +_dec = dec +_computedKey2 = getKeyJ() +_dec2 = dec + +class Foo { + constructor(...args) { + _initProto(this); + } + + [_computedKey]() { + return 1; + } + + [_computedKey2]() { + return 2; + } + +} + +(() => { + [_initProto] = babelHelpers.applyDecs(Foo, [[_dec, 2, _computedKey], [_dec2, 2, _computedKey2]], []); +})(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/method-and-field/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/method-and-field/exec.js new file mode 100644 index 000000000000..8ac906f1823a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/method-and-field/exec.js @@ -0,0 +1,23 @@ +let elements = []; + +function dec(val, context) { + elements.push({ val, context }); +} + +class Foo { + @dec + a = 123; + + @dec + a() { + return 1; + } +} + +expect(elements).toHaveLength(2); + +expect(elements[0].context.name).toBe("a"); +expect(elements[0].val).toBe(undefined); + +expect(elements[1].context.name).toBe("a"); +expect(elements[1].val()).toBe(1); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/method-and-field/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/method-and-field/input.js new file mode 100644 index 000000000000..522da0061204 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/method-and-field/input.js @@ -0,0 +1,9 @@ +class Foo { + @dec + a = 123; + + @dec + a() { + return 1; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/method-and-field/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/method-and-field/output.js new file mode 100644 index 000000000000..fe79aa632006 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/method-and-field/output.js @@ -0,0 +1,16 @@ +var _init_a, _initProto; + +class Foo { + constructor() { + babelHelpers.defineProperty(this, "a", (_initProto(this), _init_a(this, 123))); + } + + a() { + return 1; + } + +} + +(() => { + [_init_a, _initProto] = babelHelpers.applyDecs(Foo, [[dec, 0, "a"], [dec, 2, "a"]], []); +})(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/methods-with-same-key/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/methods-with-same-key/exec.js new file mode 100644 index 000000000000..9e270b2a7488 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/methods-with-same-key/exec.js @@ -0,0 +1,19 @@ +expect(() => { + let elements = []; + + function dec(val, context) { + elements.push({ val, context }); + } + + class Foo { + @dec + a() { + return 1; + } + + @dec + a() { + return 2; + } + } +}).toThrow("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: a") diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/methods-with-same-key/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/methods-with-same-key/input.js new file mode 100644 index 000000000000..db90a40aa67b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/methods-with-same-key/input.js @@ -0,0 +1,11 @@ +class Foo { + @dec + a() { + return 1; + } + + @dec + a() { + return 2; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/methods-with-same-key/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/methods-with-same-key/output.js new file mode 100644 index 000000000000..6402e1341ff0 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/methods-with-same-key/output.js @@ -0,0 +1,20 @@ +var _initProto; + +class Foo { + constructor(...args) { + _initProto(this); + } + + a() { + return 1; + } + + a() { + return 2; + } + +} + +(() => { + [_initProto] = babelHelpers.applyDecs(Foo, [[dec, 2, "a"], [dec, 2, "a"]], []); +})(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/options.json new file mode 100644 index 000000000000..69f3e599cf2d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "proposal-decorators", + { "version": "2021-12", "decoratorsBeforeExport": false } + ], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/computed-keys-same-ast/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/computed-keys-same-ast/input.js new file mode 100644 index 000000000000..2ca438188867 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/computed-keys-same-ast/input.js @@ -0,0 +1,11 @@ +class Foo { + @dec + [getKey()]() { + return 1; + } + + @dec + [getKey()]() { + return 2; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/computed-keys-same-ast/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/computed-keys-same-ast/output.js new file mode 100644 index 000000000000..9663aebaf7b3 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/computed-keys-same-ast/output.js @@ -0,0 +1,25 @@ +var _computedKey, _computedKey2, _dec, _dec2, _initProto; + +_computedKey = getKey() +_dec = dec +_computedKey2 = getKey() +_dec2 = dec + +class Foo { + static { + [_initProto] = babelHelpers.applyDecs(this, [[_dec, 2, _computedKey], [_dec2, 2, _computedKey2]], []); + } + + constructor(...args) { + _initProto(this); + } + + [_computedKey]() { + return 1; + } + + [_computedKey2]() { + return 2; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/computed-keys-same-value/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/computed-keys-same-value/input.js new file mode 100644 index 000000000000..8aeb57759b76 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/computed-keys-same-value/input.js @@ -0,0 +1,11 @@ +class Foo { + @dec + [getKeyI()]() { + return 1; + } + + @dec + [getKeyJ()]() { + return 2; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/computed-keys-same-value/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/computed-keys-same-value/output.js new file mode 100644 index 000000000000..76d9f05cad75 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/computed-keys-same-value/output.js @@ -0,0 +1,25 @@ +var _computedKey, _computedKey2, _dec, _dec2, _initProto; + +_computedKey = getKeyI() +_dec = dec +_computedKey2 = getKeyJ() +_dec2 = dec + +class Foo { + static { + [_initProto] = babelHelpers.applyDecs(this, [[_dec, 2, _computedKey], [_dec2, 2, _computedKey2]], []); + } + + constructor(...args) { + _initProto(this); + } + + [_computedKey]() { + return 1; + } + + [_computedKey2]() { + return 2; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/method-and-field/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/method-and-field/input.js new file mode 100644 index 000000000000..522da0061204 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/method-and-field/input.js @@ -0,0 +1,9 @@ +class Foo { + @dec + a = 123; + + @dec + a() { + return 1; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/method-and-field/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/method-and-field/output.js new file mode 100644 index 000000000000..cb04e3cc2e5d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/method-and-field/output.js @@ -0,0 +1,13 @@ +var _init_a, _initProto; + +class Foo { + static { + [_init_a, _initProto] = babelHelpers.applyDecs(this, [[dec, 0, "a"], [dec, 2, "a"]], []); + } + a = (_initProto(this), _init_a(this, 123)); + + a() { + return 1; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/methods-with-same-key/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/methods-with-same-key/input.js new file mode 100644 index 000000000000..db90a40aa67b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/methods-with-same-key/input.js @@ -0,0 +1,11 @@ +class Foo { + @dec + a() { + return 1; + } + + @dec + a() { + return 2; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/methods-with-same-key/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/methods-with-same-key/output.js new file mode 100644 index 000000000000..c4befb4def4d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/methods-with-same-key/output.js @@ -0,0 +1,20 @@ +var _initProto; + +class Foo { + static { + [_initProto] = babelHelpers.applyDecs(this, [[dec, 2, "a"], [dec, 2, "a"]], []); + } + + constructor(...args) { + _initProto(this); + } + + a() { + return 1; + } + + a() { + return 2; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/options.json new file mode 100644 index 000000000000..18aa205580db --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + [ + "proposal-decorators", + { "version": "2021-12", "decoratorsBeforeExport": false } + ] + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/options.json new file mode 100644 index 000000000000..69f3e599cf2d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "proposal-decorators", + { "version": "2021-12", "decoratorsBeforeExport": false } + ], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/private/exec.js new file mode 100644 index 000000000000..9d368edd0f8b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/private/exec.js @@ -0,0 +1,41 @@ +function dec(_v, context) { + return function (v) { + this[context.name + 'Context'] = context; + return (v || 1) + 1; + } +} + +class Foo { + @dec + #a; + + @dec + #b = 123; +} + +let foo = new Foo(); + +const aContext = foo['#aContext']; +const bContext = foo['#bContext']; + +expect(aContext.access.get.call(foo)).toBe(2); +aContext.access.set.call(foo, 123); +expect(aContext.access.get.call(foo)).toBe(123); +expect(aContext.name).toBe('#a'); +expect(aContext.kind).toBe('field'); +expect(aContext.isStatic).toBe(false); +expect(aContext.isPrivate).toBe(true); +expect(typeof aContext.addInitializer).toBe('undefined'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); + +expect(bContext.access.get.call(foo)).toBe(124); +bContext.access.set.call(foo, 123); +expect(bContext.access.get.call(foo)).toBe(123); +expect(bContext.name).toBe('#b'); +expect(bContext.kind).toBe('field'); +expect(bContext.isStatic).toBe(false); +expect(bContext.isPrivate).toBe(true); +expect(typeof aContext.addInitializer).toBe('undefined'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/private/input.js new file mode 100644 index 000000000000..222ec09fc95e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/private/input.js @@ -0,0 +1,7 @@ +class Foo { + @dec + #a; + + @dec + #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/private/output.js new file mode 100644 index 000000000000..af27fce2d289 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/private/output.js @@ -0,0 +1,31 @@ +var _init_a, _init_b; + +var _a = /*#__PURE__*/new WeakMap(); + +var _b = /*#__PURE__*/new WeakMap(); + +class Foo { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _a, { + writable: true, + value: _init_a(this) + }); + babelHelpers.classPrivateFieldInitSpec(this, _b, { + writable: true, + value: _init_b(this, 123) + }); + } + +} + +(() => { + [_init_a, _init_b] = babelHelpers.applyDecs(Foo, [[dec, 0, "a", function () { + return babelHelpers.classPrivateFieldGet(this, _a); + }, function (value) { + babelHelpers.classPrivateFieldSet(this, _a, value); + }], [dec, 0, "b", function () { + return babelHelpers.classPrivateFieldGet(this, _b); + }, function (value) { + babelHelpers.classPrivateFieldSet(this, _b, value); + }]], []); +})(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/public/exec.js new file mode 100644 index 000000000000..0d5ed78cf745 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/public/exec.js @@ -0,0 +1,62 @@ +function dec(_v, context) { + return function (v) { + this[context.name + 'Context'] = context; + return (v || 1) + 1; + } +} + +class Foo { + @dec + a; + + @dec + b = 123; + + @dec + ['c'] = 456; +} + +let foo = new Foo(); + +const aContext = foo['aContext']; +const bContext = foo['bContext']; +const cContext = foo['cContext']; + +expect(foo.a).toBe(2); +foo.a = 123; +expect(foo.a).toBe(123); +expect(aContext.name).toBe('a'); +expect(aContext.kind).toBe('field'); +expect(aContext.isStatic).toBe(false); +expect(aContext.isPrivate).toBe(false); +expect(typeof aContext.addInitializer).toBe('undefined'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); +expect(foo.hasOwnProperty('a')).toBe(true); +expect(Foo.prototype.hasOwnProperty('a')).toBe(false); + +expect(foo.b).toBe(124); +foo.b = 123; +expect(foo.b).toBe(123); +expect(bContext.name).toBe('b'); +expect(bContext.kind).toBe('field'); +expect(bContext.isStatic).toBe(false); +expect(bContext.isPrivate).toBe(false); +expect(typeof bContext.addInitializer).toBe('undefined'); +expect(typeof bContext.setMetadata).toBe('function'); +expect(typeof bContext.getMetadata).toBe('function'); +expect(foo.hasOwnProperty('b')).toBe(true); +expect(Foo.prototype.hasOwnProperty('b')).toBe(false); + +expect(foo.c).toBe(457); +foo.c = 456; +expect(foo.c).toBe(456); +expect(cContext.name).toBe('c'); +expect(cContext.kind).toBe('field'); +expect(cContext.isStatic).toBe(false); +expect(cContext.isPrivate).toBe(false); +expect(typeof cContext.addInitializer).toBe('undefined'); +expect(typeof cContext.setMetadata).toBe('function'); +expect(typeof cContext.getMetadata).toBe('function'); +expect(foo.hasOwnProperty('c')).toBe(true); +expect(Foo.prototype.hasOwnProperty('c')).toBe(false); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/public/input.js new file mode 100644 index 000000000000..8dff95c99a3a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/public/input.js @@ -0,0 +1,10 @@ +class Foo { + @dec + a; + + @dec + b = 123; + + @dec + ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/public/output.js new file mode 100644 index 000000000000..481926ddfe5a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/public/output.js @@ -0,0 +1,19 @@ +var _init_a, _init_b, _computedKey, _init_computedKey, _dec, _dec2, _dec3; + +_dec = dec +_dec2 = dec +_computedKey = 'c' +_dec3 = dec + +class Foo { + constructor() { + babelHelpers.defineProperty(this, "a", _init_a(this)); + babelHelpers.defineProperty(this, "b", _init_b(this, 123)); + babelHelpers.defineProperty(this, _computedKey, _init_computedKey(this, 456)); + } + +} + +(() => { + [_init_a, _init_b, _init_computedKey] = babelHelpers.applyDecs(Foo, [[_dec, 0, "a"], [_dec2, 0, "b"], [_dec3, 0, _computedKey]], []); +})(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-private/exec.js new file mode 100644 index 000000000000..51fba146164c --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-private/exec.js @@ -0,0 +1,39 @@ +function dec(_v, context) { + return function (v) { + this[context.name + 'Context'] = context; + return (v || 1) + 1; + } +} + +class Foo { + @dec + static #a; + + @dec + static #b = 123; +} + +const aContext = Foo['#aContext']; +const bContext = Foo['#bContext']; + +expect(aContext.access.get.call(Foo)).toBe(2); +aContext.access.set.call(Foo, 123); +expect(aContext.access.get.call(Foo)).toBe(123); +expect(aContext.name).toBe('#a'); +expect(aContext.kind).toBe('field'); +expect(aContext.isStatic).toBe(true); +expect(aContext.isPrivate).toBe(true); +expect(typeof aContext.addInitializer).toBe('undefined'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); + +expect(bContext.access.get.call(Foo)).toBe(124); +bContext.access.set.call(Foo, 123); +expect(bContext.access.get.call(Foo)).toBe(123); +expect(bContext.name).toBe('#b'); +expect(bContext.kind).toBe('field'); +expect(bContext.isStatic).toBe(true); +expect(bContext.isPrivate).toBe(true); +expect(typeof aContext.addInitializer).toBe('undefined'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-private/input.js new file mode 100644 index 000000000000..830d82321863 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-private/input.js @@ -0,0 +1,7 @@ +class Foo { + @dec + static #a; + + @dec + static #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-private/output.js new file mode 100644 index 000000000000..ad619ac7d598 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-private/output.js @@ -0,0 +1,24 @@ +var _init_a, _init_b; + +class Foo {} + +(() => { + [_init_a, _init_b] = babelHelpers.applyDecs(Foo, [[dec, 5, "a", function () { + return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _a); + }, function (value) { + babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _a, value); + }], [dec, 5, "b", function () { + return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _b); + }, function (value) { + babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _b, value); + }]], []); +})(); + +var _a = { + writable: true, + value: _init_a(Foo) +}; +var _b = { + writable: true, + value: _init_b(Foo, 123) +}; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-public/exec.js new file mode 100644 index 000000000000..48af3023539c --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-public/exec.js @@ -0,0 +1,57 @@ +function dec(_v, context) { + return function (v) { + this[context.name + 'Context'] = context; + return (v || 1) + 1; + } +} + +class Foo { + @dec + static a; + + @dec + static b = 123; + + @dec + static ['c'] = 456; +} + +const aContext = Foo['aContext']; +const bContext = Foo['bContext']; +const cContext = Foo['cContext']; + +expect(Foo.a).toBe(2); +Foo.a = 123; +expect(Foo.a).toBe(123); +expect(aContext.name).toBe('a'); +expect(aContext.kind).toBe('field'); +expect(aContext.isStatic).toBe(true); +expect(aContext.isPrivate).toBe(false); +expect(typeof aContext.addInitializer).toBe('undefined'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); +expect(Foo.hasOwnProperty('a')).toBe(true); + +expect(Foo.b).toBe(124); +Foo.b = 123; +expect(Foo.b).toBe(123); +expect(bContext.name).toBe('b'); +expect(bContext.kind).toBe('field'); +expect(bContext.isStatic).toBe(true); +expect(bContext.isPrivate).toBe(false); +expect(typeof bContext.addInitializer).toBe('undefined'); +expect(typeof bContext.setMetadata).toBe('function'); +expect(typeof bContext.getMetadata).toBe('function'); +expect(Foo.hasOwnProperty('b')).toBe(true); + +expect(Foo.c).toBe(457); +Foo.c = 456; +expect(Foo.c).toBe(456); +expect(cContext.name).toBe('c'); +expect(cContext.kind).toBe('field'); +expect(cContext.isStatic).toBe(true); +expect(cContext.isPrivate).toBe(false); +expect(typeof cContext.addInitializer).toBe('undefined'); +expect(typeof cContext.setMetadata).toBe('function'); +expect(typeof cContext.getMetadata).toBe('function'); +expect(Foo.hasOwnProperty('c')).toBe(true); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-public/input.js new file mode 100644 index 000000000000..9b9dae98314a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-public/input.js @@ -0,0 +1,10 @@ +class Foo { + @dec + static a; + + @dec + static b = 123; + + @dec + static ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-public/output.js new file mode 100644 index 000000000000..c5df8314385b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields--to-es2015/static-public/output.js @@ -0,0 +1,16 @@ +var _init_a, _init_b, _computedKey, _init_computedKey, _dec, _dec2, _dec3; + +_dec = dec +_dec2 = dec +_computedKey = 'c' +_dec3 = dec + +class Foo {} + +(() => { + [_init_a, _init_b, _init_computedKey] = babelHelpers.applyDecs(Foo, [[_dec, 5, "a"], [_dec2, 5, "b"], [_dec3, 5, _computedKey]], []); +})(); + +babelHelpers.defineProperty(Foo, "a", _init_a(Foo)); +babelHelpers.defineProperty(Foo, "b", _init_b(Foo, 123)); +babelHelpers.defineProperty(Foo, _computedKey, _init_computedKey(Foo, 456)); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/options.json new file mode 100644 index 000000000000..18aa205580db --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + [ + "proposal-decorators", + { "version": "2021-12", "decoratorsBeforeExport": false } + ] + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/private/input.js new file mode 100644 index 000000000000..222ec09fc95e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/private/input.js @@ -0,0 +1,7 @@ +class Foo { + @dec + #a; + + @dec + #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/private/output.js new file mode 100644 index 000000000000..b72a6d971df6 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/private/output.js @@ -0,0 +1,17 @@ +var _init_a, _init_b; + +class Foo { + static { + [_init_a, _init_b] = babelHelpers.applyDecs(this, [[dec, 0, "a", function () { + return this.#a; + }, function (value) { + this.#a = value; + }], [dec, 0, "b", function () { + return this.#b; + }, function (value) { + this.#b = value; + }]], []); + } + #a = _init_a(this); + #b = _init_b(this, 123); +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/public/input.js new file mode 100644 index 000000000000..8dff95c99a3a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/public/input.js @@ -0,0 +1,10 @@ +class Foo { + @dec + a; + + @dec + b = 123; + + @dec + ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/public/output.js new file mode 100644 index 000000000000..06c0434d04a6 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/public/output.js @@ -0,0 +1,15 @@ +var _init_a, _init_b, _computedKey, _init_computedKey, _dec, _dec2, _dec3; + +_dec = dec +_dec2 = dec +_computedKey = 'c' +_dec3 = dec + +class Foo { + static { + [_init_a, _init_b, _init_computedKey] = babelHelpers.applyDecs(this, [[_dec, 0, "a"], [_dec2, 0, "b"], [_dec3, 0, _computedKey]], []); + } + a = _init_a(this); + b = _init_b(this, 123); + [_computedKey] = _init_computedKey(this, 456); +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/static-private/input.js new file mode 100644 index 000000000000..830d82321863 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/static-private/input.js @@ -0,0 +1,7 @@ +class Foo { + @dec + static #a; + + @dec + static #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/static-private/output.js new file mode 100644 index 000000000000..cb167ff192fd --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/static-private/output.js @@ -0,0 +1,17 @@ +var _init_a, _init_b; + +class Foo { + static { + [_init_a, _init_b] = babelHelpers.applyDecs(this, [[dec, 5, "a", function () { + return this.#a; + }, function (value) { + this.#a = value; + }], [dec, 5, "b", function () { + return this.#b; + }, function (value) { + this.#b = value; + }]], []); + } + static #a = _init_a(this); + static #b = _init_b(this, 123); +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/static-public/input.js new file mode 100644 index 000000000000..9b9dae98314a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/static-public/input.js @@ -0,0 +1,10 @@ +class Foo { + @dec + static a; + + @dec + static b = 123; + + @dec + static ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/static-public/output.js new file mode 100644 index 000000000000..64af25745500 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-fields/static-public/output.js @@ -0,0 +1,15 @@ +var _init_a, _init_b, _computedKey, _init_computedKey, _dec, _dec2, _dec3; + +_dec = dec +_dec2 = dec +_computedKey = 'c' +_dec3 = dec + +class Foo { + static { + [_init_a, _init_b, _init_computedKey] = babelHelpers.applyDecs(this, [[_dec, 5, "a"], [_dec2, 5, "b"], [_dec3, 5, _computedKey]], []); + } + static a = _init_a(this); + static b = _init_b(this, 123); + static [_computedKey] = _init_computedKey(this, 456); +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/options.json new file mode 100644 index 000000000000..69f3e599cf2d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "proposal-decorators", + { "version": "2021-12", "decoratorsBeforeExport": false } + ], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/private/exec.js new file mode 100644 index 000000000000..70c0965c67e9 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/private/exec.js @@ -0,0 +1,39 @@ +function dec(get, context) { + context.addInitializer((instance) => { + instance[context.name + 'Context'] = context; + }); + + return function () { + return get.call(this) + 1; + } +} + +class Foo { + value = 1; + + @dec + get #a() { + return this.value; + } + + getA() { + return this.#a; + } +} + +let foo = new Foo(); + +const aContext = foo['#aContext']; + +expect(aContext.access.get.call(foo)).toBe(2); +expect(foo.getA()).toBe(2); +foo.value = 123; +expect(aContext.access.get.call(foo)).toBe(124); +expect(foo.getA()).toBe(124); +expect(aContext.name).toBe('#a'); +expect(aContext.kind).toBe('getter'); +expect(aContext.isStatic).toBe(false); +expect(aContext.isPrivate).toBe(true); +expect(typeof aContext.addInitializer).toBe('function'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/private/input.js new file mode 100644 index 000000000000..49c601a1dd5b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/private/input.js @@ -0,0 +1,12 @@ +class Foo { + value = 1; + + @dec + get #a() { + return this.value; + } + + getA() { + return this.#a; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/private/output.js new file mode 100644 index 000000000000..32971ca073e9 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/private/output.js @@ -0,0 +1,30 @@ +var _call_a, _initProto; + +var _a = /*#__PURE__*/new WeakMap(); + +class Foo { + constructor(...args) { + babelHelpers.classPrivateFieldInitSpec(this, _a, { + get: _get_a, + set: void 0 + }); + babelHelpers.defineProperty(this, "value", 1); + + _initProto(this); + } + + getA() { + return babelHelpers.classPrivateFieldGet(this, _a); + } + +} + +function _get_a() { + return _call_a(this); +} + +(() => { + [_call_a, _initProto] = babelHelpers.applyDecs(Foo, [[dec, 3, "a", function () { + return this.value; + }]], []); +})(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/public/exec.js new file mode 100644 index 000000000000..22f52e96d4f2 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/public/exec.js @@ -0,0 +1,50 @@ +function dec(get, context) { + context.addInitializer((instance) => { + instance[context.name + 'Context'] = context; + }); + + return function () { + return get.call(this) + 1; + } +} + +class Foo { + value = 1; + + @dec + get a() { + return this.value; + } + + @dec + get ['b']() { + return this.value; + } +} + +let foo = new Foo(); + +const aContext = foo['aContext']; +const bContext = foo['bContext']; + +expect(foo.a).toBe(2); +expect(foo.b).toBe(2); +foo.value = 123; +expect(foo.a).toBe(124); +expect(foo.b).toBe(124); + +expect(aContext.name).toBe('a'); +expect(aContext.kind).toBe('getter'); +expect(aContext.isStatic).toBe(false); +expect(aContext.isPrivate).toBe(false); +expect(typeof aContext.addInitializer).toBe('function'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); + +expect(bContext.name).toBe('b'); +expect(bContext.kind).toBe('getter'); +expect(bContext.isStatic).toBe(false); +expect(bContext.isPrivate).toBe(false); +expect(typeof bContext.addInitializer).toBe('function'); +expect(typeof bContext.setMetadata).toBe('function'); +expect(typeof bContext.getMetadata).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/public/input.js new file mode 100644 index 000000000000..9d1cbae52f1d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/public/input.js @@ -0,0 +1,13 @@ +class Foo { + value = 1; + + @dec + get a() { + return this.value; + } + + @dec + get ['b']() { + return this.value; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/public/output.js new file mode 100644 index 000000000000..0152dbf977de --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/public/output.js @@ -0,0 +1,26 @@ +var _computedKey, _dec, _dec2, _initProto; + +_dec = dec +_computedKey = 'b' +_dec2 = dec + +class Foo { + constructor(...args) { + babelHelpers.defineProperty(this, "value", 1); + + _initProto(this); + } + + get a() { + return this.value; + } + + get [_computedKey]() { + return this.value; + } + +} + +(() => { + [_initProto] = babelHelpers.applyDecs(Foo, [[_dec, 3, "a"], [_dec2, 3, _computedKey]], []); +})(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-private/exec.js new file mode 100644 index 000000000000..7d1c8027d545 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-private/exec.js @@ -0,0 +1,38 @@ +function dec(get, context) { + context.addInitializer((instance) => { + instance[context.name + 'Context'] = context; + }); + + return function () { + return get.call(this) + 1; + } +} + +class Foo { + static value = 1; + + @dec + static get #a() { + return this.value; + } + + static getA() { + return this.#a; + } +} + +const aContext = Foo['#aContext']; + +expect(aContext.access.get.call(Foo)).toBe(2); +expect(Foo.getA()).toBe(2); +Foo.value = 123; +expect(aContext.access.get.call(Foo)).toBe(124); +expect(Foo.getA()).toBe(124); + +expect(aContext.name).toBe('#a'); +expect(aContext.kind).toBe('getter'); +expect(aContext.isStatic).toBe(true); +expect(aContext.isPrivate).toBe(true); +expect(typeof aContext.addInitializer).toBe('function'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-private/input.js new file mode 100644 index 000000000000..90edd26364d8 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-private/input.js @@ -0,0 +1,12 @@ +class Foo { + static value = 1; + + @dec + static get #a() { + return this.value; + } + + static getA() { + return this.#a; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-private/output.js new file mode 100644 index 000000000000..e0262570f9e3 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-private/output.js @@ -0,0 +1,27 @@ +var _call_a, _initStatic; + +class Foo { + static getA() { + return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _a); + } + +} + +function _get_a() { + return _call_a(this); +} + +var _a = { + get: _get_a, + set: void 0 +}; + +(() => { + [_call_a, _initStatic] = babelHelpers.applyDecs(Foo, [[dec, 8, "a", function () { + return this.value; + }]], []); + + _initStatic(Foo); +})(); + +babelHelpers.defineProperty(Foo, "value", 1); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-public/exec.js new file mode 100644 index 000000000000..94c7b9d5dab5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-public/exec.js @@ -0,0 +1,48 @@ +function dec(get, context) { + context.addInitializer((instance) => { + instance[context.name + 'Context'] = context; + }); + + return function () { + return get.call(this) + 1; + } +} + +class Foo { + static value = 1; + + @dec + static get a() { + return this.value; + } + + @dec + static get ['b']() { + return this.value; + } +} + +const aContext = Foo['aContext']; +const bContext = Foo['bContext']; + +expect(Foo.a).toBe(2); +expect(Foo.b).toBe(2); +Foo.value = 123; +expect(Foo.a).toBe(124); +expect(Foo.b).toBe(124); + +expect(aContext.name).toBe('a'); +expect(aContext.kind).toBe('getter'); +expect(aContext.isStatic).toBe(true); +expect(aContext.isPrivate).toBe(false); +expect(typeof aContext.addInitializer).toBe('function'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); + +expect(bContext.name).toBe('b'); +expect(bContext.kind).toBe('getter'); +expect(bContext.isStatic).toBe(true); +expect(bContext.isPrivate).toBe(false); +expect(typeof bContext.addInitializer).toBe('function'); +expect(typeof bContext.setMetadata).toBe('function'); +expect(typeof bContext.getMetadata).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-public/input.js new file mode 100644 index 000000000000..1383fa58abf4 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-public/input.js @@ -0,0 +1,13 @@ +class Foo { + static value = 1; + + @dec + static get a() { + return this.value; + } + + @dec + static get ['b']() { + return this.value; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-public/output.js new file mode 100644 index 000000000000..68787d432777 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters--to-es2015/static-public/output.js @@ -0,0 +1,24 @@ +var _computedKey, _dec, _dec2, _initStatic; + +_dec = dec +_computedKey = 'b' +_dec2 = dec + +class Foo { + static get a() { + return this.value; + } + + static get [_computedKey]() { + return this.value; + } + +} + +(() => { + [_initStatic] = babelHelpers.applyDecs(Foo, [[_dec, 8, "a"], [_dec2, 8, _computedKey]], []); + + _initStatic(Foo); +})(); + +babelHelpers.defineProperty(Foo, "value", 1); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/options.json new file mode 100644 index 000000000000..69f3e599cf2d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "proposal-decorators", + { "version": "2021-12", "decoratorsBeforeExport": false } + ], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/private/exec.js new file mode 100644 index 000000000000..a4f9a31d2589 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/private/exec.js @@ -0,0 +1,67 @@ +function dec(value, context) { + context.addInitializer((instance) => { + instance[context.name + '_' + context.kind + 'Context'] = context; + }); + + if (context.kind === 'getter') { + return function () { + return value.call(this) + 1; + } + } else { + return function (v) { + return value.call(this, v + 1); + } + } +} + +class Foo { + value = 1; + + @dec + get #a() { + return this.value; + } + + @dec + set #a(v) { + this.value = v; + } + + getA() { + return this.#a; + } + + setA(v) { + this.#a = v; + } +} + +let foo = new Foo(); + +const a_getterContext = foo['#a_getterContext']; +const a_setterContext = foo['#a_setterContext']; + +expect(a_getterContext.access.get.call(foo)).toBe(2); +expect(foo.getA()).toBe(2); +a_setterContext.access.set.call(foo, 123); +expect(a_getterContext.access.get.call(foo)).toBe(125); +expect(foo.getA()).toBe(125); +foo.setA(456); +expect(a_getterContext.access.get.call(foo)).toBe(458); +expect(foo.getA()).toBe(458); + +expect(a_getterContext.name).toBe('#a'); +expect(a_getterContext.kind).toBe('getter'); +expect(a_getterContext.isStatic).toBe(false); +expect(a_getterContext.isPrivate).toBe(true); +expect(typeof a_getterContext.addInitializer).toBe('function'); +expect(typeof a_getterContext.setMetadata).toBe('function'); +expect(typeof a_getterContext.getMetadata).toBe('function'); + +expect(a_setterContext.name).toBe('#a'); +expect(a_setterContext.kind).toBe('setter'); +expect(a_setterContext.isStatic).toBe(false); +expect(a_setterContext.isPrivate).toBe(true); +expect(typeof a_setterContext.addInitializer).toBe('function'); +expect(typeof a_setterContext.setMetadata).toBe('function'); +expect(typeof a_setterContext.getMetadata).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/private/input.js new file mode 100644 index 000000000000..060ae225a21e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/private/input.js @@ -0,0 +1,21 @@ +class Foo { + value = 1; + + @dec + get #a() { + return this.value; + } + + @dec + set #a(v) { + this.value = v; + } + + getA() { + return this.#a; + } + + setA(v) { + this.#a = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/private/output.js new file mode 100644 index 000000000000..71e95a6ee2a2 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/private/output.js @@ -0,0 +1,40 @@ +var _call_a, _call_a2, _initProto; + +var _a = /*#__PURE__*/new WeakMap(); + +class Foo { + constructor(...args) { + babelHelpers.classPrivateFieldInitSpec(this, _a, { + get: _get_a, + set: _set_a + }); + babelHelpers.defineProperty(this, "value", 1); + + _initProto(this); + } + + getA() { + return babelHelpers.classPrivateFieldGet(this, _a); + } + + setA(v) { + babelHelpers.classPrivateFieldSet(this, _a, v); + } + +} + +function _get_a() { + return _call_a(this); +} + +function _set_a(v) { + _call_a2(this, v); +} + +(() => { + [_call_a, _call_a2, _initProto] = babelHelpers.applyDecs(Foo, [[dec, 3, "a", function () { + return this.value; + }], [dec, 4, "a", function (v) { + this.value = v; + }]], []); +})(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/public/exec.js new file mode 100644 index 000000000000..0420e538875b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/public/exec.js @@ -0,0 +1,88 @@ +function dec(value, context) { + context.addInitializer((instance) => { + instance[context.name + '_' + context.kind + 'Context'] = context; + }); + + if (context.kind === 'getter') { + return function () { + return value.call(this) + 1; + } + } else { + return function (v) { + return value.call(this, v + 1); + } + } +} + +class Foo { + value = 1; + + @dec + get a() { + return this.value; + } + + @dec + set a(v) { + this.value = v; + } + + @dec + get ['b']() { + return this.value; + } + + @dec + set ['b'](v) { + this.value = v; + } +} + +let foo = new Foo(); + +const a_getterContext = foo['a_getterContext']; +const a_setterContext = foo['a_setterContext']; + +const b_getterContext = foo['b_getterContext']; +const b_setterContext = foo['b_setterContext']; + +expect(foo.a).toBe(2); +expect(foo.b).toBe(2); +foo.a = 123; +expect(foo.a).toBe(125); +expect(foo.b).toBe(125); +foo.b = 456; +expect(foo.a).toBe(458); +expect(foo.b).toBe(458); + +expect(a_getterContext.name).toBe('a'); +expect(a_getterContext.kind).toBe('getter'); +expect(a_getterContext.isStatic).toBe(false); +expect(a_getterContext.isPrivate).toBe(false); +expect(typeof a_getterContext.addInitializer).toBe('function'); +expect(typeof a_getterContext.setMetadata).toBe('function'); +expect(typeof a_getterContext.getMetadata).toBe('function'); + +expect(a_setterContext.name).toBe('a'); +expect(a_setterContext.kind).toBe('setter'); +expect(a_setterContext.isStatic).toBe(false); +expect(a_setterContext.isPrivate).toBe(false); +expect(typeof a_setterContext.addInitializer).toBe('function'); +expect(typeof a_setterContext.setMetadata).toBe('function'); +expect(typeof a_setterContext.getMetadata).toBe('function'); + +expect(b_getterContext.name).toBe('b'); +expect(b_getterContext.kind).toBe('getter'); +expect(b_getterContext.isStatic).toBe(false); +expect(b_getterContext.isPrivate).toBe(false); +expect(typeof b_getterContext.addInitializer).toBe('function'); +expect(typeof b_getterContext.setMetadata).toBe('function'); +expect(typeof b_getterContext.getMetadata).toBe('function'); + +expect(b_setterContext.name).toBe('b'); +expect(b_setterContext.kind).toBe('setter'); +expect(b_setterContext.isStatic).toBe(false); +expect(b_setterContext.isPrivate).toBe(false); +expect(typeof b_setterContext.addInitializer).toBe('function'); +expect(typeof b_setterContext.setMetadata).toBe('function'); +expect(typeof b_setterContext.getMetadata).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/public/input.js new file mode 100644 index 000000000000..ef384166d689 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/public/input.js @@ -0,0 +1,23 @@ +class Foo { + value = 1; + + @dec + get a() { + return this.value; + } + + @dec + set a(v) { + this.value = v; + } + + @dec + get ['b']() { + return this.value; + } + + @dec + set ['b'](v) { + this.value = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/public/output.js new file mode 100644 index 000000000000..a05760c5cfe9 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/public/output.js @@ -0,0 +1,37 @@ +var _computedKey, _computedKey2, _dec, _dec2, _dec3, _dec4, _initProto; + +_dec = dec +_dec2 = dec +_computedKey = 'b' +_dec3 = dec +_computedKey2 = 'b' +_dec4 = dec + +class Foo { + constructor(...args) { + babelHelpers.defineProperty(this, "value", 1); + + _initProto(this); + } + + get a() { + return this.value; + } + + set a(v) { + this.value = v; + } + + get [_computedKey]() { + return this.value; + } + + set [_computedKey2](v) { + this.value = v; + } + +} + +(() => { + [_initProto] = babelHelpers.applyDecs(Foo, [[_dec, 3, "a"], [_dec2, 4, "a"], [_dec3, 3, _computedKey], [_dec4, 4, _computedKey2]], []); +})(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-private/exec.js new file mode 100644 index 000000000000..90bdd12c47ec --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-private/exec.js @@ -0,0 +1,65 @@ +function dec(value, context) { + context.addInitializer((instance) => { + instance[context.name + '_' + context.kind + 'Context'] = context; + }); + + if (context.kind === 'getter') { + return function () { + return value.call(this) + 1; + } + } else { + return function (v) { + return value.call(this, v + 1); + } + } +} + +class Foo { + static value = 1; + + @dec + static get #a() { + return this.value; + } + + @dec + static set #a(v) { + this.value = v; + } + + static getA() { + return this.#a; + } + + static setA(v) { + this.#a = v; + } +} + +const a_getterContext = Foo['#a_getterContext']; +const a_setterContext = Foo['#a_setterContext']; + +expect(a_getterContext.access.get.call(Foo)).toBe(2); +expect(Foo.getA()).toBe(2); +a_setterContext.access.set.call(Foo, 123); +expect(a_getterContext.access.get.call(Foo)).toBe(125); +expect(Foo.getA()).toBe(125); +Foo.setA(456); +expect(a_getterContext.access.get.call(Foo)).toBe(458); +expect(Foo.getA()).toBe(458); + +expect(a_getterContext.name).toBe('#a'); +expect(a_getterContext.kind).toBe('getter'); +expect(a_getterContext.isStatic).toBe(true); +expect(a_getterContext.isPrivate).toBe(true); +expect(typeof a_getterContext.addInitializer).toBe('function'); +expect(typeof a_getterContext.setMetadata).toBe('function'); +expect(typeof a_getterContext.getMetadata).toBe('function'); + +expect(a_setterContext.name).toBe('#a'); +expect(a_setterContext.kind).toBe('setter'); +expect(a_setterContext.isStatic).toBe(true); +expect(a_setterContext.isPrivate).toBe(true); +expect(typeof a_setterContext.addInitializer).toBe('function'); +expect(typeof a_setterContext.setMetadata).toBe('function'); +expect(typeof a_setterContext.getMetadata).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-private/input.js new file mode 100644 index 000000000000..f439a9152528 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-private/input.js @@ -0,0 +1,21 @@ +class Foo { + static value = 1; + + @dec + static get #a() { + return this.value; + } + + @dec + static set #a(v) { + this.value = v; + } + + static getA() { + return this.#a; + } + + static setA(v) { + this.#a = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-private/output.js new file mode 100644 index 000000000000..e9ac78c2c966 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-private/output.js @@ -0,0 +1,37 @@ +var _call_a, _call_a2, _initStatic; + +class Foo { + static getA() { + return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _a); + } + + static setA(v) { + babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _a, v); + } + +} + +function _get_a() { + return _call_a(this); +} + +function _set_a(v) { + _call_a2(this, v); +} + +var _a = { + get: _get_a, + set: _set_a +}; + +(() => { + [_call_a, _call_a2, _initStatic] = babelHelpers.applyDecs(Foo, [[dec, 8, "a", function () { + return this.value; + }], [dec, 9, "a", function (v) { + this.value = v; + }]], []); + + _initStatic(Foo); +})(); + +babelHelpers.defineProperty(Foo, "value", 1); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-public/exec.js new file mode 100644 index 000000000000..23ea7b2f54cd --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-public/exec.js @@ -0,0 +1,87 @@ +function dec(value, context) { + context.addInitializer((instance) => { + instance[context.name + '_' + context.kind + 'Context'] = context; + }); + + if (context.kind === 'getter') { + return function () { + return value.call(this) + 1; + } + } else { + return function (v) { + return value.call(this, v + 1); + } + } +} + +class Foo { + static value = 1; + + @dec + static get a() { + return this.value; + } + + @dec + static set a(v) { + this.value = v; + } + + @dec + static get ['b']() { + return this.value; + } + + @dec + static set ['b'](v) { + this.value = v; + } +} + +const a_getterContext = Foo['a_getterContext']; +const a_setterContext = Foo['a_setterContext']; + +const b_getterContext = Foo['b_getterContext']; +const b_setterContext = Foo['b_setterContext']; + +expect(Foo.a).toBe(2); +expect(Foo.b).toBe(2); +Foo.a = 123; +expect(Foo.a).toBe(125); +expect(Foo.b).toBe(125); +Foo.b = 456; +expect(Foo.a).toBe(458); +expect(Foo.b).toBe(458); + +expect(a_getterContext.name).toBe('a'); +expect(a_getterContext.kind).toBe('getter'); +expect(a_getterContext.isStatic).toBe(true); +expect(a_getterContext.isPrivate).toBe(false); +expect(typeof a_getterContext.addInitializer).toBe('function'); +expect(typeof a_getterContext.setMetadata).toBe('function'); +expect(typeof a_getterContext.getMetadata).toBe('function'); + +expect(a_setterContext.name).toBe('a'); +expect(a_setterContext.kind).toBe('setter'); +expect(a_setterContext.isStatic).toBe(true); +expect(a_setterContext.isPrivate).toBe(false); +expect(typeof a_setterContext.addInitializer).toBe('function'); +expect(typeof a_setterContext.setMetadata).toBe('function'); +expect(typeof a_setterContext.getMetadata).toBe('function'); + +expect(b_getterContext.name).toBe('b'); +expect(b_getterContext.kind).toBe('getter'); +expect(b_getterContext.isStatic).toBe(true); +expect(b_getterContext.isPrivate).toBe(false); +expect(typeof b_getterContext.addInitializer).toBe('function'); +expect(typeof b_getterContext.setMetadata).toBe('function'); +expect(typeof b_getterContext.getMetadata).toBe('function'); + +expect(b_setterContext.name).toBe('b'); +expect(b_setterContext.kind).toBe('setter'); +expect(b_setterContext.isStatic).toBe(true); +expect(b_setterContext.isPrivate).toBe(false); +expect(typeof b_setterContext.addInitializer).toBe('function'); +expect(typeof b_setterContext.setMetadata).toBe('function'); +expect(typeof b_setterContext.getMetadata).toBe('function'); + diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-public/input.js new file mode 100644 index 000000000000..193bf7b836a2 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-public/input.js @@ -0,0 +1,23 @@ +class Foo { + static value = 1; + + @dec + static get a() { + return this.value; + } + + @dec + static set a(v) { + this.value = v; + } + + @dec + static get ['b']() { + return this.value; + } + + @dec + static set ['b'](v) { + this.value = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-public/output.js new file mode 100644 index 000000000000..edcb11764a7a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters--to-es2015/static-public/output.js @@ -0,0 +1,35 @@ +var _computedKey, _computedKey2, _dec, _dec2, _dec3, _dec4, _initStatic; + +_dec = dec +_dec2 = dec +_computedKey = 'b' +_dec3 = dec +_computedKey2 = 'b' +_dec4 = dec + +class Foo { + static get a() { + return this.value; + } + + static set a(v) { + this.value = v; + } + + static get [_computedKey]() { + return this.value; + } + + static set [_computedKey2](v) { + this.value = v; + } + +} + +(() => { + [_initStatic] = babelHelpers.applyDecs(Foo, [[_dec, 8, "a"], [_dec2, 9, "a"], [_dec3, 8, _computedKey], [_dec4, 9, _computedKey2]], []); + + _initStatic(Foo); +})(); + +babelHelpers.defineProperty(Foo, "value", 1); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/options.json new file mode 100644 index 000000000000..18aa205580db --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + [ + "proposal-decorators", + { "version": "2021-12", "decoratorsBeforeExport": false } + ] + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/private/input.js new file mode 100644 index 000000000000..060ae225a21e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/private/input.js @@ -0,0 +1,21 @@ +class Foo { + value = 1; + + @dec + get #a() { + return this.value; + } + + @dec + set #a(v) { + this.value = v; + } + + getA() { + return this.#a; + } + + setA(v) { + this.#a = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/private/output.js new file mode 100644 index 000000000000..bcf3cae9176e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/private/output.js @@ -0,0 +1,34 @@ +var _call_a, _call_a2, _initProto; + +class Foo { + static { + [_call_a, _call_a2, _initProto] = babelHelpers.applyDecs(this, [[dec, 3, "a", function () { + return this.value; + }], [dec, 4, "a", function (v) { + this.value = v; + }]], []); + } + + constructor(...args) { + _initProto(this); + } + + value = 1; + + get #a() { + return _call_a(this); + } + + set #a(v) { + _call_a2(this, v); + } + + getA() { + return this.#a; + } + + setA(v) { + this.#a = v; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/public/input.js new file mode 100644 index 000000000000..ef384166d689 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/public/input.js @@ -0,0 +1,23 @@ +class Foo { + value = 1; + + @dec + get a() { + return this.value; + } + + @dec + set a(v) { + this.value = v; + } + + @dec + get ['b']() { + return this.value; + } + + @dec + set ['b'](v) { + this.value = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/public/output.js new file mode 100644 index 000000000000..1a7c8951454d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/public/output.js @@ -0,0 +1,37 @@ +var _computedKey, _computedKey2, _dec, _dec2, _dec3, _dec4, _initProto; + +_dec = dec +_dec2 = dec +_computedKey = 'b' +_dec3 = dec +_computedKey2 = 'b' +_dec4 = dec + +class Foo { + static { + [_initProto] = babelHelpers.applyDecs(this, [[_dec, 3, "a"], [_dec2, 4, "a"], [_dec3, 3, _computedKey], [_dec4, 4, _computedKey2]], []); + } + + constructor(...args) { + _initProto(this); + } + + value = 1; + + get a() { + return this.value; + } + + set a(v) { + this.value = v; + } + + get [_computedKey]() { + return this.value; + } + + set [_computedKey2](v) { + this.value = v; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/static-private/input.js new file mode 100644 index 000000000000..f439a9152528 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/static-private/input.js @@ -0,0 +1,21 @@ +class Foo { + static value = 1; + + @dec + static get #a() { + return this.value; + } + + @dec + static set #a(v) { + this.value = v; + } + + static getA() { + return this.#a; + } + + static setA(v) { + this.#a = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/static-private/output.js new file mode 100644 index 000000000000..e897ea3cd18e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/static-private/output.js @@ -0,0 +1,32 @@ +var _call_a, _call_a2, _initStatic; + +class Foo { + static { + [_call_a, _call_a2, _initStatic] = babelHelpers.applyDecs(this, [[dec, 8, "a", function () { + return this.value; + }], [dec, 9, "a", function (v) { + this.value = v; + }]], []); + + _initStatic(this); + + } + static value = 1; + + static get #a() { + return _call_a(this); + } + + static set #a(v) { + _call_a2(this, v); + } + + static getA() { + return this.#a; + } + + static setA(v) { + this.#a = v; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/static-public/input.js new file mode 100644 index 000000000000..193bf7b836a2 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/static-public/input.js @@ -0,0 +1,23 @@ +class Foo { + static value = 1; + + @dec + static get a() { + return this.value; + } + + @dec + static set a(v) { + this.value = v; + } + + @dec + static get ['b']() { + return this.value; + } + + @dec + static set ['b'](v) { + this.value = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/static-public/output.js new file mode 100644 index 000000000000..367b9d127fd7 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters-and-setters/static-public/output.js @@ -0,0 +1,35 @@ +var _computedKey, _computedKey2, _dec, _dec2, _dec3, _dec4, _initStatic; + +_dec = dec +_dec2 = dec +_computedKey = 'b' +_dec3 = dec +_computedKey2 = 'b' +_dec4 = dec + +class Foo { + static { + [_initStatic] = babelHelpers.applyDecs(this, [[_dec, 8, "a"], [_dec2, 9, "a"], [_dec3, 8, _computedKey], [_dec4, 9, _computedKey2]], []); + + _initStatic(this); + + } + static value = 1; + + static get a() { + return this.value; + } + + static set a(v) { + this.value = v; + } + + static get [_computedKey]() { + return this.value; + } + + static set [_computedKey2](v) { + this.value = v; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/options.json new file mode 100644 index 000000000000..18aa205580db --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + [ + "proposal-decorators", + { "version": "2021-12", "decoratorsBeforeExport": false } + ] + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/private/input.js new file mode 100644 index 000000000000..49c601a1dd5b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/private/input.js @@ -0,0 +1,12 @@ +class Foo { + value = 1; + + @dec + get #a() { + return this.value; + } + + getA() { + return this.#a; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/private/output.js new file mode 100644 index 000000000000..06ba2926bfa4 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/private/output.js @@ -0,0 +1,24 @@ +var _call_a, _initProto; + +class Foo { + static { + [_call_a, _initProto] = babelHelpers.applyDecs(this, [[dec, 3, "a", function () { + return this.value; + }]], []); + } + + constructor(...args) { + _initProto(this); + } + + value = 1; + + get #a() { + return _call_a(this); + } + + getA() { + return this.#a; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/public/input.js new file mode 100644 index 000000000000..9d1cbae52f1d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/public/input.js @@ -0,0 +1,13 @@ +class Foo { + value = 1; + + @dec + get a() { + return this.value; + } + + @dec + get ['b']() { + return this.value; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/public/output.js new file mode 100644 index 000000000000..756adf797e3b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/public/output.js @@ -0,0 +1,26 @@ +var _computedKey, _dec, _dec2, _initProto; + +_dec = dec +_computedKey = 'b' +_dec2 = dec + +class Foo { + static { + [_initProto] = babelHelpers.applyDecs(this, [[_dec, 3, "a"], [_dec2, 3, _computedKey]], []); + } + + constructor(...args) { + _initProto(this); + } + + value = 1; + + get a() { + return this.value; + } + + get [_computedKey]() { + return this.value; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/static-private/input.js new file mode 100644 index 000000000000..90edd26364d8 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/static-private/input.js @@ -0,0 +1,12 @@ +class Foo { + static value = 1; + + @dec + static get #a() { + return this.value; + } + + static getA() { + return this.#a; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/static-private/output.js new file mode 100644 index 000000000000..dc93a5313712 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/static-private/output.js @@ -0,0 +1,22 @@ +var _call_a, _initStatic; + +class Foo { + static { + [_call_a, _initStatic] = babelHelpers.applyDecs(this, [[dec, 8, "a", function () { + return this.value; + }]], []); + + _initStatic(this); + + } + static value = 1; + + static get #a() { + return _call_a(this); + } + + static getA() { + return this.#a; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/static-public/input.js new file mode 100644 index 000000000000..1383fa58abf4 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/static-public/input.js @@ -0,0 +1,13 @@ +class Foo { + static value = 1; + + @dec + static get a() { + return this.value; + } + + @dec + static get ['b']() { + return this.value; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/static-public/output.js new file mode 100644 index 000000000000..2ec6f560dc4b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-getters/static-public/output.js @@ -0,0 +1,24 @@ +var _computedKey, _dec, _dec2, _initStatic; + +_dec = dec +_computedKey = 'b' +_dec2 = dec + +class Foo { + static { + [_initStatic] = babelHelpers.applyDecs(this, [[_dec, 8, "a"], [_dec2, 8, _computedKey]], []); + + _initStatic(this); + + } + static value = 1; + + static get a() { + return this.value; + } + + static get [_computedKey]() { + return this.value; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/class/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/class/exec.js new file mode 100644 index 000000000000..110e14700092 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/class/exec.js @@ -0,0 +1,10 @@ +const key = Symbol(); + +function dec(_, { setMetadata }) { + setMetadata(key, 123); +} + +@dec +class Foo {} + +expect(Foo[Symbol.for("Symbol.metadata")][key].constructor).toBe(123); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/getting-previously-set-metadata/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/getting-previously-set-metadata/exec.js new file mode 100644 index 000000000000..91d04b4f0df5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/getting-previously-set-metadata/exec.js @@ -0,0 +1,30 @@ +const key = Symbol(); + +function dec1(_, { setMetadata, getMetadata }) { + expect(getMetadata(key)).toBe(undefined); + setMetadata(key, 123); + expect(getMetadata(key)).toBe(123); +} + +function dec2(_, { setMetadata, getMetadata }) { + expect(getMetadata(key)).toBe(123); + setMetadata(key, 456); + expect(getMetadata(key)).toBe(456); +} + +class Foo { + @dec1 @dec2 a; +} + +function dec3(_, { setMetadata, getMetadata }) { + expect(getMetadata(key)).toBe(undefined); + setMetadata(key, 789); + expect(getMetadata(key)).toBe(789); +} + +class Bar extends Foo { + @dec3 a; +} + +expect(Foo.prototype[Symbol.for("Symbol.metadata")][key].public.a).toBe(456); +expect(Bar.prototype[Symbol.for("Symbol.metadata")][key].public.a).toBe(789); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/inheritance-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/inheritance-private/exec.js new file mode 100644 index 000000000000..de49e14701f3 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/inheritance-private/exec.js @@ -0,0 +1,16 @@ +const key = Symbol(); + +function dec(_, { setMetadata }) { + setMetadata(key, 123); +} + +class Foo { + @dec #a; +} + +class Bar extends Foo { + @dec #b; +} + +expect(Foo.prototype[Symbol.for("Symbol.metadata")][key].private).toEqual([123]); +expect(Bar.prototype[Symbol.for("Symbol.metadata")][key].private).toEqual([123, 123]); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/inheritance-public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/inheritance-public/exec.js new file mode 100644 index 000000000000..8bbd6808c028 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/inheritance-public/exec.js @@ -0,0 +1,18 @@ +const key = Symbol(); + +function dec(_, { setMetadata }) { + setMetadata(key, 123); +} + +class Foo { + @dec a; +} + +class Bar extends Foo { + @dec b; +} + +expect(Foo.prototype[Symbol.for("Symbol.metadata")][key].public.a).toEqual(123); +expect(Foo.prototype[Symbol.for("Symbol.metadata")][key].public.b).toEqual(undefined); +expect(Bar.prototype[Symbol.for("Symbol.metadata")][key].public.a).toEqual(123); +expect(Bar.prototype[Symbol.for("Symbol.metadata")][key].public.b).toEqual(123); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/non-symbol-keys/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/non-symbol-keys/exec.js new file mode 100644 index 000000000000..a6c8ae1af6f8 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/non-symbol-keys/exec.js @@ -0,0 +1,10 @@ +expect(() => { + const key = 'test'; + + function dec(_, { setMetadata }) { + setMetadata(key, 123); + } + + @dec + class Foo {} +}).toThrow('Metadata keys must be symbols, received: test') diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/options.json new file mode 100644 index 000000000000..69f3e599cf2d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "proposal-decorators", + { "version": "2021-12", "decoratorsBeforeExport": false } + ], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/proto-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/proto-private/exec.js new file mode 100644 index 000000000000..87ca2369944f --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/proto-private/exec.js @@ -0,0 +1,11 @@ +const key = Symbol(); + +function dec(_, { setMetadata }) { + setMetadata(key, 123); +} + +class Foo { + @dec #a; +} + +expect(Foo.prototype[Symbol.for("Symbol.metadata")][key].private[0]).toBe(123); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/proto-public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/proto-public/exec.js new file mode 100644 index 000000000000..c09dab0c4935 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/proto-public/exec.js @@ -0,0 +1,11 @@ +const key = Symbol(); + +function dec(_, { setMetadata }) { + setMetadata(key, 123); +} + +class Foo { + @dec a; +} + +expect(Foo.prototype[Symbol.for("Symbol.metadata")][key].public.a).toBe(123); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/static-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/static-private/exec.js new file mode 100644 index 000000000000..d941ef10e5bb --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/static-private/exec.js @@ -0,0 +1,11 @@ +const key = Symbol(); + +function dec(_, { setMetadata }) { + setMetadata(key, 123); +} + +class Foo { + @dec static #a; +} + +expect(Foo[Symbol.for("Symbol.metadata")][key].private[0]).toBe(123); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/static-public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/static-public/exec.js new file mode 100644 index 000000000000..649592a40010 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-metadata--to-es2015/static-public/exec.js @@ -0,0 +1,11 @@ +const key = Symbol(); + +function dec(_, { setMetadata }) { + setMetadata(key, 123); +} + +class Foo { + @dec static a; +} + +expect(Foo[Symbol.for("Symbol.metadata")][key].public.a).toBe(123); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/options.json new file mode 100644 index 000000000000..69f3e599cf2d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "proposal-decorators", + { "version": "2021-12", "decoratorsBeforeExport": false } + ], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/private/exec.js new file mode 100644 index 000000000000..5a3ce1785ff0 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/private/exec.js @@ -0,0 +1,41 @@ +function dec(fn, context) { + context.addInitializer((instance) => { + instance[context.name + 'Context'] = context; + }); + + return function () { + return fn.call(this) + 1; + } +} + +class Foo { + value = 1; + + @dec + #a() { + return this.value; + } + + callA() { + return this.#a(); + } +} + +let foo = new Foo(); + +const aContext = foo['#aContext']; + +// First call gets the method, second call calls the method with correct `this` +expect(aContext.access.get.call(foo).call(foo)).toBe(2); +expect(foo.callA()).toBe(2); +foo.value = 123; +expect(aContext.access.get.call(foo).call(foo)).toBe(124); +expect(foo.callA()).toBe(124); + +expect(aContext.name).toBe('#a'); +expect(aContext.kind).toBe('method'); +expect(aContext.isStatic).toBe(false); +expect(aContext.isPrivate).toBe(true); +expect(typeof aContext.addInitializer).toBe('function'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/private/input.js new file mode 100644 index 000000000000..fc7ace8128ee --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/private/input.js @@ -0,0 +1,12 @@ +class Foo { + value = 1; + + @dec + #a() { + return this.value; + } + + callA() { + return this.#a(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/private/output.js new file mode 100644 index 000000000000..4e18191445f1 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/private/output.js @@ -0,0 +1,26 @@ +var _call_a, _initProto; + +var _a = /*#__PURE__*/new WeakMap(); + +class Foo { + constructor(...args) { + babelHelpers.classPrivateFieldInitSpec(this, _a, { + writable: true, + value: _call_a + }); + babelHelpers.defineProperty(this, "value", 1); + + _initProto(this); + } + + callA() { + return babelHelpers.classPrivateFieldGet(this, _a).call(this); + } + +} + +(() => { + [_call_a, _initProto] = babelHelpers.applyDecs(Foo, [[dec, 2, "a", function () { + return this.value; + }]], []); +})(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/public/exec.js new file mode 100644 index 000000000000..0f27b61caa4d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/public/exec.js @@ -0,0 +1,50 @@ +function dec(fn, context) { + context.addInitializer((instance) => { + instance[context.name + 'Context'] = context; + }); + + return function () { + return fn.call(this) + 1; + } +} + +class Foo { + value = 1; + + @dec + a() { + return this.value; + } + + @dec + ['b']() { + return this.value; + } +} + +let foo = new Foo(); + +const aContext = foo['aContext']; +const bContext = foo['bContext']; + +expect(foo.a()).toBe(2); +expect(foo.b()).toBe(2); +foo.value = 123; +expect(foo.a()).toBe(124); +expect(foo.b()).toBe(124); + +expect(aContext.name).toBe('a'); +expect(aContext.kind).toBe('method'); +expect(aContext.isStatic).toBe(false); +expect(aContext.isPrivate).toBe(false); +expect(typeof aContext.addInitializer).toBe('function'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); + +expect(bContext.name).toBe('b'); +expect(bContext.kind).toBe('method'); +expect(bContext.isStatic).toBe(false); +expect(bContext.isPrivate).toBe(false); +expect(typeof bContext.addInitializer).toBe('function'); +expect(typeof bContext.setMetadata).toBe('function'); +expect(typeof bContext.getMetadata).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/public/input.js new file mode 100644 index 000000000000..f364442146ea --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/public/input.js @@ -0,0 +1,13 @@ +class Foo { + value = 1; + + @dec + a() { + return this.value; + } + + @dec + ['b']() { + return this.value; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/public/output.js new file mode 100644 index 000000000000..1835a9fa3548 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/public/output.js @@ -0,0 +1,26 @@ +var _computedKey, _dec, _dec2, _initProto; + +_dec = dec +_computedKey = 'b' +_dec2 = dec + +class Foo { + constructor(...args) { + babelHelpers.defineProperty(this, "value", 1); + + _initProto(this); + } + + a() { + return this.value; + } + + [_computedKey]() { + return this.value; + } + +} + +(() => { + [_initProto] = babelHelpers.applyDecs(Foo, [[_dec, 2, "a"], [_dec2, 2, _computedKey]], []); +})(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-private/exec.js new file mode 100644 index 000000000000..bab58275b48a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-private/exec.js @@ -0,0 +1,39 @@ +function dec(fn, context) { + context.addInitializer((instance) => { + instance[context.name + 'Context'] = context; + }); + + return function () { + return fn.call(this) + 1; + } +} + +class Foo { + static value = 1; + + @dec + static #a() { + return this.value; + } + + static callA() { + return this.#a(); + } +} + +const aContext = Foo['#aContext']; + +// First call gets the method, second call calls the method with correct `this` +expect(aContext.access.get.call(Foo).call(Foo)).toBe(2); +expect(Foo.callA()).toBe(2); +Foo.value = 123; +expect(aContext.access.get.call(Foo).call(Foo)).toBe(124); +expect(Foo.callA()).toBe(124); + +expect(aContext.name).toBe('#a'); +expect(aContext.kind).toBe('method'); +expect(aContext.isStatic).toBe(true); +expect(aContext.isPrivate).toBe(true); +expect(typeof aContext.addInitializer).toBe('function'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-private/input.js new file mode 100644 index 000000000000..e68191455f14 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-private/input.js @@ -0,0 +1,12 @@ +class Foo { + static value = 1; + + @dec + static #a() { + return this.value; + } + + static callA() { + return this.#a(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-private/output.js new file mode 100644 index 000000000000..3c147c920d64 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-private/output.js @@ -0,0 +1,22 @@ +var _call_a, _initStatic; + +class Foo { + static callA() { + return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _a).call(this); + } + +} + +(() => { + [_call_a, _initStatic] = babelHelpers.applyDecs(Foo, [[dec, 7, "a", function () { + return this.value; + }]], []); + + _initStatic(Foo); +})(); + +var _a = { + writable: true, + value: _call_a +}; +babelHelpers.defineProperty(Foo, "value", 1); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-public/exec.js new file mode 100644 index 000000000000..e68458db59d9 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-public/exec.js @@ -0,0 +1,48 @@ +function dec(fn, context) { + context.addInitializer((instance) => { + instance[context.name + 'Context'] = context; + }); + + return function () { + return fn.call(this) + 1; + } +} + +class Foo { + static value = 1; + + @dec + static a() { + return this.value; + } + + @dec + static ['b']() { + return this.value; + } +} + +const aContext = Foo['aContext']; +const bContext = Foo['bContext']; + +expect(Foo.a()).toBe(2); +expect(Foo.b()).toBe(2); +Foo.value = 123; +expect(Foo.a()).toBe(124); +expect(Foo.b()).toBe(124); + +expect(aContext.name).toBe('a'); +expect(aContext.kind).toBe('method'); +expect(aContext.isStatic).toBe(true); +expect(aContext.isPrivate).toBe(false); +expect(typeof aContext.addInitializer).toBe('function'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); + +expect(bContext.name).toBe('b'); +expect(bContext.kind).toBe('method'); +expect(bContext.isStatic).toBe(true); +expect(bContext.isPrivate).toBe(false); +expect(typeof bContext.addInitializer).toBe('function'); +expect(typeof bContext.setMetadata).toBe('function'); +expect(typeof bContext.getMetadata).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-public/input.js new file mode 100644 index 000000000000..77dae8711cd3 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-public/input.js @@ -0,0 +1,13 @@ +class Foo { + static value = 1; + + @dec + static a() { + return this.value; + } + + @dec + static ['b']() { + return this.value; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-public/output.js new file mode 100644 index 000000000000..d1c334def59e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods--to-es2015/static-public/output.js @@ -0,0 +1,24 @@ +var _computedKey, _dec, _dec2, _initStatic; + +_dec = dec +_computedKey = 'b' +_dec2 = dec + +class Foo { + static a() { + return this.value; + } + + static [_computedKey]() { + return this.value; + } + +} + +(() => { + [_initStatic] = babelHelpers.applyDecs(Foo, [[_dec, 7, "a"], [_dec2, 7, _computedKey]], []); + + _initStatic(Foo); +})(); + +babelHelpers.defineProperty(Foo, "value", 1); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/options.json new file mode 100644 index 000000000000..18aa205580db --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + [ + "proposal-decorators", + { "version": "2021-12", "decoratorsBeforeExport": false } + ] + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/private/input.js new file mode 100644 index 000000000000..fc7ace8128ee --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/private/input.js @@ -0,0 +1,12 @@ +class Foo { + value = 1; + + @dec + #a() { + return this.value; + } + + callA() { + return this.#a(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/private/output.js new file mode 100644 index 000000000000..6f76b6021b6c --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/private/output.js @@ -0,0 +1,21 @@ +var _call_a, _initProto; + +class Foo { + static { + [_call_a, _initProto] = babelHelpers.applyDecs(this, [[dec, 2, "a", function () { + return this.value; + }]], []); + } + + constructor(...args) { + _initProto(this); + } + + #a = _call_a; + value = 1; + + callA() { + return this.#a(); + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/public/input.js new file mode 100644 index 000000000000..f364442146ea --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/public/input.js @@ -0,0 +1,13 @@ +class Foo { + value = 1; + + @dec + a() { + return this.value; + } + + @dec + ['b']() { + return this.value; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/public/output.js new file mode 100644 index 000000000000..efac40c1c35e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/public/output.js @@ -0,0 +1,26 @@ +var _computedKey, _dec, _dec2, _initProto; + +_dec = dec +_computedKey = 'b' +_dec2 = dec + +class Foo { + static { + [_initProto] = babelHelpers.applyDecs(this, [[_dec, 2, "a"], [_dec2, 2, _computedKey]], []); + } + + constructor(...args) { + _initProto(this); + } + + value = 1; + + a() { + return this.value; + } + + [_computedKey]() { + return this.value; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/static-private/input.js new file mode 100644 index 000000000000..e68191455f14 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/static-private/input.js @@ -0,0 +1,12 @@ +class Foo { + static value = 1; + + @dec + static #a() { + return this.value; + } + + static callA() { + return this.#a(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/static-private/output.js new file mode 100644 index 000000000000..6b807803efa6 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/static-private/output.js @@ -0,0 +1,19 @@ +var _call_a, _initStatic; + +class Foo { + static { + [_call_a, _initStatic] = babelHelpers.applyDecs(this, [[dec, 7, "a", function () { + return this.value; + }]], []); + + _initStatic(this); + + } + static #a = _call_a; + static value = 1; + + static callA() { + return this.#a(); + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/static-public/input.js new file mode 100644 index 000000000000..77dae8711cd3 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/static-public/input.js @@ -0,0 +1,13 @@ +class Foo { + static value = 1; + + @dec + static a() { + return this.value; + } + + @dec + static ['b']() { + return this.value; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/static-public/output.js new file mode 100644 index 000000000000..1ad76b111fb4 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-methods/static-public/output.js @@ -0,0 +1,24 @@ +var _computedKey, _dec, _dec2, _initStatic; + +_dec = dec +_computedKey = 'b' +_dec2 = dec + +class Foo { + static { + [_initStatic] = babelHelpers.applyDecs(this, [[_dec, 7, "a"], [_dec2, 7, _computedKey]], []); + + _initStatic(this); + + } + static value = 1; + + static a() { + return this.value; + } + + static [_computedKey]() { + return this.value; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/initProto-existing-derived-constructor/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/initProto-existing-derived-constructor/exec.js new file mode 100644 index 000000000000..3d81b8cdf452 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/initProto-existing-derived-constructor/exec.js @@ -0,0 +1,28 @@ +let self, a, initCalled; + +function deco(_, context) { + context.addInitializer(() => { + initCalled = true; + }) +} + +class B { + constructor(s) { + a = s; + } +} + +class A extends B { + constructor() { + let a = 2; + self = super(a); + } + + @deco + method() {} +} + +let instance = new A(); +expect(self).toBe(instance); +expect(a).toBe(2); +expect(initCalled).toBe(true); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/initProto-existing-derived-constructor/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/initProto-existing-derived-constructor/input.js new file mode 100644 index 000000000000..d72d66fd2a29 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/initProto-existing-derived-constructor/input.js @@ -0,0 +1,10 @@ +class A extends B { + constructor() { + let a = 2; + super(a); + foo(); + } + + @deco + method() {} +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/initProto-existing-derived-constructor/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/initProto-existing-derived-constructor/output.js new file mode 100644 index 000000000000..61a1fde840db --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/initProto-existing-derived-constructor/output.js @@ -0,0 +1,18 @@ +var _initProto; + +class A extends B { + constructor() { + let a = 2; + + _initProto(super(a)); + + foo(); + } + + method() {} + +} + +(() => { + [_initProto] = babelHelpers.applyDecs(A, [[deco, 2, "method"]], []); +})(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/initializer-timing/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/initializer-timing/exec.js new file mode 100644 index 000000000000..ac5920dcd16a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/initializer-timing/exec.js @@ -0,0 +1,34 @@ +function dec1(fn, context) { + context.addInitializer((instance) => { + expect(instance.value).toBe(undefined); + }); + + return fn; +} + +class Foo { + value = 1; + + @dec1 + foo() {} +} + +function dec2(fn, context) { + context.addInitializer((instance) => { + expect(instance.value).toBe(1); + }); + + return fn; +} + + +class Bar extends Foo { + constructor() { + super(); + + this.value = 2; + } + + @dec2 + bar() {} +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/options.json new file mode 100644 index 000000000000..69f3e599cf2d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "proposal-decorators", + { "version": "2021-12", "decoratorsBeforeExport": false } + ], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/valid-expression-formats/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/valid-expression-formats/input.js new file mode 100644 index 000000000000..844a924e3e80 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/valid-expression-formats/input.js @@ -0,0 +1,22 @@ +@dec +@call() +@chain.expr() +@(arbitrary + expr) +@(array[expr]) +class Foo { + #a; + + @dec + @call() + @chain.expr() + @(arbitrary + expr) + @(array[expr]) + method() {} + + makeClass() { + return class Nested { + @(this.#a) + bar; + } + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/valid-expression-formats/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/valid-expression-formats/output.js new file mode 100644 index 000000000000..79b845170bc7 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/valid-expression-formats/output.js @@ -0,0 +1,40 @@ +var _initClass, _initProto; + +let _Foo; + +var _a = /*#__PURE__*/new WeakMap(); + +class Foo { + constructor(...args) { + babelHelpers.classPrivateFieldInitSpec(this, _a, { + writable: true, + value: void 0 + }); + + _initProto(this); + } + + method() {} + + makeClass() { + var _init_bar, _class, _temp; + + return _temp = _class = class Nested { + constructor() { + babelHelpers.defineProperty(this, "bar", _init_bar(this)); + } + + }, (() => { + [_init_bar] = babelHelpers.applyDecs(_class, [[babelHelpers.classPrivateFieldGet(_class, _a), 0, "bar"]], []); + })(), _temp; + } + +} + +(() => { + [_Foo, _initClass, _initProto] = babelHelpers.applyDecs(Foo, [[[dec, call(), chain.expr(), arbitrary + expr, array[expr]], 2, "method"]], [dec, call(), chain.expr(), arbitrary + expr, array[expr]]); +})(); + +(() => { + _initClass(); +})(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/all-decorators/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/all-decorators/input.js new file mode 100644 index 000000000000..e57aa75977ea --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/all-decorators/input.js @@ -0,0 +1,26 @@ +@dec +class Class { + @dec a; + @dec b() {} + @dec get c() {} + @dec set c(v) {} + @dec accessor d; + + @dec #e; + @dec #f() {} + @dec get #g() {} + @dec set #g(v) {} + @dec accessor #h; + + @dec static i; + @dec static j() {} + @dec static get k() {} + @dec static set l(v) {} + @dec static accessor m; + + @dec static #n; + @dec static #o() {} + @dec static get #p() {} + @dec static set #q(v) {} + @dec static accessor #r; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/all-decorators/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/all-decorators/output.js new file mode 100644 index 000000000000..d13052811623 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/all-decorators/output.js @@ -0,0 +1,113 @@ +var _initClass, _init_a, _init_d, _init_e, _call_f, _call_g, _call_g2, _init_h, _get_h, _set_h, _init_i, _init_m, _init_n, _call_o, _call_p, _call_q, _init_r, _get_r, _set_r, _initProto, _initStatic; + +let _Class; + +new class extends babelHelpers.identity { + static { + class Class { + static { + [_init_a, _init_d, _init_e, _call_f, _call_g, _call_g2, _init_h, _get_h, _set_h, _init_i, _init_m, _init_n, _call_o, _call_p, _call_q, _init_r, _get_r, _set_r, _Class, _initClass, _initProto, _initStatic] = babelHelpers.applyDecs(this, [[dec, 0, "a"], [dec, 2, "b"], [dec, 3, "c"], [dec, 4, "c"], [dec, 1, "d"], [dec, 0, "e", function () { + return this.#e; + }, function (value) { + this.#e = value; + }], [dec, 2, "f", function () {}], [dec, 3, "g", function () {}], [dec, 4, "g", function (v) {}], [dec, 1, "h", function () { + return this.#B; + }, function (value) { + this.#B = value; + }], [dec, 5, "i"], [dec, 7, "j"], [dec, 8, "k"], [dec, 9, "l"], [dec, 6, "m"], [dec, 5, "n", function () { + return this.#n; + }, function (value) { + this.#n = value; + }], [dec, 7, "o", function () {}], [dec, 8, "p", function () {}], [dec, 9, "q", function (v) {}], [dec, 6, "r", function () { + return this.#D; + }, function (value) { + this.#D = value; + }]], [dec]); + + _initStatic(this); + + } + #f = _call_f; + a = (_initProto(this), _init_a(this)); + + b() {} + + get c() {} + + set c(v) {} + + #A = _init_d(this); + + get d() { + return this.#A; + } + + set d(v) { + this.#A = v; + } + + #e = _init_e(this); + + get #g() { + return _call_g(this); + } + + set #g(v) { + _call_g2(this, v); + } + + #B = _init_h(this); + + set #h(v) { + _set_h(this, v); + } + + get #h() { + _get_h(this); + } + + static j() {} + + static get k() {} + + static set l(v) {} + + static get m() { + return this.#C; + } + + static set m(v) { + this.#C = v; + } + + set #r(v) { + _set_r(this, v); + } + + get #r() { + _get_r(this); + } + + } + + } + #o = _call_o; + i = _init_i(this); + #C = _init_m(this); + #n = _init_n(this); + + get #p() { + return _call_p(this); + } + + set #q(v) { + _call_q(this, v); + } + + #D = _init_r(this); + + constructor() { + super(_Class), _initClass(); + } + +}(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor-multiple-super/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor-multiple-super/input.js new file mode 100644 index 000000000000..6b0bc3b5a233 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor-multiple-super/input.js @@ -0,0 +1,23 @@ +class A extends B { + constructor() { + if (Math.random() > 0.5) { + super(true); + } else { + super(false); + } + } + + @deco + method() {} +} + +class C extends B { + constructor() { + try { + super(super(), null.x); + } catch {} + } + + @deco + method() {} +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor-multiple-super/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor-multiple-super/output.js new file mode 100644 index 000000000000..8a52cbd4b25a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor-multiple-super/output.js @@ -0,0 +1,33 @@ +var _initProto, _initProto2; + +class A extends B { + static { + [_initProto] = babelHelpers.applyDecs(this, [[deco, 2, "method"]], []); + } + + constructor() { + if (Math.random() > 0.5) { + _initProto(super(true)); + } else { + _initProto(super(false)); + } + } + + method() {} + +} + +class C extends B { + static { + [_initProto2] = babelHelpers.applyDecs(this, [[deco, 2, "method"]], []); + } + + constructor() { + try { + _initProto2(super(_initProto2(super()), null.x)); + } catch {} + } + + method() {} + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor/input.js new file mode 100644 index 000000000000..d72d66fd2a29 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor/input.js @@ -0,0 +1,10 @@ +class A extends B { + constructor() { + let a = 2; + super(a); + foo(); + } + + @deco + method() {} +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor/output.js new file mode 100644 index 000000000000..e2fb9d7eb331 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/initProto-existing-derived-constructor/output.js @@ -0,0 +1,18 @@ +var _initProto; + +class A extends B { + static { + [_initProto] = babelHelpers.applyDecs(this, [[deco, 2, "method"]], []); + } + + constructor() { + let a = 2; + + _initProto(super(a)); + + foo(); + } + + method() {} + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/options.json new file mode 100644 index 000000000000..18aa205580db --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + [ + "proposal-decorators", + { "version": "2021-12", "decoratorsBeforeExport": false } + ] + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/private-keys-in-enclosing-class/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/private-keys-in-enclosing-class/input.js new file mode 100644 index 000000000000..586fb0adef1e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/private-keys-in-enclosing-class/input.js @@ -0,0 +1,10 @@ +class A { + #A = 1; + static B = class B extends A { + accessor a = 2; + + getA() { + return this.#A; + } + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/private-keys-in-enclosing-class/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/private-keys-in-enclosing-class/output.js new file mode 100644 index 000000000000..102e9e463973 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/private-keys-in-enclosing-class/output.js @@ -0,0 +1,19 @@ +class A { + #A = 1; + static B = class B extends A { + #B = 2; + + get a() { + return this.#B; + } + + set a(v) { + this.#B = v; + } + + getA() { + return this.#A; + } + + }; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-array-pattern/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-array-pattern/input.js new file mode 100644 index 000000000000..ff2a3c43e415 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-array-pattern/input.js @@ -0,0 +1,7 @@ +class Foo { + @dec #x() {} + + bar() { + ([this.#x] = this.baz); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-array-pattern/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-array-pattern/options.json new file mode 100644 index 000000000000..188eae592ec5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-array-pattern/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Decorated private methods are not updatable, but \"#x\" is updated via this expression." +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-for-of/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-for-of/input.js new file mode 100644 index 000000000000..34df0b29ed8b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-for-of/input.js @@ -0,0 +1,7 @@ +class Foo { + @dec #x() {} + + bar() { + for (this.#x of this.baz); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-for-of/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-for-of/options.json new file mode 100644 index 000000000000..188eae592ec5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-for-of/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Decorated private methods are not updatable, but \"#x\" is updated via this expression." +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-object-pattern/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-object-pattern/input.js new file mode 100644 index 000000000000..56e2501e42e1 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-object-pattern/input.js @@ -0,0 +1,7 @@ +class Foo { + @dec #x() {} + + bar() { + ({ x: this.#x } = this.baz); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-object-pattern/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-object-pattern/options.json new file mode 100644 index 000000000000..188eae592ec5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-object-pattern/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Decorated private methods are not updatable, but \"#x\" is updated via this expression." +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-rest/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-rest/input.js new file mode 100644 index 000000000000..c24cac0560ad --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-rest/input.js @@ -0,0 +1,7 @@ +class Foo { + @dec #x() {} + + bar() { + ([...this.#x] = this.baz); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-rest/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-rest/options.json new file mode 100644 index 000000000000..188eae592ec5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-rest/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Decorated private methods are not updatable, but \"#x\" is updated via this expression." +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-update/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-update/input.js new file mode 100644 index 000000000000..f3b235ffe0df --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-update/input.js @@ -0,0 +1,7 @@ +class Foo { + @dec #x() {} + + bar() { + this.#x++; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-update/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-update/options.json new file mode 100644 index 000000000000..188eae592ec5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method-via-update/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Decorated private methods are not updatable, but \"#x\" is updated via this expression." +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method/input.js new file mode 100644 index 000000000000..46f4ecf6453b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method/input.js @@ -0,0 +1,7 @@ +class Foo { + @dec #x() {} + + bar() { + this.#x = 123; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method/options.json new file mode 100644 index 000000000000..188eae592ec5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/setting-private-method/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Decorated private methods are not updatable, but \"#x\" is updated via this expression." +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-nested-constructor-expression/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-nested-constructor-expression/input.js new file mode 100644 index 000000000000..ebd79d22da06 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-nested-constructor-expression/input.js @@ -0,0 +1,6 @@ +@dec +class Foo extends Bar { + constructor() { + let foo = super(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-nested-constructor-expression/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-nested-constructor-expression/output.js new file mode 100644 index 000000000000..dcf5a0fae917 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-nested-constructor-expression/output.js @@ -0,0 +1,18 @@ +var _initClass; + +let _Foo; + +class Foo extends Bar { + static { + [_Foo, _initClass] = babelHelpers.applyDecs(this, [], [dec]); + } + + constructor() { + let foo = super(); + } + + static { + _initClass(); + + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-private-accessor/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-private-accessor/input.js new file mode 100644 index 000000000000..4be504d54e70 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-private-accessor/input.js @@ -0,0 +1,6 @@ +class Foo extends Bar { + @dec + get #x() { + return super.foo(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-private-accessor/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-private-accessor/output.js new file mode 100644 index 000000000000..dea033cc9d65 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-private-accessor/output.js @@ -0,0 +1,20 @@ +var _call_x, _initProto; + +class Foo extends Bar { + static { + [_call_x, _initProto] = babelHelpers.applyDecs(this, [[dec, 3, "x", function () { + return babelHelpers.get(babelHelpers.getPrototypeOf(Foo), "foo", this).call(this); + }]], []); + } + + constructor(...args) { + super(...args); + + _initProto(this); + } + + get #x() { + return _call_x(this); + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-private-method/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-private-method/input.js new file mode 100644 index 000000000000..73200f5f673d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-private-method/input.js @@ -0,0 +1,6 @@ +class Foo extends Bar { + @dec + #x() { + return super.foo(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-private-method/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-private-method/output.js new file mode 100644 index 000000000000..b77541b7c254 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/super-in-private-method/output.js @@ -0,0 +1,17 @@ +var _call_x, _initProto; + +class Foo extends Bar { + static { + [_call_x, _initProto] = babelHelpers.applyDecs(this, [[dec, 2, "x", function () { + return babelHelpers.get(babelHelpers.getPrototypeOf(Foo), "foo", this).call(this); + }]], []); + } + + constructor(...args) { + super(...args); + + _initProto(this); + } + + #x = _call_x; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/valid-expression-formats/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/valid-expression-formats/input.js new file mode 100644 index 000000000000..844a924e3e80 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/valid-expression-formats/input.js @@ -0,0 +1,22 @@ +@dec +@call() +@chain.expr() +@(arbitrary + expr) +@(array[expr]) +class Foo { + #a; + + @dec + @call() + @chain.expr() + @(arbitrary + expr) + @(array[expr]) + method() {} + + makeClass() { + return class Nested { + @(this.#a) + bar; + } + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/valid-expression-formats/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/valid-expression-formats/output.js new file mode 100644 index 000000000000..5490137a9009 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/valid-expression-formats/output.js @@ -0,0 +1,33 @@ +var _initClass, _initProto; + +let _Foo; + +class Foo { + static { + [_Foo, _initClass, _initProto] = babelHelpers.applyDecs(this, [[[dec, call(), chain.expr(), arbitrary + expr, array[expr]], 2, "method"]], [dec, call(), chain.expr(), arbitrary + expr, array[expr]]); + } + + constructor(...args) { + _initProto(this); + } + + #a; + + method() {} + + makeClass() { + var _init_bar; + + return class Nested { + static { + [_init_bar] = babelHelpers.applyDecs(this, [[this.#a, 0, "bar"]], []); + } + bar = _init_bar(this); + }; + } + + static { + _initClass(); + + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/options.json new file mode 100644 index 000000000000..69f3e599cf2d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/options.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "proposal-decorators", + { "version": "2021-12", "decoratorsBeforeExport": false } + ], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/private/exec.js new file mode 100644 index 000000000000..c2768a0a2c67 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/private/exec.js @@ -0,0 +1,40 @@ +function dec(set, context) { + context.addInitializer((instance) => { + instance[context.name + 'Context'] = context; + }); + + return function (v) { + return set.call(this, v + 1); + } +} + +class Foo { + value = 1; + + @dec + set #a(v) { + return this.value = v; + } + + setA(v) { + this.#a = v; + } +} + +let foo = new Foo(); + +const aContext = foo['#aContext']; + +expect(foo.value).toBe(1); +aContext.access.set.call(foo, 123); +expect(foo.value).toBe(124); +foo.setA(456); +expect(foo.value).toBe(457); + +expect(aContext.name).toBe('#a'); +expect(aContext.kind).toBe('setter'); +expect(aContext.isStatic).toBe(false); +expect(aContext.isPrivate).toBe(true); +expect(typeof aContext.addInitializer).toBe('function'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/private/input.js new file mode 100644 index 000000000000..254f2f102f9a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/private/input.js @@ -0,0 +1,12 @@ +class Foo { + value = 1; + + @dec + set #a(v) { + return this.value = v; + } + + setA(v) { + this.#a = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/private/output.js new file mode 100644 index 000000000000..5d0ad516b80b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/private/output.js @@ -0,0 +1,30 @@ +var _call_a, _initProto; + +var _a = /*#__PURE__*/new WeakMap(); + +class Foo { + constructor(...args) { + babelHelpers.classPrivateFieldInitSpec(this, _a, { + get: void 0, + set: _set_a + }); + babelHelpers.defineProperty(this, "value", 1); + + _initProto(this); + } + + setA(v) { + babelHelpers.classPrivateFieldSet(this, _a, v); + } + +} + +function _set_a(v) { + _call_a(this, v); +} + +(() => { + [_call_a, _initProto] = babelHelpers.applyDecs(Foo, [[dec, 4, "a", function (v) { + return this.value = v; + }]], []); +})(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/public/exec.js new file mode 100644 index 000000000000..ab2f98534b95 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/public/exec.js @@ -0,0 +1,50 @@ +function dec(set, context) { + context.addInitializer((instance) => { + instance[context.name + 'Context'] = context; + }); + + return function (v) { + return set.call(this, v + 1); + } +} + +class Foo { + value = 1; + + @dec + set a(v) { + return this.value = v; + } + + @dec + set ['b'](v) { + return this.value = v; + } +} + +let foo = new Foo(); + +const aContext = foo['aContext']; +const bContext = foo['bContext']; + +expect(foo.value).toBe(1); +foo.a = 123; +expect(foo.value).toBe(124); +foo.a = 456; +expect(foo.value).toBe(457); + +expect(aContext.name).toBe('a'); +expect(aContext.kind).toBe('setter'); +expect(aContext.isStatic).toBe(false); +expect(aContext.isPrivate).toBe(false); +expect(typeof aContext.addInitializer).toBe('function'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); + +expect(bContext.name).toBe('b'); +expect(bContext.kind).toBe('setter'); +expect(bContext.isStatic).toBe(false); +expect(bContext.isPrivate).toBe(false); +expect(typeof bContext.addInitializer).toBe('function'); +expect(typeof bContext.setMetadata).toBe('function'); +expect(typeof bContext.getMetadata).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/public/input.js new file mode 100644 index 000000000000..b8b837257ff0 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/public/input.js @@ -0,0 +1,13 @@ +class Foo { + value = 1; + + @dec + set a(v) { + return this.value = v; + } + + @dec + set ['b'](v) { + return this.value = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/public/output.js new file mode 100644 index 000000000000..256297632372 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/public/output.js @@ -0,0 +1,26 @@ +var _computedKey, _dec, _dec2, _initProto; + +_dec = dec +_computedKey = 'b' +_dec2 = dec + +class Foo { + constructor(...args) { + babelHelpers.defineProperty(this, "value", 1); + + _initProto(this); + } + + set a(v) { + return this.value = v; + } + + set [_computedKey](v) { + return this.value = v; + } + +} + +(() => { + [_initProto] = babelHelpers.applyDecs(Foo, [[_dec, 4, "a"], [_dec2, 4, _computedKey]], []); +})(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-private/exec.js new file mode 100644 index 000000000000..55ecade3ac96 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-private/exec.js @@ -0,0 +1,38 @@ +function dec(set, context) { + context.addInitializer((instance) => { + instance[context.name + 'Context'] = context; + }); + + return function (v) { + return set.call(this, v + 1); + } +} + +class Foo { + static value = 1; + + @dec + static set #a(v) { + return this.value = v; + } + + static setA(v) { + this.#a = v; + } +} + +const aContext = Foo['#aContext']; + +expect(Foo.value).toBe(1); +aContext.access.set.call(Foo, 123); +expect(Foo.value).toBe(124); +Foo.setA(456); +expect(Foo.value).toBe(457); + +expect(aContext.name).toBe('#a'); +expect(aContext.kind).toBe('setter'); +expect(aContext.isStatic).toBe(true); +expect(aContext.isPrivate).toBe(true); +expect(typeof aContext.addInitializer).toBe('function'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-private/input.js new file mode 100644 index 000000000000..0038d6b6bd30 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-private/input.js @@ -0,0 +1,12 @@ +class Foo { + static value = 1; + + @dec + static set #a(v) { + return this.value = v; + } + + static setA(v) { + this.#a = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-private/output.js new file mode 100644 index 000000000000..45e47a57177c --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-private/output.js @@ -0,0 +1,27 @@ +var _call_a, _initStatic; + +class Foo { + static setA(v) { + babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _a, v); + } + +} + +function _set_a(v) { + _call_a(this, v); +} + +var _a = { + get: void 0, + set: _set_a +}; + +(() => { + [_call_a, _initStatic] = babelHelpers.applyDecs(Foo, [[dec, 9, "a", function (v) { + return this.value = v; + }]], []); + + _initStatic(Foo); +})(); + +babelHelpers.defineProperty(Foo, "value", 1); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-public/exec.js new file mode 100644 index 000000000000..2e7eb639048c --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-public/exec.js @@ -0,0 +1,49 @@ +function dec(set, context) { + context.addInitializer((instance) => { + instance[context.name + 'Context'] = context; + }); + + return function (v) { + return set.call(this, v + 1); + } +} + +class Foo { + static value = 1; + + @dec + static set a(v) { + return this.value = v; + } + + @dec + static set ['b'](v) { + return this.value = v; + } +} + +const aContext = Foo['aContext']; +const bContext = Foo['bContext']; + + +expect(Foo.value).toBe(1); +Foo.a = 123; +expect(Foo.value).toBe(124); +Foo.a = 456; +expect(Foo.value).toBe(457); + +expect(aContext.name).toBe('a'); +expect(aContext.kind).toBe('setter'); +expect(aContext.isStatic).toBe(true); +expect(aContext.isPrivate).toBe(false); +expect(typeof aContext.addInitializer).toBe('function'); +expect(typeof aContext.setMetadata).toBe('function'); +expect(typeof aContext.getMetadata).toBe('function'); + +expect(bContext.name).toBe('b'); +expect(bContext.kind).toBe('setter'); +expect(bContext.isStatic).toBe(true); +expect(bContext.isPrivate).toBe(false); +expect(typeof bContext.addInitializer).toBe('function'); +expect(typeof bContext.setMetadata).toBe('function'); +expect(typeof bContext.getMetadata).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-public/input.js new file mode 100644 index 000000000000..0cca65be7fa3 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-public/input.js @@ -0,0 +1,13 @@ +class Foo { + static value = 1; + + @dec + static set a(v) { + return this.value = v; + } + + @dec + static set ['b'](v) { + return this.value = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-public/output.js new file mode 100644 index 000000000000..e890b8fb8d4a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters--to-es2015/static-public/output.js @@ -0,0 +1,24 @@ +var _computedKey, _dec, _dec2, _initStatic; + +_dec = dec +_computedKey = 'b' +_dec2 = dec + +class Foo { + static set a(v) { + return this.value = v; + } + + static set [_computedKey](v) { + return this.value = v; + } + +} + +(() => { + [_initStatic] = babelHelpers.applyDecs(Foo, [[_dec, 9, "a"], [_dec2, 9, _computedKey]], []); + + _initStatic(Foo); +})(); + +babelHelpers.defineProperty(Foo, "value", 1); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/options.json new file mode 100644 index 000000000000..18aa205580db --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + [ + "proposal-decorators", + { "version": "2021-12", "decoratorsBeforeExport": false } + ] + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/private/input.js new file mode 100644 index 000000000000..254f2f102f9a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/private/input.js @@ -0,0 +1,12 @@ +class Foo { + value = 1; + + @dec + set #a(v) { + return this.value = v; + } + + setA(v) { + this.#a = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/private/output.js new file mode 100644 index 000000000000..a1b2e04ab8f1 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/private/output.js @@ -0,0 +1,24 @@ +var _call_a, _initProto; + +class Foo { + static { + [_call_a, _initProto] = babelHelpers.applyDecs(this, [[dec, 4, "a", function (v) { + return this.value = v; + }]], []); + } + + constructor(...args) { + _initProto(this); + } + + value = 1; + + set #a(v) { + _call_a(this, v); + } + + setA(v) { + this.#a = v; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/public/input.js new file mode 100644 index 000000000000..b8b837257ff0 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/public/input.js @@ -0,0 +1,13 @@ +class Foo { + value = 1; + + @dec + set a(v) { + return this.value = v; + } + + @dec + set ['b'](v) { + return this.value = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/public/output.js new file mode 100644 index 000000000000..ad6f2edce743 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/public/output.js @@ -0,0 +1,26 @@ +var _computedKey, _dec, _dec2, _initProto; + +_dec = dec +_computedKey = 'b' +_dec2 = dec + +class Foo { + static { + [_initProto] = babelHelpers.applyDecs(this, [[_dec, 4, "a"], [_dec2, 4, _computedKey]], []); + } + + constructor(...args) { + _initProto(this); + } + + value = 1; + + set a(v) { + return this.value = v; + } + + set [_computedKey](v) { + return this.value = v; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/static-private/input.js new file mode 100644 index 000000000000..0038d6b6bd30 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/static-private/input.js @@ -0,0 +1,12 @@ +class Foo { + static value = 1; + + @dec + static set #a(v) { + return this.value = v; + } + + static setA(v) { + this.#a = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/static-private/output.js new file mode 100644 index 000000000000..6bcadc0cc528 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/static-private/output.js @@ -0,0 +1,22 @@ +var _call_a, _initStatic; + +class Foo { + static { + [_call_a, _initStatic] = babelHelpers.applyDecs(this, [[dec, 9, "a", function (v) { + return this.value = v; + }]], []); + + _initStatic(this); + + } + static value = 1; + + static set #a(v) { + _call_a(this, v); + } + + static setA(v) { + this.#a = v; + } + +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/static-public/input.js new file mode 100644 index 000000000000..0cca65be7fa3 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/static-public/input.js @@ -0,0 +1,13 @@ +class Foo { + static value = 1; + + @dec + static set a(v) { + return this.value = v; + } + + @dec + static set ['b'](v) { + return this.value = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/static-public/output.js new file mode 100644 index 000000000000..230ee2bc8aff --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-setters/static-public/output.js @@ -0,0 +1,24 @@ +var _computedKey, _dec, _dec2, _initStatic; + +_dec = dec +_computedKey = 'b' +_dec2 = dec + +class Foo { + static { + [_initStatic] = babelHelpers.applyDecs(this, [[_dec, 9, "a"], [_dec2, 9, _computedKey]], []); + + _initStatic(this); + + } + static value = 1; + + static set a(v) { + return this.value = v; + } + + static set [_computedKey](v) { + return this.value = v; + } + +} diff --git a/packages/babel-plugin-syntax-decorators/src/index.ts b/packages/babel-plugin-syntax-decorators/src/index.ts index a825adfd77a5..ccfbeedaa77b 100644 --- a/packages/babel-plugin-syntax-decorators/src/index.ts +++ b/packages/babel-plugin-syntax-decorators/src/index.ts @@ -3,19 +3,29 @@ import { declare } from "@babel/helper-plugin-utils"; export default declare((api, options) => { api.assertVersion(7); - const { legacy = false } = options; + let { legacy = false } = options; if (typeof legacy !== "boolean") { throw new Error("'legacy' must be a boolean."); } + const { decoratorsBeforeExport, version = "2018-09" } = options; + if ( + !(version === "2021-12" || version === "2018-09" || version === "legacy") + ) { + throw new Error("Unsupported decorators version: " + version); + } + if (options.version !== undefined && options.legacy !== undefined) { + throw new Error( + 'You can either specify `legacy: true` or `version: "legacy"` with decorators, not both.', + ); + } + legacy ||= version === "legacy"; - const { decoratorsBeforeExport } = options; if (decoratorsBeforeExport === undefined) { if (!legacy) { throw new Error( - "The '@babel/plugin-syntax-decorators' plugin requires a" + - " 'decoratorsBeforeExport' option, whose value must be a boolean." + - " If you want to use the legacy decorators semantics, you can set" + - " the 'legacy: true' option.", + "The decorators plugin requires a 'decoratorsBeforeExport' option," + + " whose value must be a boolean. If you want to use the legacy" + + " decorators semantics, you can set the `version: 'legacy'` option.", ); } } else { @@ -38,6 +48,10 @@ export default declare((api, options) => { ? "decorators-legacy" : ["decorators", { decoratorsBeforeExport }], ); + + if (version === "2021-12") { + parserOpts.plugins.push("decoratorAutoAccessors"); + } }, }; }); diff --git a/packages/babel-runtime-corejs2/package.json b/packages/babel-runtime-corejs2/package.json index 217b7b8fa822..98577387d20d 100644 --- a/packages/babel-runtime-corejs2/package.json +++ b/packages/babel-runtime-corejs2/package.json @@ -18,6 +18,15 @@ "regenerator-runtime": "^0.13.4" }, "exports": { + "./helpers/applyDecs": [ + { + "node": "./helpers/applyDecs.js", + "import": "./helpers/esm/applyDecs.js", + "default": "./helpers/applyDecs.js" + }, + "./helpers/applyDecs.js" + ], + "./helpers/esm/applyDecs": "./helpers/esm/applyDecs.js", "./helpers/asyncIterator": [ { "node": "./helpers/asyncIterator.js", @@ -837,6 +846,15 @@ "./helpers/classPrivateMethodSet.js" ], "./helpers/esm/classPrivateMethodSet": "./helpers/esm/classPrivateMethodSet.js", + "./helpers/identity": [ + { + "node": "./helpers/identity.js", + "import": "./helpers/esm/identity.js", + "default": "./helpers/identity.js" + }, + "./helpers/identity.js" + ], + "./helpers/esm/identity": "./helpers/esm/identity.js", "./package": "./package.json", "./package.json": "./package.json", "./regenerator": "./regenerator/index.js", diff --git a/packages/babel-runtime-corejs3/package.json b/packages/babel-runtime-corejs3/package.json index 254c7a2c3518..a6ddb186bec3 100644 --- a/packages/babel-runtime-corejs3/package.json +++ b/packages/babel-runtime-corejs3/package.json @@ -17,6 +17,15 @@ "regenerator-runtime": "^0.13.4" }, "exports": { + "./helpers/applyDecs": [ + { + "node": "./helpers/applyDecs.js", + "import": "./helpers/esm/applyDecs.js", + "default": "./helpers/applyDecs.js" + }, + "./helpers/applyDecs.js" + ], + "./helpers/esm/applyDecs": "./helpers/esm/applyDecs.js", "./helpers/asyncIterator": [ { "node": "./helpers/asyncIterator.js", @@ -836,6 +845,15 @@ "./helpers/classPrivateMethodSet.js" ], "./helpers/esm/classPrivateMethodSet": "./helpers/esm/classPrivateMethodSet.js", + "./helpers/identity": [ + { + "node": "./helpers/identity.js", + "import": "./helpers/esm/identity.js", + "default": "./helpers/identity.js" + }, + "./helpers/identity.js" + ], + "./helpers/esm/identity": "./helpers/esm/identity.js", "./package": "./package.json", "./package.json": "./package.json", "./regenerator": "./regenerator/index.js", diff --git a/packages/babel-runtime/package.json b/packages/babel-runtime/package.json index 9f58421f4e17..9ac7ec6efccc 100644 --- a/packages/babel-runtime/package.json +++ b/packages/babel-runtime/package.json @@ -17,6 +17,15 @@ "regenerator-runtime": "^0.13.4" }, "exports": { + "./helpers/applyDecs": [ + { + "node": "./helpers/applyDecs.js", + "import": "./helpers/esm/applyDecs.js", + "default": "./helpers/applyDecs.js" + }, + "./helpers/applyDecs.js" + ], + "./helpers/esm/applyDecs": "./helpers/esm/applyDecs.js", "./helpers/asyncIterator": [ { "node": "./helpers/asyncIterator.js", @@ -836,6 +845,15 @@ "./helpers/classPrivateMethodSet.js" ], "./helpers/esm/classPrivateMethodSet": "./helpers/esm/classPrivateMethodSet.js", + "./helpers/identity": [ + { + "node": "./helpers/identity.js", + "import": "./helpers/esm/identity.js", + "default": "./helpers/identity.js" + }, + "./helpers/identity.js" + ], + "./helpers/esm/identity": "./helpers/esm/identity.js", "./package": "./package.json", "./package.json": "./package.json", "./regenerator": "./regenerator/index.js", diff --git a/packages/babel-standalone/src/preset-stage-0.ts b/packages/babel-standalone/src/preset-stage-0.ts index 7add5af3e1c1..66a8f8a180f6 100644 --- a/packages/babel-standalone/src/preset-stage-0.ts +++ b/packages/babel-standalone/src/preset-stage-0.ts @@ -5,7 +5,8 @@ export default (_: any, opts: any = {}) => { const { loose = false, useBuiltIns = false, - decoratorsLegacy = false, + decoratorsLegacy, + decoratorsVersion, decoratorsBeforeExport, pipelineProposal, pipelineTopicToken, @@ -20,6 +21,7 @@ export default (_: any, opts: any = {}) => { loose, useBuiltIns, decoratorsLegacy, + decoratorsVersion, decoratorsBeforeExport, pipelineProposal, pipelineTopicToken, diff --git a/packages/babel-standalone/src/preset-stage-1.ts b/packages/babel-standalone/src/preset-stage-1.ts index 6fdf52daec0f..67a3ef331819 100644 --- a/packages/babel-standalone/src/preset-stage-1.ts +++ b/packages/babel-standalone/src/preset-stage-1.ts @@ -5,7 +5,8 @@ export default (_: any, opts: any = {}) => { const { loose = false, useBuiltIns = false, - decoratorsLegacy = false, + decoratorsLegacy, + decoratorsVersion, decoratorsBeforeExport, pipelineProposal, pipelineTopicToken, @@ -20,6 +21,7 @@ export default (_: any, opts: any = {}) => { loose, useBuiltIns, decoratorsLegacy, + decoratorsVersion, decoratorsBeforeExport, pipelineProposal, pipelineTopicToken, diff --git a/packages/babel-standalone/src/preset-stage-2.ts b/packages/babel-standalone/src/preset-stage-2.ts index 4d743659dae5..26f172b49b0f 100644 --- a/packages/babel-standalone/src/preset-stage-2.ts +++ b/packages/babel-standalone/src/preset-stage-2.ts @@ -6,6 +6,7 @@ export default (_: any, opts: any = {}) => { loose = false, useBuiltIns = false, decoratorsLegacy = false, + decoratorsVersion = "2018-09", decoratorsBeforeExport, pipelineProposal = "minimal", pipelineTopicToken = "%", @@ -17,7 +18,10 @@ export default (_: any, opts: any = {}) => { plugins: [ [ babelPlugins.proposalDecorators, - { legacy: decoratorsLegacy, decoratorsBeforeExport }, + { + version: decoratorsLegacy ? "legacy" : decoratorsVersion, + decoratorsBeforeExport, + }, ], [ babelPlugins.proposalPipelineOperator, diff --git a/packages/babel-standalone/test/preset-stage-1.test.js b/packages/babel-standalone/test/preset-stage-1.test.js index cbac8842006e..d8f2e111811c 100644 --- a/packages/babel-standalone/test/preset-stage-1.test.js +++ b/packages/babel-standalone/test/preset-stage-1.test.js @@ -55,5 +55,20 @@ _ref = x, _ref;" _ref = x, _ref;" `); }); + it("should support decorators versioned 2021-12", () => { + const output = Babel.transform("@dec class C {}", { + plugins: [["external-helpers", { helperVersion: "7.100.0" }]], + presets: [ + [ + "stage-1", + { + decoratorsVersion: "2021-12", + decoratorsBeforeExport: false, + }, + ], + ], + }).code; + expect(output).toMatch("babelHelpers.applyDecs"); + }); }, ); diff --git a/packages/babel-types/src/ast-types/generated/index.ts b/packages/babel-types/src/ast-types/generated/index.ts index 5e6547f5e991..f750429c735d 100644 --- a/packages/babel-types/src/ast-types/generated/index.ts +++ b/packages/babel-types/src/ast-types/generated/index.ts @@ -736,8 +736,10 @@ export interface ClassBody extends BaseNode { | ClassPrivateMethod | ClassProperty | ClassPrivateProperty + | ClassAccessorProperty | TSDeclareMethod | TSIndexSignature + | StaticBlock >; } diff --git a/packages/babel-types/src/builders/generated/index.ts b/packages/babel-types/src/builders/generated/index.ts index 5ba1190e4145..130814020c40 100644 --- a/packages/babel-types/src/builders/generated/index.ts +++ b/packages/babel-types/src/builders/generated/index.ts @@ -344,8 +344,10 @@ export function classBody( | t.ClassPrivateMethod | t.ClassProperty | t.ClassPrivateProperty + | t.ClassAccessorProperty | t.TSDeclareMethod | t.TSIndexSignature + | t.StaticBlock >, ): t.ClassBody { return builder.apply("ClassBody", arguments); diff --git a/packages/babel-types/src/definitions/core.ts b/packages/babel-types/src/definitions/core.ts index 5c38150c6a46..df9783987d60 100644 --- a/packages/babel-types/src/definitions/core.ts +++ b/packages/babel-types/src/definitions/core.ts @@ -1262,8 +1262,10 @@ defineType("ClassBody", { "ClassPrivateMethod", "ClassProperty", "ClassPrivateProperty", + "ClassAccessorProperty", "TSDeclareMethod", "TSIndexSignature", + "StaticBlock", ), ), ), diff --git a/yarn.lock b/yarn.lock index e652e42876aa..91ef49c8c271 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1307,9 +1307,11 @@ __metadata: "@babel/helper-create-class-features-plugin": "workspace:^" "@babel/helper-plugin-test-runner": "workspace:^" "@babel/helper-plugin-utils": "workspace:^" + "@babel/helper-replace-supers": "workspace:^" "@babel/plugin-syntax-decorators": "workspace:^" "@babel/traverse": "workspace:^" babel-plugin-polyfill-es-shims: ^0.6.0 + charcodes: ^0.2.0 object.getownpropertydescriptors: ^2.1.1 peerDependencies: "@babel/core": ^7.0.0-0