mirror of
https://github.com/swc-project/swc.git
synced 2024-12-24 14:16:12 +03:00
fix(es/compat): Preserve more class names (#5106)
This commit is contained in:
parent
9385103649
commit
dd39a798ae
5
crates/swc/tests/fixture/issues-5xxx/5102/input/.swcrc
Normal file
5
crates/swc/tests/fixture/issues-5xxx/5102/input/.swcrc
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"jsc": {
|
||||
"target": "es5"
|
||||
}
|
||||
}
|
21
crates/swc/tests/fixture/issues-5xxx/5102/input/index.js
Normal file
21
crates/swc/tests/fixture/issues-5xxx/5102/input/index.js
Normal file
@ -0,0 +1,21 @@
|
||||
let a = class /* a */ {};
|
||||
let [b] = [class /* no named */ {}];
|
||||
let [c = class /* c */ {}] = [];
|
||||
let [d = class /* d */ {}] = [class /* no named */ {}];
|
||||
|
||||
let { e } = { e: class /* e */ {} };
|
||||
let { f = class /* f */ {} } = {};
|
||||
let { g = class /* g */ {} } = { g: class /* g */ {} };
|
||||
let { _: h = class /* h */ {} } = {};
|
||||
|
||||
a = class /* a */ {};
|
||||
b ||= class /* b */ {};
|
||||
c ??= class /* c */ {};
|
||||
|
||||
function foo(bar = class /* bar */ {}) {}
|
||||
|
||||
d = {
|
||||
i: class /* i */ {},
|
||||
["j"]: class /* j */ {},
|
||||
"-": class {},
|
||||
};
|
79
crates/swc/tests/fixture/issues-5xxx/5102/output/index.js
Normal file
79
crates/swc/tests/fixture/issues-5xxx/5102/output/index.js
Normal file
@ -0,0 +1,79 @@
|
||||
import _class_call_check from "@swc/helpers/src/_class_call_check.mjs";
|
||||
import _define_property from "@swc/helpers/src/_define_property.mjs";
|
||||
var a = function a() {
|
||||
"use strict";
|
||||
_class_call_check(this, a);
|
||||
};
|
||||
var ref = [
|
||||
function _class() {
|
||||
"use strict";
|
||||
_class_call_check(this, _class);
|
||||
}
|
||||
], b = ref[0];
|
||||
var ref1 = [], tmp = ref1[0], c = tmp === void 0 ? function c() {
|
||||
"use strict";
|
||||
_class_call_check(this, c);
|
||||
} : tmp;
|
||||
var ref2 = [
|
||||
function _class() {
|
||||
"use strict";
|
||||
_class_call_check(this, _class);
|
||||
}
|
||||
], tmp1 = ref2[0], d = tmp1 === void 0 ? function d() {
|
||||
"use strict";
|
||||
_class_call_check(this, d);
|
||||
} : tmp1;
|
||||
var e = {
|
||||
e: function e() {
|
||||
"use strict";
|
||||
_class_call_check(this, e);
|
||||
}
|
||||
}.e;
|
||||
var ref3 = {}, _f = ref3.f, f = _f === void 0 ? function f() {
|
||||
"use strict";
|
||||
_class_call_check(this, f);
|
||||
} : _f;
|
||||
var ref4 = {
|
||||
g: function g() {
|
||||
"use strict";
|
||||
_class_call_check(this, g);
|
||||
}
|
||||
}, _g = ref4.g, g = _g === void 0 ? function g() {
|
||||
"use strict";
|
||||
_class_call_check(this, g);
|
||||
} : _g;
|
||||
var ref5 = {}, tmp2 = ref5._, h = tmp2 === void 0 ? function h() {
|
||||
"use strict";
|
||||
_class_call_check(this, h);
|
||||
} : tmp2;
|
||||
a = function a() {
|
||||
"use strict";
|
||||
_class_call_check(this, a);
|
||||
};
|
||||
b || (b = function b() {
|
||||
"use strict";
|
||||
_class_call_check(this, b);
|
||||
});
|
||||
c !== null && c !== void 0 ? c : c = function c() {
|
||||
"use strict";
|
||||
_class_call_check(this, c);
|
||||
};
|
||||
function foo() {
|
||||
var bar = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : function bar() {
|
||||
"use strict";
|
||||
_class_call_check(this, bar);
|
||||
};
|
||||
}
|
||||
var _obj;
|
||||
d = (_obj = {
|
||||
i: function i() {
|
||||
"use strict";
|
||||
_class_call_check(this, i);
|
||||
}
|
||||
}, _define_property(_obj, "j", function j() {
|
||||
"use strict";
|
||||
_class_call_check(this, j);
|
||||
}), _define_property(_obj, "-", function _class() {
|
||||
"use strict";
|
||||
_class_call_check(this, _class);
|
||||
}), _obj);
|
@ -1,9 +1,9 @@
|
||||
import _class_call_check from "@swc/helpers/src/_class_call_check.mjs";
|
||||
var _class;
|
||||
function foo() {
|
||||
var x = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : function _class() {
|
||||
var x = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : function x() {
|
||||
"use strict";
|
||||
_class_call_check(this, _class);
|
||||
_class_call_check(this, x);
|
||||
};
|
||||
return undefined;
|
||||
}
|
||||
|
@ -1,7 +1,10 @@
|
||||
var _class;
|
||||
import _class_call_check from "@swc/helpers/src/_class_call_check.mjs";
|
||||
(function() {
|
||||
arguments.length > 0 && void 0 !== arguments[0] && arguments[0];
|
||||
var x = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : function() {
|
||||
"use strict";
|
||||
_class_call_check(this, x);
|
||||
};
|
||||
})(((_class = function() {
|
||||
"use strict";
|
||||
_class_call_check(this, _class);
|
||||
|
@ -1,9 +1,9 @@
|
||||
import _class_call_check from "@swc/helpers/src/_class_call_check.mjs";
|
||||
var _class;
|
||||
function foo() {
|
||||
var x = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : function _class() {
|
||||
var x = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : function x() {
|
||||
"use strict";
|
||||
_class_call_check(this, _class);
|
||||
_class_call_check(this, x);
|
||||
};
|
||||
return undefined;
|
||||
}
|
||||
|
@ -1,7 +1,10 @@
|
||||
var _class;
|
||||
import _class_call_check from "@swc/helpers/src/_class_call_check.mjs";
|
||||
(function() {
|
||||
arguments.length > 0 && void 0 !== arguments[0] && arguments[0];
|
||||
var x = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : function() {
|
||||
"use strict";
|
||||
_class_call_check(this, x);
|
||||
};
|
||||
})(((_class = function() {
|
||||
"use strict";
|
||||
_class_call_check(this, _class);
|
||||
|
@ -1,8 +1,8 @@
|
||||
import _class_call_check from "@swc/helpers/src/_class_call_check.mjs";
|
||||
function foo() {
|
||||
var x = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : function _class() {
|
||||
var x = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : function x() {
|
||||
"use strict";
|
||||
_class_call_check(this, _class);
|
||||
_class_call_check(this, x);
|
||||
};
|
||||
return undefined;
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
import _class_call_check from "@swc/helpers/src/_class_call_check.mjs";
|
||||
(function() {
|
||||
arguments.length > 0 && void 0 !== arguments[0] && arguments[0];
|
||||
var x = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : function() {
|
||||
"use strict";
|
||||
_class_call_check(this, x);
|
||||
};
|
||||
})(function _class() {
|
||||
"use strict";
|
||||
_class_call_check(this, _class), this.prop = "hello";
|
||||
|
@ -14,9 +14,9 @@ import _class_call_check from "@swc/helpers/src/_class_call_check.mjs";
|
||||
_class_call_check(this, _class);
|
||||
};
|
||||
/** @typedef {number} Baz */ module.exports = {
|
||||
Baz: function _class() {
|
||||
Baz: function Baz() {
|
||||
"use strict";
|
||||
_class_call_check(this, _class);
|
||||
_class_call_check(this, Baz);
|
||||
}
|
||||
};
|
||||
// ok
|
||||
|
@ -7,9 +7,9 @@ exports.Bar = function _class() {
|
||||
"use strict";
|
||||
_class_call_check(this, _class);
|
||||
}, module.exports = {
|
||||
Baz: function _class() {
|
||||
Baz: function Baz() {
|
||||
"use strict";
|
||||
_class_call_check(this, _class);
|
||||
_class_call_check(this, Baz);
|
||||
}
|
||||
}, exports.Quid = 2, module.exports = {
|
||||
Quack: 2
|
||||
|
@ -7,8 +7,9 @@ use swc_ecma_transforms_base::{helper, native::is_native, perf::Check};
|
||||
use swc_ecma_transforms_classes::super_field::SuperFieldAccessFolder;
|
||||
use swc_ecma_transforms_macros::fast_path;
|
||||
use swc_ecma_utils::{
|
||||
alias_if_required, default_constructor, prepend_stmt, private_ident, prop_name_to_expr,
|
||||
quote_expr, quote_ident, quote_str, ExprFactory, IsDirective, ModuleItemLike, StmtLike,
|
||||
alias_if_required, default_constructor, is_valid_prop_ident, prepend_stmt, private_ident,
|
||||
prop_name_to_expr, quote_expr, quote_ident, quote_str, ExprFactory, IdentExt, IsDirective,
|
||||
ModuleItemLike, StmtLike,
|
||||
};
|
||||
use swc_ecma_visit::{
|
||||
as_folder, noop_visit_mut_type, noop_visit_type, Fold, Visit, VisitMut, VisitMutWith, VisitWith,
|
||||
@ -260,17 +261,81 @@ where
|
||||
d.visit_mut_children_with(self)
|
||||
}
|
||||
|
||||
/// `let { f = class /* f */ {} } = {};`
|
||||
fn visit_mut_assign_pat_prop(&mut self, n: &mut AssignPatProp) {
|
||||
if let Some(value) = &mut n.value {
|
||||
if let Expr::Class(c @ ClassExpr { ident: None, .. }) = &mut **value {
|
||||
c.ident = Some(n.key.clone());
|
||||
}
|
||||
}
|
||||
|
||||
n.visit_mut_children_with(self);
|
||||
}
|
||||
|
||||
/// `let [c = class /* c */ {}] = [];`
|
||||
/// `function foo(bar = class /* bar */ {}) {}`
|
||||
fn visit_mut_assign_pat(&mut self, n: &mut AssignPat) {
|
||||
if let (
|
||||
Pat::Ident(BindingIdent { id, .. }),
|
||||
Expr::Class(c @ ClassExpr { ident: None, .. }),
|
||||
) = (&*n.left, &mut *n.right)
|
||||
{
|
||||
c.ident = Some(id.clone())
|
||||
}
|
||||
|
||||
n.visit_mut_children_with(self);
|
||||
}
|
||||
|
||||
/// {
|
||||
/// hello: class {},
|
||||
/// "foo": class {},
|
||||
/// ["x"]: class {}
|
||||
/// }
|
||||
fn visit_mut_key_value_prop(&mut self, n: &mut KeyValueProp) {
|
||||
if let Expr::Class(c @ ClassExpr { ident: None, .. }) = &mut *n.value {
|
||||
match &n.key {
|
||||
PropName::Ident(ident) => {
|
||||
c.ident = Some(ident.clone().private());
|
||||
}
|
||||
PropName::Str(Str { value, span, .. }) => {
|
||||
if is_valid_prop_ident(value) {
|
||||
c.ident = Some(private_ident!(*span, value));
|
||||
}
|
||||
}
|
||||
PropName::Computed(ComputedPropName { expr, .. }) => {
|
||||
if let Expr::Lit(Lit::Str(Str { value, span, .. })) = &**expr {
|
||||
if is_valid_prop_ident(value) {
|
||||
c.ident = Some(private_ident!(*span, value));
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
n.visit_mut_children_with(self)
|
||||
}
|
||||
|
||||
fn visit_mut_assign_expr(&mut self, a: &mut AssignExpr) {
|
||||
if let AssignExpr {
|
||||
op: op!("="),
|
||||
left: PatOrExpr::Pat(pat),
|
||||
op: op!("=") | op!("||=") | op!("??="),
|
||||
left,
|
||||
right,
|
||||
..
|
||||
} = a
|
||||
{
|
||||
if let Pat::Ident(i) = &**pat {
|
||||
if let Expr::Class(c @ ClassExpr { ident: None, .. }) = &mut **right {
|
||||
c.ident = Some(i.id.clone())
|
||||
if let Expr::Class(c @ ClassExpr { ident: None, .. }) = &mut **right {
|
||||
match left {
|
||||
PatOrExpr::Pat(pat) => {
|
||||
if let Pat::Ident(i) = &**pat {
|
||||
c.ident = Some(i.id.clone())
|
||||
}
|
||||
}
|
||||
PatOrExpr::Expr(expr) => {
|
||||
if let Expr::Ident(ident) = &**expr {
|
||||
c.ident = Some(ident.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -943,14 +943,14 @@ var x = {
|
||||
/*#__PURE__*/
|
||||
function (Foo) {
|
||||
"use strict";
|
||||
_inherits(_class, Foo);
|
||||
var _super = _createSuper(_class);
|
||||
function _class() {
|
||||
_classCallCheck(this, _class);
|
||||
_inherits(Foo1, Foo);
|
||||
var _super = _createSuper(Foo1);
|
||||
function Foo1() {
|
||||
_classCallCheck(this, Foo1);
|
||||
return _super.apply(this, arguments);
|
||||
}
|
||||
|
||||
return _class;
|
||||
return Foo1;
|
||||
}(Foo)
|
||||
};
|
||||
|
||||
@ -7356,6 +7356,8 @@ test!(
|
||||
r#"
|
||||
let C = class {}
|
||||
D = class {}
|
||||
C ||= class /* C */ {};
|
||||
D ??= class /* D */ {};
|
||||
"#,
|
||||
r#"
|
||||
var C = function C() {
|
||||
@ -7366,5 +7368,13 @@ D = function D() {
|
||||
"use strict";
|
||||
_classCallCheck(this, D);
|
||||
};
|
||||
C ||= function C() {
|
||||
"use strict";
|
||||
_classCallCheck(this, C);
|
||||
};
|
||||
D ??= function D() {
|
||||
"use strict";
|
||||
_classCallCheck(this, D);
|
||||
};
|
||||
"#
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user