Skip to content

Commit

Permalink
fix options argument for always
Browse files Browse the repository at this point in the history
  • Loading branch information
benStre committed Jan 12, 2025
1 parent b1ab98b commit bff1d87
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,3 +920,17 @@ test!(
</div>
"#
);



test!(
Syntax::Es(EsSyntax {
jsx: true,
..Default::default()
},),
|_| TransformVisitor,
t57,
r#"
const test = always(() => x + 1, {allowStatic: true});
"#
);
41 changes: 30 additions & 11 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,13 +826,22 @@ impl TransformVisitor {
})
.expr
}

fn get_remaining_args(call: &CallExpr) -> Vec<ExprOrSpread> {
call.clone()
.args
.into_iter()
.skip(1)
.collect()
}
}

impl Fold for TransformVisitor {
fn fold_call_expr(&mut self, call: CallExpr) -> CallExpr {
return match &call.callee {
Callee::Expr(e) => {
let arg = TransformVisitor::get_first_arg(&call);
let remaining_args = TransformVisitor::get_remaining_args(&call);

return match e.unwrap_parens() {
Expr::Ident(i) if i.sym.eq_ignore_ascii_case("always") => {
Expand All @@ -859,14 +868,8 @@ impl Fold for TransformVisitor {
match reactive.unwrap_parens() {
Expr::Call(c) => c.clone(),
// transform_expr_reactive returns a CallExpr in all cases except for Expr::Arrow(_) | Expr::Fn
_ => CallExpr {
span: DUMMY_SP,
callee: Callee::Expr(Box::new(Expr::Ident(Ident::new(
"always".into(),
DUMMY_SP,
call.ctxt,
)))),
args: vec![
_ => {
let mut new_args = vec![
match arg.unwrap_parens() {
Expr::Arrow(_) | Expr::Fn(_) => arg.into(),
_ => Expr::Arrow(ArrowExpr {
Expand All @@ -881,9 +884,25 @@ impl Fold for TransformVisitor {
})
.into(),
}
],
type_args: Take::dummy(),
ctxt: call.ctxt,
];

if remaining_args.len() > 0 {
for arg in remaining_args {
new_args.push(arg);
}
}

CallExpr {
span: DUMMY_SP,
callee: Callee::Expr(Box::new(Expr::Ident(Ident::new(
"always".into(),
DUMMY_SP,
call.ctxt,
)))),
args: new_args,
type_args: Take::dummy(),
ctxt: call.ctxt,
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/__swc_snapshots__/src/lib.rs/t57.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const test = always(()=>x + 1, {
allowStatic: true
});

0 comments on commit bff1d87

Please sign in to comment.