fix(es/helpers): Fix decorator helper script (#4072)

This commit is contained in:
Donny/강동윤 2022-03-18 10:44:13 +09:00 committed by GitHub
parent 1483a71c7e
commit 55cfad152d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 2 deletions

View File

@ -3,6 +3,7 @@ authors = ["강동윤 <kdy1997.dev@gmail.com>"]
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"

View File

@ -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()

View File

@ -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;

View File

@ -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;