diff --git a/crates/swc_ecma_transforms_classes/src/super_field.rs b/crates/swc_ecma_transforms_classes/src/super_field.rs index 0d49fa7575ec..ffa8aa2095e4 100644 --- a/crates/swc_ecma_transforms_classes/src/super_field.rs +++ b/crates/swc_ecma_transforms_classes/src/super_field.rs @@ -353,18 +353,39 @@ impl<'a> SuperFieldAccessFolder<'a> { } fn super_to_get_call(&mut self, super_token: Span, prop: SuperProp) -> Expr { - let proto_arg = self.proto_arg(); + if self.constant_super { + Expr::Member(MemberExpr { + span: super_token, + obj: Box::new({ + let name = self.super_class.clone().unwrap_or_else(|| { + quote_ident!(if self.is_static { "Function" } else { "Object" }) + }); + // in static default super class is Function.prototype + if self.is_static && self.super_class.is_some() { + Expr::Ident(name) + } else { + name.make_member(quote_ident!("prototype")) + } + }), + prop: match prop { + SuperProp::Ident(i) => MemberProp::Ident(i), + SuperProp::Computed(c) => MemberProp::Computed(c), + }, + }) + } else { + let proto_arg = self.proto_arg(); - let prop_arg = prop_arg(prop).as_arg(); + let prop_arg = prop_arg(prop).as_arg(); - let this_arg = self.this_arg(super_token).as_arg(); + let this_arg = self.this_arg(super_token).as_arg(); - Expr::Call(CallExpr { - span: super_token, - callee: helper!(get, "get"), - args: vec![proto_arg.as_arg(), prop_arg, this_arg], - type_args: Default::default(), - }) + Expr::Call(CallExpr { + span: super_token, + callee: helper!(get, "get"), + args: vec![proto_arg.as_arg(), prop_arg, this_arg], + type_args: Default::default(), + }) + } } fn super_to_set_call( diff --git a/crates/swc_ecma_transforms_compat/tests/classes/issue-5936/exec.js b/crates/swc_ecma_transforms_compat/tests/classes/issue-5936/exec.js new file mode 100644 index 000000000000..a327dd76377d --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/classes/issue-5936/exec.js @@ -0,0 +1,14 @@ +let a = 0; + +class Superclass { + doStuff() {} +} + +class Subclass extends Superclass { + doStuff() { + console.log("hola"); + super.doStuff(); + } +} + +new Subclass().doStuff(); diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_classes.rs b/crates/swc_ecma_transforms_compat/tests/es2015_classes.rs index adb885e5f34c..a35c0caa86d7 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2015_classes.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2015_classes.rs @@ -6824,11 +6824,11 @@ function Test() { var _this; woops.super.test(); _this = _super.call(this); - _get(Test.prototype, "test", _this).call(_assertThisInitialized(_this)); + Foo.prototype.test.call(_assertThisInitialized(_this)); _this = _super.call(this, ...arguments); _this = _super.call(this, "test", ...arguments); - _get(Test.prototype, "test", _this).apply(_assertThisInitialized(_this), arguments); - _get(Test.prototype, "test", _this).call(_assertThisInitialized(_this), "test", ...arguments); + Foo.prototype.test.apply(_assertThisInitialized(_this), arguments); + Foo.prototype.test.call(_assertThisInitialized(_this), "test", ...arguments); return _this; } @@ -6867,8 +6867,8 @@ let Test = /*#__PURE__*/function (Foo) { function Test() { _classCallCheck(this, Test); var _this = _super.call(this); - _get(Test.prototype, "test", _this); - _get(Test.prototype, "test", _this).whatever; + Foo.prototype.test; + Foo.prototype.test.whatever; return _this; } @@ -6912,9 +6912,9 @@ let Test = /*#__PURE__*/function (Foo) { _classCallCheck(this, Test); var _this = _super.call(this); - _get(Test.prototype, "test", _this).whatever(); + Foo.prototype.test.whatever(); - _get(Test.prototype, "test", _this).call(_assertThisInitialized(_this)); + Foo.prototype.test.call(_assertThisInitialized(_this)); return _this; } @@ -6922,7 +6922,7 @@ let Test = /*#__PURE__*/function (Foo) { _createClass(Test, null, [{ key: "test", value: function test() { - return _get(Test, "wow", this).call(this); + return Foo.wow.call(this); } }]); return Test; @@ -6958,14 +6958,14 @@ let Test = /*#__PURE__*/function () { function Test() { _classCallCheck(this, Test); - _get(Test.prototype, "hasOwnProperty", this).call(this, "test"); - return _get(Test.prototype, "constructor", this); + Object.prototype.hasOwnProperty.call(this, "test"); + return Object.prototype.constructor; } _createClass(Test, null, [{ key: "test", value: function test() { - return _get(Test, "constructor", this); + return Function.prototype.constructor; } }]); return Test; diff --git a/crates/swc_ecma_transforms_compat/tests/es2022_class_properties.rs b/crates/swc_ecma_transforms_compat/tests/es2022_class_properties.rs index 9afd597dff44..dea265cb1d33 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2022_class_properties.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2022_class_properties.rs @@ -6023,7 +6023,7 @@ var _B; class A extends (_B = class B {}) {} -_defineProperty(A, "x", _get(A, "x", A)); +_defineProperty(A, "x", _B.x); "# ); @@ -6051,7 +6051,7 @@ class A extends B { } } -_defineProperty(A, "foo", _get(A, "bar", A)); +_defineProperty(A, "foo", B.bar); "# );