diff --git a/crates/swc_ecma_transforms_compat/src/es2015/shorthand_property.rs b/crates/swc_ecma_transforms_compat/src/es2015/shorthand_property.rs index 57882a47b0e..1d3106aab5a 100644 --- a/crates/swc_ecma_transforms_compat/src/es2015/shorthand_property.rs +++ b/crates/swc_ecma_transforms_compat/src/es2015/shorthand_property.rs @@ -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; @@ -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(), @@ -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 -};" - ); -} diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_shorthand_propertie.rs b/crates/swc_ecma_transforms_compat/tests/es2015_shorthand_propertie.rs new file mode 100644 index 00000000000..d2a49cc38e5 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/es2015_shorthand_propertie.rs @@ -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(), + ); +} diff --git a/crates/swc_ecma_transforms_compat/tests/shorthand_properties/.method-type-annotations/input.js b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/.method-type-annotations/input.js new file mode 100644 index 00000000000..b6ce120afd8 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/.method-type-annotations/input.js @@ -0,0 +1,6 @@ +// @flow +var obj = { + method(a: string): number { + return 5 + 5; + } +}; diff --git a/crates/swc_ecma_transforms_compat/tests/shorthand_properties/.method-type-annotations/output.js b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/.method-type-annotations/output.js new file mode 100644 index 00000000000..cfd883f5b17 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/.method-type-annotations/output.js @@ -0,0 +1,6 @@ +// @flow +var obj = { + method: function (a: string): number { + return 5 + 5; + } +}; diff --git a/crates/swc_ecma_transforms_compat/tests/shorthand_properties/method-plain/input.js b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/method-plain/input.js new file mode 100644 index 00000000000..9114ccf44c6 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/method-plain/input.js @@ -0,0 +1,5 @@ +var obj = { + method() { + return 5 + 5; + } +}; diff --git a/crates/swc_ecma_transforms_compat/tests/shorthand_properties/method-plain/output.js b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/method-plain/output.js new file mode 100644 index 00000000000..874b21c6b91 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/method-plain/output.js @@ -0,0 +1,5 @@ +var obj = { + method: function () { + return 5 + 5; + } +}; diff --git a/crates/swc_ecma_transforms_compat/tests/shorthand_properties/proto/input.js b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/proto/input.js new file mode 100644 index 00000000000..f0389cab128 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/proto/input.js @@ -0,0 +1,7 @@ +var shorthand = { + __proto__, +} + +var method = { + __proto__() {} +} diff --git a/crates/swc_ecma_transforms_compat/tests/shorthand_properties/proto/output.js b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/proto/output.js new file mode 100644 index 00000000000..252e4cac7a2 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/proto/output.js @@ -0,0 +1,6 @@ +var shorthand = { + ["__proto__"]: __proto__ +}; +var method = { + ["__proto__"]: function () {} +}; diff --git a/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-comments/input.js b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-comments/input.js new file mode 100644 index 00000000000..26d4bae5c90 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-comments/input.js @@ -0,0 +1,4 @@ +var A = "a"; +var o = { + A // comment +}; diff --git a/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-comments/output.js b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-comments/output.js new file mode 100644 index 00000000000..c5040d1fb3e --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-comments/output.js @@ -0,0 +1,4 @@ +var A = "a"; +var o = { + A: A // comment +}; diff --git a/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-mixed/input.js b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-mixed/input.js new file mode 100644 index 00000000000..c2c49e02538 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-mixed/input.js @@ -0,0 +1 @@ +var coords = { x, y, foo: "bar" }; diff --git a/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-mixed/output.js b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-mixed/output.js new file mode 100644 index 00000000000..88427e8bfde --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-mixed/output.js @@ -0,0 +1,5 @@ +var coords = { + x: x, + y: y, + foo: "bar" +}; diff --git a/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-multiple/input.js b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-multiple/input.js new file mode 100644 index 00000000000..224e1f6c7b8 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-multiple/input.js @@ -0,0 +1 @@ +var coords = { x, y }; diff --git a/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-multiple/output.js b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-multiple/output.js new file mode 100644 index 00000000000..e07257b750a --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-multiple/output.js @@ -0,0 +1,4 @@ +var coords = { + x: x, + y: y +}; diff --git a/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-single/input.js b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-single/input.js new file mode 100644 index 00000000000..8cd07504743 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-single/input.js @@ -0,0 +1 @@ +var coords = { x }; diff --git a/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-single/output.js b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-single/output.js new file mode 100644 index 00000000000..9b14ab0e304 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/shorthand_properties/shorthand-single/output.js @@ -0,0 +1,3 @@ +var coords = { + x: x +};