Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/ast): Fix definition of SetterProp #8314

Merged
merged 26 commits into from
Jan 19, 2024
Merged
5 changes: 2 additions & 3 deletions crates/swc_ecma_ast/src/prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ use crate::{
function::Function,
ident::Ident,
lit::{BigInt, Number, Str},
pat::Pat,
stmt::BlockStmt,
typescript::TsTypeAnn,
Id, MemberProp,
Id, MemberProp, Param,
};

#[ast_node]
Expand Down Expand Up @@ -76,7 +75,7 @@ pub struct GetterProp {
pub struct SetterProp {
pub span: Span,
pub key: PropName,
pub param: Box<Pat>,
pub params: Vec<Param>,
kdy1 marked this conversation as resolved.
Show resolved Hide resolved
kdy1 marked this conversation as resolved.
Show resolved Hide resolved
#[cfg_attr(feature = "serde-impl", serde(default))]
pub body: Option<BlockStmt>,
}
Expand Down
6 changes: 5 additions & 1 deletion crates/swc_ecma_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2112,7 +2112,11 @@ where
formatting_space!();

punct!("(");
emit!(node.param);
self.emit_list(
node.span(),
Some(&node.params),
ListFormat::CommaListElements,
)?;
punct!(")");

emit!(node.body);
Expand Down
12 changes: 3 additions & 9 deletions crates/swc_ecma_compat_common/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,11 @@ macro_rules! impl_visit_mut_fn {

f.visit_mut_children_with(self);

let (mut params, body) = self.visit_mut_fn_like(
&mut vec![Param {
span: DUMMY_SP,
decorators: Default::default(),
pat: *f.param.take(),
}],
&mut f.body.take().unwrap(),
);
let (mut params, body) =
self.visit_mut_fn_like(&mut f.params, &mut f.body.take().unwrap());
debug_assert!(params.len() == 1);

f.param = Box::new(params.pop().unwrap().pat);
f.params = params;
f.body = Some(body);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_compat_es2015/src/block_scoping/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ impl VisitMut for BlockScoping {

fn visit_mut_setter_prop(&mut self, f: &mut SetterProp) {
f.key.visit_mut_with(self);
f.param.visit_mut_with(self);
f.params.visit_mut_with(self);
self.visit_mut_with_scope(ScopeKind::Fn, &mut f.body);
}

Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_compat_es2015/src/computed_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl VisitMut for ComputedProps {
Prop::Setter(SetterProp {
span,
body,
param,
params,
key,
}) => (
key,
Expand All @@ -179,7 +179,7 @@ impl VisitMut for ComputedProps {
body,
is_async: false,
is_generator: false,
params: vec![(*param).into()],
params,
decorators: Default::default(),
type_params: Default::default(),
return_type: Default::default(),
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_compat_es2015/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ impl VisitMut for Generator {
fn visit_mut_getter_prop(&mut self, _: &mut GetterProp) {}

fn visit_mut_setter_prop(&mut self, e: &mut SetterProp) {
e.param.visit_mut_with(self);
e.params.visit_mut_with(self);
}

fn visit_mut_expr(&mut self, e: &mut Expr) {
Expand Down Expand Up @@ -1290,7 +1290,7 @@ impl Generator {
KeyValueProp {
key: quote_ident!("set").into(),
value: Function {
params: vec![(*s.param).into()],
params: s.params,
decorators: Default::default(),
span: s.span,
body: s.body,
Expand Down
8 changes: 2 additions & 6 deletions crates/swc_ecma_compat_es2015/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,18 +717,14 @@ impl VisitMut for Params {

f.visit_mut_children_with(self);

let mut params = vec![Param {
span: DUMMY_SP,
decorators: Default::default(),
pat: *f.param.take(),
}];
let mut params = f.params.take();

let mut body = f.body.take().unwrap();
self.visit_mut_fn_like(&mut params, &mut body, true);

debug_assert!(params.len() == 1);

f.param = Box::new(params.pop().unwrap().pat);
f.params = params;
f.body = Some(body);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_compat_es2022/src/class_properties/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,7 @@ impl Visit for SuperVisitor {
/// Don't recurse into fn
fn visit_setter_prop(&mut self, n: &SetterProp) {
n.key.visit_with(self);
n.param.visit_with(self);
n.params.visit_with(self);
}

fn visit_super(&mut self, _: &Super) {
Expand Down
31 changes: 9 additions & 22 deletions crates/swc_ecma_parser/src/parser/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,28 +351,15 @@ impl<I: Tokens> ParseObject<Box<Expr>> for Parser<I> {
false,
)
.map(|v| *v)
.map(
|Function {
mut params, body, ..
}| {
params.retain(|p| match &p.pat {
Pat::Ident(p) => p.sym != "this",
_ => true,
});
Comment on lines -358 to -361
Copy link
Member Author

@kdy1 kdy1 Nov 21, 2023

Choose a reason for hiding this comment

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

Removal of this code is the fix of this PR.


// debug_assert_eq!(params.len(), 1);
PropOrSpread::Prop(Box::new(Prop::Setter(SetterProp {
span: span!(parser, start),
key,
body,
param: Box::new(
params.into_iter().map(|p| p.pat).next().unwrap_or(
Pat::Invalid(Invalid { span: key_span }),
),
),
})))
},
)
.map(|Function { params, body, .. }| {
// debug_assert_eq!(params.len(), 1);
PropOrSpread::Prop(Box::new(Prop::Setter(SetterProp {
span: span!(parser, start),
key,
body,
params,
})))
})
}
"async" => parser
.parse_fn_args_body(
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_quote_macros/src/ast/prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl_struct!(KeyValueProp, [key, value]);
impl_struct!(AssignProp, [key, value]);

impl_struct!(GetterProp, [span, key, type_ann, body]);
impl_struct!(SetterProp, [span, key, param, body]);
impl_struct!(SetterProp, [span, key, params, body]);

impl_struct!(MethodProp, [key, function]);

Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_transforms_base/src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ impl<'a> VisitMut for Resolver<'a> {
{
self.with_child(ScopeKind::Fn, |child| {
child.ident_type = IdentType::Binding;
n.param.visit_mut_with(child);
n.params.visit_mut_with(child);
n.body.visit_mut_with(child);
});
};
Expand Down
7 changes: 5 additions & 2 deletions crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ where
in_pat_of_param: true,
..a.ctx
};
n.param.visit_with(&mut *a.with_ctx(ctx));
n.params.visit_with(&mut *a.with_ctx(ctx));
}

n.body.visit_with(a);
Expand Down Expand Up @@ -1379,7 +1379,10 @@ fn for_each_id_ref_in_expr(e: &Expr, op: &mut impl FnMut(&Ident)) {
}
Prop::Setter(p) => {
for_each_id_ref_in_prop_name(&p.key, op);
for_each_id_ref_in_pat(&p.param, op);

for p in &p.params {
for_each_id_ref_in_pat(&p.pat, op);
}
}
Prop::Method(p) => {
for_each_id_ref_in_fn(&p.function, op);
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_utils/src/function/fn_env_hoister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ fn extend_super(
Prop::Setter(SetterProp {
span: DUMMY_SP,
key: PropName::Ident(quote_ident!("_")),
param: value.clone().into(),
params: vec![value.clone().into()],
body: Some(BlockStmt {
span: DUMMY_SP,
stmts: vec![Expr::Ident(
Expand Down Expand Up @@ -719,7 +719,7 @@ fn extend_super(
Prop::Setter(SetterProp {
span: DUMMY_SP,
key: PropName::Ident(quote_ident!("_")),
param: value.clone().into(),
params: vec![value.clone().into()],
body: Some(BlockStmt {
span: DUMMY_SP,
stmts: vec![Expr::Ident(
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl Visit for ThisVisitor {
/// Don't recurse into fn
fn visit_setter_prop(&mut self, n: &SetterProp) {
n.key.visit_with(self);
n.param.visit_with(self);
n.params.visit_with(self);
}

fn visit_this_expr(&mut self, _: &ThisExpr) {
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_visit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ define!({
pub struct SetterProp {
pub span: Span,
pub key: PropName,
pub param: Box<Pat>,
pub params: Vec<Param>,
pub body: Option<BlockStmt>,
}
pub struct MethodProp {
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_estree_compat/src/babelify/prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl Babelify for SetterProp {
base: ctx.base(self.span),
kind: ObjectMethodKind::Set,
key: self.key.babelify(ctx),
params: vec![self.param.babelify(ctx).into()],
params: self.params.babelify(ctx),
body: self.body.unwrap().babelify(ctx),
return_type: Default::default(),
computed: Default::default(),
Expand Down
Loading