Skip to content

Commit

Permalink
fix(es/compat): Super method call in loose mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Austaras committed Oct 19, 2022
1 parent 6008995 commit cb5b587
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 22 deletions.
39 changes: 30 additions & 9 deletions crates/swc_ecma_transforms_classes/src/super_field.rs
Expand Up @@ -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(
Expand Down
14 changes: 14 additions & 0 deletions 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();
22 changes: 11 additions & 11 deletions crates/swc_ecma_transforms_compat/tests/es2015_classes.rs
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -6912,17 +6912,17 @@ 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;
}
_createClass(Test, null, [{
key: "test",
value: function test() {
return _get(Test, "wow", this).call(this);
return Foo.wow.call(this);
}
}]);
return Test;
Expand Down Expand Up @@ -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;
Expand Down
Expand Up @@ -6023,7 +6023,7 @@ var _B;
class A extends (_B = class B {}) {}
_defineProperty(A, "x", _get(A, "x", A));
_defineProperty(A, "x", _B.x);
"#
);

Expand Down Expand Up @@ -6051,7 +6051,7 @@ class A extends B {
}
}
_defineProperty(A, "foo", _get(A, "bar", A));
_defineProperty(A, "foo", B.bar);
"#
);

Expand Down

0 comments on commit cb5b587

Please sign in to comment.