fix(es/compat): Handle __proto__ edge case in shorthand pass (#8077)

**Related issue:**

 - https://github.com/babel/babel/pull/12664
This commit is contained in:
magic-akari 2023-10-09 23:08:45 -05:00 committed by GitHub
parent ed648337f1
commit a912937cea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 110 additions and 76 deletions

View File

@ -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
};"
);
}

View File

@ -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(),
);
}

View File

@ -0,0 +1,6 @@
// @flow
var obj = {
method(a: string): number {
return 5 + 5;
}
};

View File

@ -0,0 +1,6 @@
// @flow
var obj = {
method: function (a: string): number {
return 5 + 5;
}
};

View File

@ -0,0 +1,5 @@
var obj = {
method() {
return 5 + 5;
}
};

View File

@ -0,0 +1,5 @@
var obj = {
method: function () {
return 5 + 5;
}
};

View File

@ -0,0 +1,7 @@
var shorthand = {
__proto__,
}
var method = {
__proto__() {}
}

View File

@ -0,0 +1,6 @@
var shorthand = {
["__proto__"]: __proto__
};
var method = {
["__proto__"]: function () {}
};

View File

@ -0,0 +1,4 @@
var A = "a";
var o = {
A // comment
};

View File

@ -0,0 +1,4 @@
var A = "a";
var o = {
A: A // comment
};

View File

@ -0,0 +1 @@
var coords = { x, y, foo: "bar" };

View File

@ -0,0 +1,5 @@
var coords = {
x: x,
y: y,
foo: "bar"
};

View File

@ -0,0 +1 @@
var coords = { x, y };

View File

@ -0,0 +1,4 @@
var coords = {
x: x,
y: y
};

View File

@ -0,0 +1 @@
var coords = { x };

View File

@ -0,0 +1,3 @@
var coords = {
x: x
};