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(es/compat): Simplify static blocks in the Class #7056

Merged
merged 4 commits into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/swc/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
private_as_properties: assumptions.private_fields_as_properties,
constant_super: assumptions.constant_super,
set_public_fields: assumptions.set_public_class_fields,
no_document_all: assumptions.no_document_all
no_document_all: assumptions.no_document_all,
static_blocks_mark: Mark::new(),
}
}
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
const a = 2;
class C {
}
var __ = {
writable: true,
value: (()=>{
const a = 1;
a;
})()
};
(()=>{
const a = 1;
a;
})();
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ var C = function C() {
"use strict";
_class_call_check(this, C);
};
var __ = {
writable: true,
value: function() {
var a = 1;
a;
}()
};
(function() {
var a = 1;
a;
})();
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,18 @@ function f() {
const b1 = 22;
class C1 {
}
var __ = {
writable: true,
value: (()=>{
var a1 = 111;
var a2 = 111;
const b1 = 222;
const b2 = 222;
})()
};
}
class C2 {
}
var __ = {
writable: true,
value: (()=>{
(()=>{
var a1 = 111;
var a2 = 111;
const b1 = 222;
const b2 = 222;
})()
};
})();
}
class C2 {
}
(()=>{
var a1 = 111;
var a2 = 111;
const b1 = 222;
const b2 = 222;
})();
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,20 @@ function f() {
"use strict";
_class_call_check(this, C1);
};
var __ = {
writable: true,
value: function() {
var a1 = 111;
var a2 = 111;
var b1 = 222;
var b2 = 222;
}()
};
}
var C2 = function C2() {
"use strict";
_class_call_check(this, C2);
};
var __ = {
writable: true,
value: function() {
(function() {
var a1 = 111;
var a2 = 111;
var b1 = 222;
var b2 = 222;
}()
})();
}
var C2 = function C2() {
"use strict";
_class_call_check(this, C2);
};
(function() {
var a1 = 111;
var a2 = 111;
var b1 = 222;
var b2 = 222;
})();
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ class C {
_class_private_field_set(this, _x, x);
}
}
var __ = {
writable: true,
value: (()=>{
// getX has privileged access to #x
getX = (obj)=>_class_private_field_get(obj, _x);
})()
};
(()=>{
// getX has privileged access to #x
getX = (obj)=>_class_private_field_get(obj, _x);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ var _x = {
writable: true,
value: 1
};
var __ = {
writable: true,
value: (()=>{
_class_static_private_field_spec_get(C, C, _x);
})()
};
(()=>{
_class_static_private_field_spec_get(C, C, _x);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ var _x = {
writable: true,
value: 123
};
var __ = {
writable: true,
value: (()=>{
console.log(_class_static_private_field_spec_get(C, C, _x));
})()
};
(()=>{
console.log(_class_static_private_field_spec_get(C, C, _x));
})();
30 changes: 6 additions & 24 deletions crates/swc/tests/tsc-references/classStaticBlock14.1.normal.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,9 @@ var __5 = {
writable: true,
value: 1
};
var __ = {
writable: true,
value: (()=>{})()
};
var __2 = {
writable: true,
value: (()=>{})()
};
var __4 = {
writable: true,
value: (()=>{})()
};
var __6 = {
writable: true,
value: (()=>{})()
};
var __7 = {
writable: true,
value: (()=>{})()
};
var __8 = {
writable: true,
value: (()=>{})()
};
(()=>{})();
(()=>{})();
(()=>{})();
(()=>{})();
(()=>{})();
(()=>{})();
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,10 @@ var __5 = {
writable: true,
value: 5
};
var __ = {
writable: true,
value: (()=>{})()
};
var __2 = {
writable: true,
value: (()=>{})()
};
var __4 = {
writable: true,
value: (()=>{})()
};
var __6 = {
writable: true,
value: (()=>{})()
};
var __7 = {
writable: true,
value: (()=>{})()
};
var __8 = {
writable: true,
value: (()=>{})()
};
(()=>{})();
(()=>{})();
(()=>{})();
(()=>{})();
(()=>{})();
(()=>{})();
console.log(_C__1);
23 changes: 10 additions & 13 deletions crates/swc/tests/tsc-references/classStaticBlock17.1.normal.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@ class A {
_class_private_field_set(this, _x, v);
}
}
var __ = {
writable: true,
value: (()=>{
friendA = {
getX (obj) {
return _class_private_field_get(obj, _x);
},
setX (obj, value) {
_class_private_field_set(obj, _x, value);
}
};
})()
};
(()=>{
friendA = {
getX (obj) {
return _class_private_field_get(obj, _x);
},
setX (obj, value) {
_class_private_field_set(obj, _x, value);
}
};
})();
class B {
constructor(a){
const x = friendA.getX(a); // ok
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
//// [classStaticBlock18.ts]
function foo() {
var _class, __;
var _class;
return _class = class {
}, _class.foo = 1, __ = {
writable: true,
value: (()=>{
var _class, __;
const c = (_class = class {
}, _class.bar = 2, __ = {
writable: true,
value: (()=>{
// do
})()
}, _class);
})()
}, _class;
}, _class.foo = 1, (()=>{
var _class;
const c = (_class = class {
}, _class.bar = 2, (()=>{
// do
})(), _class);
})(), _class;
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
//// [classStaticBlock18.ts]
import _class_call_check from "@swc/helpers/src/_class_call_check.mjs";
function foo() {
var _class, __;
var _class;
return _class = function _class() {
"use strict";
_class_call_check(this, _class);
}, _class.foo = 1, __ = {
writable: true,
value: function() {
var _class, __;
var c = (_class = function _class() {
"use strict";
_class_call_check(this, _class);
}, _class.bar = 2, __ = {
writable: true,
value: function() {
// do
}()
}, _class);
}()
}, _class;
}, _class.foo = 1, function() {
var _class;
var c = (_class = function _class() {
"use strict";
_class_call_check(this, _class);
}, _class.bar = 2, function() {
// do
}(), _class);
}(), _class;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@ const a = 1;
const b = 2;
class C {
}
var __ = {
writable: true,
value: (()=>{
const a = 11;
a;
b;
})()
};
var __1 = {
writable: true,
value: (()=>{
const a = 11;
a;
b;
})()
};
(()=>{
const a = 11;
a;
b;
})();
(()=>{
const a = 11;
a;
b;
})();
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@ var C = function C() {
"use strict";
_class_call_check(this, C);
};
var __ = {
writable: true,
value: function() {
var a = 11;
a;
b;
}()
};
var __1 = {
writable: true,
value: function() {
var a = 11;
a;
b;
}()
};
(function() {
var a = 11;
a;
b;
})();
(function() {
var a = 11;
a;
b;
})();
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ var C = function C() {
"use strict";
_class_call_check(this, C);
};
var __ = {
writable: true,
value: function() {
// something
}()
};
(function() {
// something
})();