Skip to content

Commit

Permalink
fix(es/compat): Handle classes with accessors and a method with the s…
Browse files Browse the repository at this point in the history
…ame name (#5039)
  • Loading branch information
Austaras committed Jun 28, 2022
1 parent 8bd3bef commit 25bd520
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 21 deletions.
17 changes: 14 additions & 3 deletions crates/swc_ecma_transforms_compat/src/es2015/classes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,9 +965,20 @@ where
method: None,
});
match m.kind {
MethodKind::Getter => data.get = Some(value),
MethodKind::Setter => data.set = Some(value),
MethodKind::Method => data.method = Some(value),
// https://github.com/swc-project/swc/issues/5029
MethodKind::Getter => {
data.method = None;
data.get = Some(value)
}
MethodKind::Setter => {
data.method = None;
data.set = Some(value)
}
MethodKind::Method => {
data.get = None;
data.set = None;
data.method = Some(value)
}
}
}

Expand Down
18 changes: 7 additions & 11 deletions crates/swc_ecma_transforms_compat/src/es2015/classes/prop_name.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
use num_bigint::BigInt as BigIntValue;
use ordered_float::OrderedFloat;
use swc_atoms::JsWord;
use swc_common::{Span, Spanned};
use swc_ecma_ast::*;

#[derive(Debug, PartialEq, Eq, Hash)]
pub enum HashKey {
Ident(JsWord),
Str(JsWord),
Num(OrderedFloat<f64>),
BigInt(BigIntValue),
/// Not for key merging
Computed(Span),
}

