Skip to content

Commit

Permalink
fix(es/compat): Fix super method call in loose mode (#6207)
Browse files Browse the repository at this point in the history
  • Loading branch information
Austaras committed Oct 20, 2022
1 parent 62a9405 commit 0e2de30
Show file tree
Hide file tree
Showing 3 changed files with 70 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
49 changes: 38 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 @@ -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()
"
);
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

1 comment on commit 0e2de30

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 0e2de30 Previous: 1b0dad1 Ratio
es/full/bugs-1 345368 ns/iter (± 20236) 341182 ns/iter (± 33524) 1.01
es/full/minify/libraries/antd 1817098823 ns/iter (± 51093946) 1805592024 ns/iter (± 23178570) 1.01
es/full/minify/libraries/d3 390257175 ns/iter (± 11258056) 385007305 ns/iter (± 7726408) 1.01
es/full/minify/libraries/echarts 1526384465 ns/iter (± 14351208) 1521509326 ns/iter (± 32121559) 1.00
es/full/minify/libraries/jquery 102279282 ns/iter (± 2020688) 101439439 ns/iter (± 4820874) 1.01
es/full/minify/libraries/lodash 114181448 ns/iter (± 3530208) 112006175 ns/iter (± 6841303) 1.02
es/full/minify/libraries/moment 59785773 ns/iter (± 1514045) 58118957 ns/iter (± 543713) 1.03
es/full/minify/libraries/react 20849864 ns/iter (± 1211373) 21027227 ns/iter (± 940693) 0.99
es/full/minify/libraries/terser 314403919 ns/iter (± 8910988) 304582403 ns/iter (± 14793283) 1.03
es/full/minify/libraries/three 556642513 ns/iter (± 31642549) 551495282 ns/iter (± 28176431) 1.01
es/full/minify/libraries/typescript 3389155519 ns/iter (± 39664121) 3429272507 ns/iter (± 104250035) 0.99
es/full/minify/libraries/victory 810488954 ns/iter (± 19435529) 822865187 ns/iter (± 13704796) 0.98
es/full/minify/libraries/vue 142661260 ns/iter (± 7055152) 153867437 ns/iter (± 8611442) 0.93
es/full/codegen/es3 32904 ns/iter (± 723) 34215 ns/iter (± 963) 0.96
es/full/codegen/es5 33312 ns/iter (± 1220) 34395 ns/iter (± 1137) 0.97
es/full/codegen/es2015 33290 ns/iter (± 356) 34448 ns/iter (± 1302) 0.97
es/full/codegen/es2016 33279 ns/iter (± 300) 34316 ns/iter (± 1599) 0.97
es/full/codegen/es2017 33282 ns/iter (± 604) 34217 ns/iter (± 1516) 0.97
es/full/codegen/es2018 33276 ns/iter (± 3275) 34283 ns/iter (± 1693) 0.97
es/full/codegen/es2019 33335 ns/iter (± 3840) 34285 ns/iter (± 699) 0.97
es/full/codegen/es2020 33346 ns/iter (± 2266) 34458 ns/iter (± 1474) 0.97
es/full/all/es3 194466665 ns/iter (± 16552867) 209533136 ns/iter (± 13014947) 0.93
es/full/all/es5 199597454 ns/iter (± 22177848) 190276806 ns/iter (± 10111348) 1.05
es/full/all/es2015 157314062 ns/iter (± 728489715) 150762636 ns/iter (± 4710106) 1.04
es/full/all/es2016 160477349 ns/iter (± 15304870) 158595974 ns/iter (± 12750011) 1.01
es/full/all/es2017 158398985 ns/iter (± 22304138) 169869646 ns/iter (± 29047393) 0.93
es/full/all/es2018 174304095 ns/iter (± 14026404) 166886979 ns/iter (± 14068534) 1.04
es/full/all/es2019 167647697 ns/iter (± 18086250) 148368909 ns/iter (± 6858116) 1.13
es/full/all/es2020 149216086 ns/iter (± 34088132) 143230552 ns/iter (± 10476460) 1.04
es/full/parser 725197 ns/iter (± 157207) 747540 ns/iter (± 25565) 0.97
es/full/base/fixer 26296 ns/iter (± 876) 26603 ns/iter (± 1500) 0.99
es/full/base/resolver_and_hygiene 92801 ns/iter (± 2955) 92782 ns/iter (± 2254) 1.00
serialization of ast node 214 ns/iter (± 84) 215 ns/iter (± 1) 1.00
serialization of serde 219 ns/iter (± 9) 223 ns/iter (± 17) 0.98

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.