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): Super method call in loose mode #6207

Merged
merged 2 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 30 additions & 9 deletions crates/swc_ecma_transforms_classes/src/super_field.rs
Original file line number Diff line number Diff line change
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
49 changes: 38 additions & 11 deletions crates/swc_ecma_transforms_compat/tests/es2015_classes.rs
Original file line number Diff line number Diff line change
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 @@ -7373,3 +7373,30 @@ D ??= function D() {
};
"#
);

test_exec!(
syntax(),
|t| classes(
Some(t.comments.clone()),
Config {
constant_super: true,
..Default::default()
}
),
issue_5936,
"
class Superclass {
doStuff() {
}
}

class Subclass extends Superclass {
doStuff() {
console.log('hola');
super.doStuff();
}
}

expect(() => new Subclass().doStuff()).not.toThrowError()
"
);
Original file line number Diff line number Diff line change
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