Skip to content

Commit

Permalink
fix(es/ast): Fix definition of SetterProp (#8314)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8157
 - Closes #8377
  • Loading branch information
kdy1 committed Jan 21, 2024
1 parent 572bcae commit bc38ac9
Show file tree
Hide file tree
Showing 33 changed files with 275 additions and 89 deletions.
4 changes: 2 additions & 2 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, Pat,
};

#[ast_node]
Expand Down Expand Up @@ -76,6 +75,7 @@ pub struct GetterProp {
pub struct SetterProp {
pub span: Span,
pub key: PropName,
pub this_param: Option<Pat>,
pub param: Box<Pat>,
#[cfg_attr(feature = "serde-impl", serde(default))]
pub body: Option<BlockStmt>,
Expand Down
8 changes: 8 additions & 0 deletions crates/swc_ecma_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2218,7 +2218,15 @@ where
formatting_space!();

punct!("(");
if let Some(this) = &node.this_param {
emit!(this);
punct!(",");

formatting_space!();
}

emit!(node.param);

punct!(")");

emit!(node.body);
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_compat_common/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ macro_rules! impl_visit_mut_fn {
let (mut params, body) = self.visit_mut_fn_like(
&mut vec![Param {
span: DUMMY_SP,
decorators: Default::default(),
decorators: vec![],
pat: *f.param.take(),
}],
&mut f.body.take().unwrap(),
);
debug_assert!(params.len() == 1);

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

Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_compat_es2015/src/computed_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ impl VisitMut for ComputedProps {
body,
param,
key,
..
}) => (
key,
Box::new(Function {
Expand Down
32 changes: 22 additions & 10 deletions crates/swc_ecma_parser/src/parser/object.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Parser for object literal.
use swc_common::Spanned;
use swc_common::{Spanned, DUMMY_SP};

use super::*;
use crate::parser::class_and_fn::is_not_this;
Expand Down Expand Up @@ -355,21 +355,33 @@ impl<I: Tokens> ParseObject<Box<Expr>> for Parser<I> {
|Function {
mut params, body, ..
}| {
params.retain(|p| match &p.pat {
Pat::Ident(p) => p.sym != "this",
_ => true,
});
let mut this = None;
if params.len() >= 2 {
this = Some(params.remove(0).pat);
}

let param = Box::new(
params
.into_iter()
.next()
.map(|v| v.pat)
.unwrap_or_else(|| {
parser.emit_err(
key_span,
SyntaxError::SetterParam,
);

Pat::Invalid(Invalid { span: DUMMY_SP })
}),
);

// 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 }),
),
),
param,
this_param: this,
})))
},
)
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_parser/tests/tsc/accessorWithES3.json
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@
"value": "b",
"optional": false
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_parser/tests/tsc/accessorWithES5.json
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@
"value": "b",
"optional": false
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@
"optional": false
}
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down Expand Up @@ -408,6 +409,7 @@
}
}
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down Expand Up @@ -518,6 +520,7 @@
"raw": "\"\""
}
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down Expand Up @@ -619,6 +622,7 @@
"optional": false
}
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down Expand Up @@ -748,6 +752,7 @@
]
}
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@
"optional": false
}
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down Expand Up @@ -408,6 +409,7 @@
}
}
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down Expand Up @@ -518,6 +520,7 @@
"raw": "\"\""
}
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down Expand Up @@ -619,6 +622,7 @@
"optional": false
}
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down Expand Up @@ -748,6 +752,7 @@
]
}
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
}
}
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
}
}
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@
}
}
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@
}
}
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,7 @@
"value": "x",
"optional": false
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down Expand Up @@ -1935,6 +1936,7 @@
"value": "y",
"optional": false
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2808,6 +2808,7 @@
"value": "X",
"optional": false
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down
2 changes: 2 additions & 0 deletions crates/swc_ecma_parser/tests/tsc/objectLiteralErrorsES3.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
"value": "a",
"optional": false
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down Expand Up @@ -275,6 +276,7 @@
"value": "a",
"optional": false
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
"value": "bar",
"optional": false
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down Expand Up @@ -391,6 +392,7 @@
"value": "foo",
"optional": false
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_parser/tests/tsc/parserAccessors4.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"value": "Foo",
"optional": false
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"optional": false
}
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_parser/tests/tsc/parserES3Accessors4.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"value": "Foo",
"optional": false
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2808,6 +2808,7 @@
"value": "X",
"optional": false
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
}
}
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_parser/tests/tsc/symbolProperty18.json
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@
}
}
},
"thisParam": null,
"param": {
"type": "Identifier",
"span": {
Expand Down
Loading

0 comments on commit bc38ac9

Please sign in to comment.