Skip to content

Commit

Permalink
fix(es/compat): Fix legacy decorator pass (#3459)
Browse files Browse the repository at this point in the history
  • Loading branch information
williamtetlow authored Feb 7, 2022
1 parent ec12019 commit 4f5e87b
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 78 deletions.
7 changes: 1 addition & 6 deletions crates/swc/tests/fixture/issue-1160/output/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}
var own = Object.getOwnPropertyDescriptor(target, property);
if (own && (own.get || own.set)) {
delete desc.writable;
delete desc.initializer;
}
if (desc.initializer === void 0) {
Object.defineProperty(target, property, desc);
desc = null;
Expand Down Expand Up @@ -53,7 +48,7 @@ let Xpto = ((_class = class Xpto {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _class);
function Decorator() {
return function(...args) {};
Expand Down
7 changes: 1 addition & 6 deletions crates/swc/tests/fixture/issue-1278/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}
var own = Object.getOwnPropertyDescriptor(target, property);
if (own && (own.get || own.set)) {
delete desc.writable;
delete desc.initializer;
}
if (desc.initializer === void 0) {
Object.defineProperty(target, property, desc);
desc = null;
Expand Down Expand Up @@ -54,6 +49,6 @@ let MyClass = ((_class = class MyClass {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _class);
console.log(new MyClass());
21 changes: 8 additions & 13 deletions crates/swc/tests/fixture/issue-1345/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}
var own = Object.getOwnPropertyDescriptor(target, property);
if (own && (own.get || own.set)) {
delete desc.writable;
delete desc.initializer;
}
if (desc.initializer === void 0) {
Object.defineProperty(target, property, desc);
desc = null;
Expand Down Expand Up @@ -75,61 +70,61 @@ export var AccountMemberView = _class = _dec16((_class = function AccountMemberV
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _descriptor1 = _applyDecoratedDescriptor(_class.prototype, "mallId", [
_dec2,
_dec3
], {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _descriptor2 = _applyDecoratedDescriptor(_class.prototype, "allowAccountCnt", [
_dec4,
_dec5
], {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _descriptor3 = _applyDecoratedDescriptor(_class.prototype, "allowQuickAccountCnt", [
_dec6,
_dec7
], {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _descriptor4 = _applyDecoratedDescriptor(_class.prototype, "accountEnddedAt", [
_dec8,
_dec9
], {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _descriptor5 = _applyDecoratedDescriptor(_class.prototype, "accountQuickEnddedAt", [
_dec10,
_dec11
], {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _descriptor6 = _applyDecoratedDescriptor(_class.prototype, "accountCnt", [
_dec12,
_dec13
], {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _descriptor7 = _applyDecoratedDescriptor(_class.prototype, "accountQuickCnt", [
_dec14,
_dec15
], {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _class)) || _class;
5 changes: 0 additions & 5 deletions crates/swc/tests/fixture/issue-1362/case1/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}
var own = Object.getOwnPropertyDescriptor(target, property);
if (own && (own.get || own.set)) {
delete desc.writable;
delete desc.initializer;
}
if (desc.initializer === void 0) {
Object.defineProperty(target, property, desc);
desc = null;
Expand Down
7 changes: 1 addition & 6 deletions crates/swc/tests/fixture/issue-1421/case1/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}
var own = Object.getOwnPropertyDescriptor(target, property);
if (own && (own.get || own.set)) {
delete desc.writable;
delete desc.initializer;
}
if (desc.initializer === void 0) {
Object.defineProperty(target, property, desc);
desc = null;
Expand Down Expand Up @@ -60,5 +55,5 @@ var User = ((_class = function User() {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _class);
5 changes: 0 additions & 5 deletions crates/swc/tests/fixture/issue-2428/1/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}
var own = Object.getOwnPropertyDescriptor(target, property);
if (own && (own.get || own.set)) {
delete desc.writable;
delete desc.initializer;
}
if (desc.initializer === void 0) {
Object.defineProperty(target, property, desc);
desc = null;
Expand Down
7 changes: 1 addition & 6 deletions crates/swc/tests/fixture/issue-3337/output/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}
var own = Object.getOwnPropertyDescriptor(target, property);
if (own && (own.get || own.set)) {
delete desc.writable;
delete desc.initializer;
}
if (desc.initializer === void 0) {
Object.defineProperty(target, property, desc);
desc = null;
Expand Down Expand Up @@ -75,5 +70,5 @@ var Schema = ((_class = function Schema() {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _class);
30 changes: 15 additions & 15 deletions crates/swc_ecma_transforms/tests/decorators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4467,42 +4467,42 @@ export let Product = _class = _dec6(((_class = class Product extends Timestamped
configurable: true,
enumerable: true,
writable: true,
initializer: void 0,
initializer: null,
}), _descriptor1 = _applyDecoratedDescriptor(_class.prototype, 'price', [
_dec1
], {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0,
initializer: null,
}), _descriptor2 = _applyDecoratedDescriptor(_class.prototype, 'type', [
_dec2
], {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0,
initializer: null,
}), _descriptor3 = _applyDecoratedDescriptor(_class.prototype, 'productEntityId', [
_dec3
], {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0,
initializer: null,
}), _descriptor4 = _applyDecoratedDescriptor(_class.prototype, 'orders', [
_dec4
], {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0,
initializer: null,
}), _descriptor5 = _applyDecoratedDescriptor(_class.prototype, 'discounts', [
_dec5
], {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _class)) || _class;
"
);
Expand Down Expand Up @@ -4532,7 +4532,7 @@ export class Product extends TimestampedEntity {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _class)) || _class;
"
);
Expand Down Expand Up @@ -4789,15 +4789,15 @@ export let AppController = _class = _dec14(_class = _dec13(_class = _dec12(((_cl
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _descriptor1 = _applyDecoratedDescriptor(_class.prototype, "appService2", [
_dec2,
_dec3
], {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _dec4 = Get(), _dec5 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:type", Function), _dec6 = typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata("design:paramtypes", []), _applyDecoratedDescriptor(_class.prototype, "getHello", [
_dec4,
_dec5,
Expand Down Expand Up @@ -5124,7 +5124,7 @@ test!(
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _class);
function Decorator() {
return function(...args) {
Expand Down Expand Up @@ -5719,7 +5719,7 @@ test!(
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _class);
"
);
Expand Down Expand Up @@ -5858,7 +5858,7 @@ test!(
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _class);
console.log(new MyClass());
"
Expand Down Expand Up @@ -6029,7 +6029,7 @@ test!(
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _class);"
);

Expand All @@ -6048,11 +6048,11 @@ fn fixture(input: PathBuf) {

chain!(
resolver_with_mark(top_level_mark),
strip(top_level_mark),
decorators(Config {
legacy: true,
emit_metadata: true,
})
}),
strip(top_level_mark),
)
},
&code,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class TestClass2 {
@deco public testProperty?: string;
}

function deco(target: any, key: string) {
console.log(target, key);
}

const instance = new TestClass2();
expect(instance.hasOwnProperty("testProperty")).toBe(true);
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const returnValue = "asdasd"
class TestClass2 {
@deco public testProperty: Date;
@deco public declare testProperty: Date;
}
function deco(target: any, key: string) {
console.log(target, key);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const returnValue = "asdasd"
class TestClass2 {
@deco public testProperty: Date;
@deco public declare testProperty: Date;
}
function deco(target: any, key: string) {
console.log(target, key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}

var own = Object.getOwnPropertyDescriptor(target, property);
if (own && (own.get || own.set)) {
// Prevent overriding
delete desc.writable;
delete desc.initializer;
}

if (desc.initializer === void 0) {
Object.defineProperty(target, property, desc);
desc = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,7 @@ impl Legacy {
},
}))
} else {
undefined(DUMMY_SP)
// Box::new(Expr::Lit(Lit::Null(Null { span:
// DUMMY_SP })))
Box::new(Expr::Lit(Lit::Null(Null { span: DUMMY_SP })))
},
}))),
],
Expand Down Expand Up @@ -675,7 +673,7 @@ impl Legacy {
extra_exprs.push(call_expr);
}

if !p.is_static {
if !p.is_static && !p.declare {
constructor_stmts.push(
CallExpr {
span: DUMMY_SP,
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_transforms_typescript/tests/strip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ test!(
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
initializer: null
}), _class);
"#,
ok_if_code_eq
Expand Down Expand Up @@ -994,7 +994,7 @@ test_exec!(
}
class Child extends Base {
@DefineAction() action: number
@DefineAction() declare action: number
callApi() {
console.log(this.action) // undefined
Expand Down

1 comment on commit 4f5e87b

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 4f5e87b Previous: ec12019 Ratio
full_es2015 212259390 ns/iter (± 11387851) 170250754 ns/iter (± 19716588) 1.25
full_es2016 226035319 ns/iter (± 13486402) 176813931 ns/iter (± 21169408) 1.28
full_es2017 222857563 ns/iter (± 16528123) 177922753 ns/iter (± 19843000) 1.25
full_es2018 220786265 ns/iter (± 18331203) 179045889 ns/iter (± 12518899) 1.23
full_es2019 220337767 ns/iter (± 12725208) 176714601 ns/iter (± 9826885) 1.25
full_es2020 192878309 ns/iter (± 13220187) 157786957 ns/iter (± 22565011) 1.22
full_es3 271883110 ns/iter (± 16672400) 219656153 ns/iter (± 44820389) 1.24
full_es5 277813152 ns/iter (± 14981729) 221269978 ns/iter (± 32053495) 1.26
parser 897632 ns/iter (± 79659) 715559 ns/iter (± 14395) 1.25

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.