Skip to content

Commit

Permalink
fix(es/compat): Handle __proto__ edge case in shorthand pass (#8077)
Browse files Browse the repository at this point in the history
**Related issue:**

 - babel/babel#12664
  • Loading branch information
magic-akari authored Oct 10, 2023
1 parent ed64833 commit a912937
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 76 deletions.
106 changes: 30 additions & 76 deletions crates/swc_ecma_transforms_compat/src/es2015/shorthand_property.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use swc_common::util::take::Take;
use swc_ecma_ast::*;
use swc_ecma_transforms_base::perf::Parallel;
use swc_ecma_utils::quote_ident;
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};
use swc_trace_macro::swc_trace;

Expand Down Expand Up @@ -62,15 +61,41 @@ impl VisitMut for Shorthand {
prop.visit_mut_children_with(self);

match prop {
Prop::Shorthand(Ident { sym, span, .. }) => {
Prop::Shorthand(ident) => {
let value = ident.clone().into();

*prop = Prop::KeyValue(KeyValueProp {
key: PropName::Ident(quote_ident!(*span, sym.clone())),
value: Box::new(quote_ident!(*span, sym.clone()).into()),
key: if ident.sym == "__proto__" {
PropName::Computed(ComputedPropName {
span: ident.span,
expr: ident.sym.clone().into(),
})
} else {
ident.take().into()
},
value,
});
}
Prop::Method(MethodProp { key, function }) => {
let key = match key.take() {
PropName::Ident(Ident { span, sym, .. }) if sym == "__proto__" => {
ComputedPropName {
span,
expr: sym.into(),
}
.into()
}
PropName::Str(s @ Str { span, .. }) if s.value == "__proto__" => {
ComputedPropName {
span,
expr: s.into(),
}
.into()
}
key => key,
};
*prop = Prop::KeyValue(KeyValueProp {
key: key.take(),
key,
value: Box::new(Expr::Fn(FnExpr {
ident: None,
function: function.take(),
Expand All @@ -81,74 +106,3 @@ impl VisitMut for Shorthand {
}
}
}

#[cfg(test)]
mod tests {
use swc_ecma_transforms_testing::test;

use super::*;

test!(
::swc_ecma_parser::Syntax::default(),
|_| shorthand(),
babel_method_plain,
"var obj = {
method() {
return 5 + 5;
}
};",
"var obj = {
method: function () {
return 5 + 5;
}
};"
);

test!(
::swc_ecma_parser::Syntax::default(),
|_| shorthand(),
babel_comments,
"var A = 'a';
var o = {
A // comment
};",
"var A = 'a';
var o = {
A: A // comment
};"
);

test!(
::swc_ecma_parser::Syntax::default(),
|_| shorthand(),
babel_mixed,
"var coords = { x, y, foo: 'bar' };",
"var coords = {
x: x,
y: y,
foo: 'bar'
};"
);

test!(
::swc_ecma_parser::Syntax::default(),
|_| shorthand(),
babel_multiple,
"var coords = { x, y };",
"var coords = {
x: x,
y: y
};"
);

test!(
::swc_ecma_parser::Syntax::default(),
|_| shorthand(),
babel_single,
"var coords = { x };",
"var coords = {
x: x
};"
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::path::PathBuf;

use swc_common::{chain, Mark};
use swc_ecma_transforms_base::resolver;
use swc_ecma_transforms_compat::es2015::shorthand;
use swc_ecma_transforms_testing::test_fixture;

#[testing::fixture("tests/shorthand_properties/**/input.js")]
fn fixture(input: PathBuf) {
let output = input.with_file_name("output.js");

test_fixture(
Default::default(),
&|_| {
let unresolved_mark = Mark::new();
chain!(resolver(unresolved_mark, Mark::new(), false), shorthand())
},
&input,
&output,
Default::default(),
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @flow
var obj = {
method(a: string): number {
return 5 + 5;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @flow
var obj = {
method: function (a: string): number {
return 5 + 5;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var obj = {
method() {
return 5 + 5;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var obj = {
method: function () {
return 5 + 5;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var shorthand = {
__proto__,
}

var method = {
__proto__() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var shorthand = {
["__proto__"]: __proto__
};
var method = {
["__proto__"]: function () {}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var A = "a";
var o = {
A // comment
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var A = "a";
var o = {
A: A // comment
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var coords = { x, y, foo: "bar" };
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var coords = {
x: x,
y: y,
foo: "bar"
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var coords = { x, y };
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var coords = {
x: x,
y: y
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var coords = { x };
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var coords = {
x: x
};

1 comment on commit a912937

@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: a912937 Previous: 7073e83 Ratio
es/full/bugs-1 288912 ns/iter (± 6903) 283859 ns/iter (± 5198) 1.02
es/full/minify/libraries/antd 1418839549 ns/iter (± 16816020) 1336899744 ns/iter (± 8418434) 1.06
es/full/minify/libraries/d3 298145181 ns/iter (± 3182837) 291136349 ns/iter (± 2826509) 1.02
es/full/minify/libraries/echarts 1140048154 ns/iter (± 16732217) 1082305373 ns/iter (± 4216008) 1.05
es/full/minify/libraries/jquery 89077699 ns/iter (± 874779) 87832596 ns/iter (± 170685) 1.01
es/full/minify/libraries/lodash 103596015 ns/iter (± 858063) 102286359 ns/iter (± 753320) 1.01
es/full/minify/libraries/moment 52421188 ns/iter (± 229780) 51871807 ns/iter (± 207893) 1.01
es/full/minify/libraries/react 18792450 ns/iter (± 56670) 18695417 ns/iter (± 92713) 1.01
es/full/minify/libraries/terser 231040948 ns/iter (± 1804258) 227396645 ns/iter (± 2358048) 1.02
es/full/minify/libraries/three 413091017 ns/iter (± 3028383) 399928348 ns/iter (± 2920127) 1.03
es/full/minify/libraries/typescript 2852726572 ns/iter (± 21325554) 2716173899 ns/iter (± 11057911) 1.05
es/full/minify/libraries/victory 616232395 ns/iter (± 5986436) 581697770 ns/iter (± 5536501) 1.06
es/full/minify/libraries/vue 127638514 ns/iter (± 1833582) 124649384 ns/iter (± 358184) 1.02
es/full/codegen/es3 34187 ns/iter (± 80) 33751 ns/iter (± 98) 1.01
es/full/codegen/es5 34181 ns/iter (± 74) 33776 ns/iter (± 123) 1.01
es/full/codegen/es2015 34288 ns/iter (± 61) 33855 ns/iter (± 112) 1.01
es/full/codegen/es2016 34253 ns/iter (± 161) 33862 ns/iter (± 122) 1.01
es/full/codegen/es2017 34209 ns/iter (± 123) 33904 ns/iter (± 95) 1.01
es/full/codegen/es2018 34223 ns/iter (± 58) 33810 ns/iter (± 109) 1.01
es/full/codegen/es2019 34207 ns/iter (± 283) 33723 ns/iter (± 104) 1.01
es/full/codegen/es2020 34176 ns/iter (± 94) 33693 ns/iter (± 94) 1.01
es/full/all/es3 176962505 ns/iter (± 839106) 176662543 ns/iter (± 1166046) 1.00
es/full/all/es5 169354009 ns/iter (± 1373011) 169575561 ns/iter (± 1173449) 1.00
es/full/all/es2015 128041413 ns/iter (± 1333296) 128765636 ns/iter (± 989201) 0.99
es/full/all/es2016 128136684 ns/iter (± 1048898) 128022097 ns/iter (± 818210) 1.00
es/full/all/es2017 127659000 ns/iter (± 1031554) 127640494 ns/iter (± 2910808) 1.00
es/full/all/es2018 125583867 ns/iter (± 1362918) 125620409 ns/iter (± 1131352) 1.00
es/full/all/es2019 124405072 ns/iter (± 705511) 124259384 ns/iter (± 710201) 1.00
es/full/all/es2020 120956954 ns/iter (± 720919) 120242153 ns/iter (± 1278410) 1.01
es/full/parser 566851 ns/iter (± 3523) 557701 ns/iter (± 3738) 1.02
es/full/base/fixer 18242 ns/iter (± 112) 19386 ns/iter (± 219) 0.94
es/full/base/resolver_and_hygiene 84320 ns/iter (± 213) 84184 ns/iter (± 232) 1.00

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

Please sign in to comment.