Skip to content

Commit 37765b4

Browse files
committed
fix: TS constructor parameter properties order
1 parent e5df16c commit 37765b4

File tree

9 files changed

+34
-31
lines changed

9 files changed

+34
-31
lines changed

crates/swc/tests/fixture/issues-6xxx/6985/useDefineForClassFields/2020_TS/output/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import _define_property from "@swc/helpers/src/_define_property.mjs";
44
var _b = /*#__PURE__*/ new WeakMap();
55
class Foo {
66
constructor(foo){
7+
_define_property(this, "foo", void 0);
78
_define_property(this, "a", void 0);
89
_class_private_field_init(this, _b, {
910
writable: true,
1011
value: void 0
1112
});
12-
_define_property(this, "foo", void 0);
1313
this.foo = foo;
1414
this.a = 1;
1515
_class_private_field_set(this, _b, 2);

crates/swc/tests/fixture/issues-6xxx/6985/useDefineForClassFields/2022_TS/output/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class Foo {
2+
foo;
23
a;
34
#b;
45
static c = 3;
@@ -8,5 +9,4 @@ class Foo {
89
this.a = 1;
910
this.#b = 2;
1011
}
11-
foo;
1212
}

crates/swc/tests/tsc-references/assignParameterPropertyToPropertyDeclarationESNext.1.normal.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//// [assignParameterPropertyToPropertyDeclarationESNext.ts]
22
class C {
3+
foo;
34
qux;
45
bar;
56
quiz;
@@ -36,21 +37,20 @@ class C {
3637
this.foo // ok
3738
;
3839
}
39-
foo;
4040
}
4141
class D extends C {
4242
quill = this.foo // ok
4343
;
4444
}
4545
class E {
46+
foo2;
4647
bar;
4748
foo1;
4849
constructor(foo2){
4950
this.foo2 = foo2;
5051
this.bar = ()=>this.foo1 + this.foo2;
5152
this.foo1 = '';
5253
}
53-
foo2;
5454
}
5555
class F {
5656
Inner = class extends F {
@@ -59,16 +59,17 @@ class F {
5959
p1 = 0;
6060
}
6161
class G {
62+
p1;
6263
Inner;
6364
constructor(p1){
6465
this.p1 = p1;
6566
this.Inner = class extends G {
6667
p2 = this.p1;
6768
};
6869
}
69-
p1;
7070
}
7171
class H {
72+
p1;
7273
constructor(p1){
7374
this.p1 = p1;
7475
this.p2 = ()=>{
@@ -78,5 +79,4 @@ class H {
7879
}
7980
p2;
8081
p3;
81-
p1;
8282
}

crates/swc/tests/tsc-references/defineProperty(target=es2022).1.normal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var x = "p";
33
let _x = x;
44
class A {
5+
y;
56
a;
67
b;
78
c;
@@ -16,12 +17,12 @@ class A {
1617
this.z = this.y;
1718
}
1819
z;
19-
y;
2020
}
2121
class B {
2222
a;
2323
}
2424
class C extends B {
25+
ka;
2526
z;
2627
constructor(ka){
2728
super();
@@ -30,5 +31,4 @@ class C extends B {
3031
this.ki = this.ka;
3132
}
3233
ki;
33-
ka;
3434
}

crates/swc/tests/tsc-references/defineProperty(target=es5).1.normal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ var A = /*#__PURE__*/ function() {
1212
"use strict";
1313
function A(y) {
1414
_class_call_check(this, A);
15+
_define_property(this, "y", void 0);
1516
_define_property(this, "a", void 0);
1617
_define_property(this, "b", void 0);
1718
_define_property(this, "c", void 0);
1819
_define_property(this, "computed", void 0);
1920
_define_property(this, _x1, void 0);
2021
_define_property(this, "z", void 0);
21-
_define_property(this, "y", void 0);
2222
this.y = y;
2323
this.a = this.y;
2424
this["computed"] = 13;
@@ -46,9 +46,9 @@ var C = /*#__PURE__*/ function(B) {
4646
_class_call_check(this, C);
4747
var _this;
4848
_this = _super.call(this);
49+
_define_property(_assert_this_initialized(_this), "ka", void 0);
4950
_define_property(_assert_this_initialized(_this), "z", void 0);
5051
_define_property(_assert_this_initialized(_this), "ki", void 0);
51-
_define_property(_assert_this_initialized(_this), "ka", void 0);
5252
_this.ka = ka;
5353
_this.z = _this.ka;
5454
_this.ki = _this.ka;

crates/swc/tests/tsc-references/emitStatementsBeforeSuperCallWithDefineFields.1.normal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class Sub extends Base {
77
constructor(p){
88
console.log('hi');
99
super();
10-
_define_property(this, "field", void 0);
1110
_define_property(this, "p", void 0);
11+
_define_property(this, "field", void 0);
1212
this.p = p;
1313
this.field = 0;
1414
}
@@ -18,8 +18,8 @@ class Test extends Base {
1818
constructor(p){
1919
1;
2020
super();
21-
_define_property(this, "prop", void 0);
2221
_define_property(this, "p", void 0);
22+
_define_property(this, "prop", void 0);
2323
this.p = p;
2424
this.prop = 1;
2525
}

crates/swc/tests/tsc-references/propertyOverridesAccessors5.1.normal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ class A {
55
}
66
}
77
class B extends A {
8+
p;
89
constructor(p){
910
super();
1011
this.p = p;
1112
}
12-
p;
1313
}

crates/swc/tests/tsc-references/redefinedPararameterProperty.1.normal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ class Base {
33
a = 1;
44
}
55
class Derived extends Base {
6+
a;
67
b;
78
constructor(a){
89
super();
910
this.a = a;
1011
this.b = this.a /*undefined*/ ;
1112
}
12-
a;
1313
}

crates/swc_ecma_transforms_typescript/src/strip.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,23 +1962,26 @@ where
19621962
inject_after_super(c, assign_exprs);
19631963

19641964
if !extra_props.is_empty() {
1965-
class.body.extend(extra_props.into_iter().map(|prop| {
1966-
ClassMember::ClassProp(ClassProp {
1967-
key: PropName::Ident(prop),
1968-
value: None,
1969-
decorators: Vec::new(),
1970-
is_static: false,
1971-
type_ann: None,
1972-
span: DUMMY_SP,
1973-
accessibility: None,
1974-
is_abstract: false,
1975-
is_optional: false,
1976-
is_override: false,
1977-
readonly: false,
1978-
declare: false,
1979-
definite: false,
1980-
})
1981-
}))
1965+
class.body.splice(
1966+
0..0,
1967+
extra_props.into_iter().map(|prop| {
1968+
ClassMember::ClassProp(ClassProp {
1969+
key: PropName::Ident(prop),
1970+
value: None,
1971+
decorators: Vec::new(),
1972+
is_static: false,
1973+
type_ann: None,
1974+
span: DUMMY_SP,
1975+
accessibility: None,
1976+
is_abstract: false,
1977+
is_optional: false,
1978+
is_override: false,
1979+
readonly: false,
1980+
declare: false,
1981+
definite: false,
1982+
})
1983+
}),
1984+
);
19821985
}
19831986
}
19841987

0 commit comments

Comments
 (0)