Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) Class fields are set to undefined if there's no initializer #3459

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
kdy1 marked this conversation as resolved.
Show resolved Hide resolved
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