impl From<&PropName> for HashKey {
fn from(p: &PropName) -> Self {
match *p {
PropName::Ident(Ident { ref sym, .. }) => HashKey::Ident(sym.clone()),
PropName::Str(Str { ref value, .. }) => HashKey::Str(value.clone()),
PropName::Num(Number { value, .. }) => HashKey::Num(value.into()),
PropName::BigInt(BigInt { ref value, .. }) => HashKey::BigInt(value.clone()),
PropName::Computed(ref expr) => HashKey::Computed(expr.span()),
match p {
PropName::Ident(Ident { sym: value, .. }) | PropName::Str(Str { value, .. }) => {
HashKey::Str(value.clone())
}
PropName::Num(Number { value, .. }) => HashKey::Str(value.to_string().into()),
PropName::BigInt(BigInt { value, .. }) => HashKey::Str(value.to_string().into()),
PropName::Computed(expr) => HashKey::Computed(expr.span()),
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class A {
static b() {}
static get b() {}
static set b(b) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let A = /*#__PURE__*/ function() {
"use strict";
function A() {
_classCallCheck(this, A);
}
_createClass(A, null, [
{
key: "b",
get: function() {},
set: function(b) {}
}
]);
return A;
}();
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class A {
"b"() {}
get b() {}
set b(b) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let A = /*#__PURE__*/ function() {
"use strict";
function A() {
_classCallCheck(this, A);
}
_createClass(A, [
{
key: "b",
get: function() {},
set: function(b) {}
}
]);
return A;
}();
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class A {
get b() {}
set b(b) {}
b() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let A = /*#__PURE__*/ function() {
"use strict";
function A() {
_classCallCheck(this, A);
}
_createClass(A, [
{
key: "b",
value: function b() {}
}
]);
return A;
}();
7 changes: 0 additions & 7 deletions crates/swc_ecma_transforms_compat/tests/es2015_classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1902,9 +1902,6 @@ function Foo() {
_createClass(Foo, [{
key: "foo",
value: function foo() {}
}, {
key: "foo",
value: function foo() {}
}, {
key: bar,
value: function() {}
Expand Down Expand Up @@ -7231,10 +7228,6 @@ let Test = /*#__PURE__*/function (Foo) {
return _super.apply(this, arguments);
}
var _proto = Test.prototype;
_proto["foo"] = function foo() {};
_createClass(Test, [
{
key: "foo",
Expand Down

1 comment on commit 25bd520

@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: 25bd520 Previous: 376c4e1 Ratio
es/full/minify/libraries/antd 1660157241 ns/iter (± 29445587) 1650352318 ns/iter (± 22405676) 1.01
es/full/minify/libraries/d3 432446991 ns/iter (± 9623092) 410692002 ns/iter (± 5985850) 1.05
es/full/minify/libraries/echarts 1640863988 ns/iter (± 59944311) 1620558786 ns/iter (± 16520914) 1.01
es/full/minify/libraries/jquery 97162700 ns/iter (± 9486253) 91234715 ns/iter (± 1710878) 1.06
es/full/minify/libraries/lodash 129508882 ns/iter (± 7671518) 120218577 ns/iter (± 2246002) 1.08
es/full/minify/libraries/moment 50736458 ns/iter (± 2014851) 50225925 ns/iter (± 599070) 1.01
es/full/minify/libraries/react 17556311 ns/iter (± 422645) 16913175 ns/iter (± 232698) 1.04
es/full/minify/libraries/terser 605230980 ns/iter (± 12204837) 600727821 ns/iter (± 14201905) 1.01
es/full/minify/libraries/three 546272389 ns/iter (± 7194303) 544133444 ns/iter (± 8020000) 1.00
es/full/minify/libraries/typescript 3546859049 ns/iter (± 112292243) 3494031014 ns/iter (± 78102606) 1.02
es/full/minify/libraries/victory 744850007 ns/iter (± 27294268) 704360073 ns/iter (± 12178621) 1.06
es/full/minify/libraries/vue 141427411 ns/iter (± 10246009) 131353871 ns/iter (± 4147410) 1.08
es/full/codegen/es3 32307 ns/iter (± 959) 32769 ns/iter (± 974) 0.99
es/full/codegen/es5 32290 ns/iter (± 399) 32718 ns/iter (± 1531) 0.99
es/full/codegen/es2015 32177 ns/iter (± 288) 32563 ns/iter (± 288) 0.99
es/full/codegen/es2016 32262 ns/iter (± 642) 32805 ns/iter (± 288) 0.98
es/full/codegen/es2017 32113 ns/iter (± 753) 32872 ns/iter (± 423) 0.98
es/full/codegen/es2018 32104 ns/iter (± 981) 32883 ns/iter (± 843) 0.98
es/full/codegen/es2019 32358 ns/iter (± 257) 32911 ns/iter (± 1230) 0.98
es/full/codegen/es2020 32363 ns/iter (± 1284) 32866 ns/iter (± 1283) 0.98
es/full/all/es3 185492468 ns/iter (± 9634017) 182622275 ns/iter (± 6364872) 1.02
es/full/all/es5 175228710 ns/iter (± 15617929) 171188778 ns/iter (± 6014477) 1.02
es/full/all/es2015 140694418 ns/iter (± 9049913) 143054436 ns/iter (± 4896559) 0.98
es/full/all/es2016 139315801 ns/iter (± 7565191) 142916735 ns/iter (± 3006689) 0.97
es/full/all/es2017 138056332 ns/iter (± 5650379) 140541203 ns/iter (± 3354802) 0.98
es/full/all/es2018 135434615 ns/iter (± 6908263) 139622879 ns/iter (± 6782736) 0.97
es/full/all/es2019 147058509 ns/iter (± 9486317) 138937121 ns/iter (± 4600810) 1.06
es/full/all/es2020 138386534 ns/iter (± 6084780) 133158568 ns/iter (± 3926862) 1.04
es/full/parser 695481 ns/iter (± 21226) 705087 ns/iter (± 18735) 0.99
es/full/base/fixer 29172 ns/iter (± 383) 29199 ns/iter (± 1323) 1.00
es/full/base/resolver_and_hygiene 85995 ns/iter (± 1688) 87409 ns/iter (± 3800) 0.98
serialization of ast node 207 ns/iter (± 2) 206 ns/iter (± 9) 1.00
serialization of serde 217 ns/iter (± 4) 225 ns/iter (± 3) 0.96

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

Please sign in to comment.