From 55cfad152d52e7f818fc4e71913e94ce84ab91a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 18 Mar 2022 10:44:13 +0900 Subject: [PATCH] fix(es/helpers): Fix decorator helper script (#4072) --- crates/swc_ecma_transforms/Cargo.toml | 1 + .../tests/fixture/decorator/issue-3705/exec.ts | 12 ++++++++++++ .../src/helpers/_apply_decorated_descriptor.js | 9 ++++++++- .../swc-helpers/src/_apply_decorated_descriptor.js | 8 +++++++- 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 crates/swc_ecma_transforms/tests/fixture/decorator/issue-3705/exec.ts diff --git a/crates/swc_ecma_transforms/Cargo.toml b/crates/swc_ecma_transforms/Cargo.toml index 7931386bacf..9cc8acfd31a 100644 --- a/crates/swc_ecma_transforms/Cargo.toml +++ b/crates/swc_ecma_transforms/Cargo.toml @@ -3,6 +3,7 @@ authors = ["강동윤 "] description = "rust port of babel and closure compiler." documentation = "https://rustdoc.swc.rs/swc_ecma_transforms/" edition = "2021" +include = ["Cargo.toml", "src/**/*.rs"] license = "Apache-2.0" name = "swc_ecma_transforms" repository = "https://github.com/swc-project/swc.git" diff --git a/crates/swc_ecma_transforms/tests/fixture/decorator/issue-3705/exec.ts b/crates/swc_ecma_transforms/tests/fixture/decorator/issue-3705/exec.ts new file mode 100644 index 00000000000..6a25bd1ff08 --- /dev/null +++ b/crates/swc_ecma_transforms/tests/fixture/decorator/issue-3705/exec.ts @@ -0,0 +1,12 @@ +function field(host: any, field: any, descr?: any): any { + return { ...descr, get: () => { }, set: () => { } } +} + +class TestMems { + @field + static some = 1 +} + + +expect(Object.keys(TestMems)).toEqual(['some']) +expect(TestMems.some).toBeUndefined() \ No newline at end of file diff --git a/crates/swc_ecma_transforms_base/src/helpers/_apply_decorated_descriptor.js b/crates/swc_ecma_transforms_base/src/helpers/_apply_decorated_descriptor.js index 96331891177..a2a9fd0125d 100644 --- a/crates/swc_ecma_transforms_base/src/helpers/_apply_decorated_descriptor.js +++ b/crates/swc_ecma_transforms_base/src/helpers/_apply_decorated_descriptor.js @@ -12,10 +12,17 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator ? (decorator(target, property, desc) || desc) : desc; }, desc); - if (context && desc.initializer !== void 0) { + var hasAccessor = Object.prototype.hasOwnProperty.call(desc, 'get') || Object.prototype.hasOwnProperty.call(desc, 'set'); + if (context && desc.initializer !== void 0 && !hasAccessor) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } + + if (hasAccessor) { + delete desc.writable; + delete desc.initializer; + delete desc.value; + } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; diff --git a/packages/swc-helpers/src/_apply_decorated_descriptor.js b/packages/swc-helpers/src/_apply_decorated_descriptor.js index a3c26dd63cf..bccd7d21cf1 100644 --- a/packages/swc-helpers/src/_apply_decorated_descriptor.js +++ b/packages/swc-helpers/src/_apply_decorated_descriptor.js @@ -14,11 +14,17 @@ export default function _applyDecoratedDescriptor(target, property, decorators, return decorator ? (decorator(target, property, desc) || desc) : desc; }, desc); - if (context && desc.initializer !== void 0) { + var hasAccessor = Object.prototype.hasOwnProperty.call(desc, 'get') || Object.prototype.hasOwnProperty.call(desc, 'set'); + if (context && desc.initializer !== void 0 && !hasAccessor) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } + if (hasAccessor) { + delete desc.writable; + delete desc.initializer; + delete desc.value; + } if (desc.initializer === void 0) { Object['define' + 'Property'](target, property, desc); desc = null;