From bff1d8723500e73681e1d2755a3e2b38dd6433d1 Mon Sep 17 00:00:00 2001 From: benStre Date: Sun, 12 Jan 2025 21:05:00 +0100 Subject: [PATCH] fix options argument for always --- src/lib.rs | 14 ++++++++ src/visitor.rs | 41 +++++++++++++++++------ tests/__swc_snapshots__/src/lib.rs/t57.js | 3 ++ 3 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 tests/__swc_snapshots__/src/lib.rs/t57.js diff --git a/src/lib.rs b/src/lib.rs index 0eb9cb3..3819eb6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -920,3 +920,17 @@ test!( "# ); + + + +test!( + Syntax::Es(EsSyntax { + jsx: true, + ..Default::default() + },), + |_| TransformVisitor, + t57, + r#" + const test = always(() => x + 1, {allowStatic: true}); + "# +); diff --git a/src/visitor.rs b/src/visitor.rs index c7d214f..ad5f5f5 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -826,6 +826,14 @@ impl TransformVisitor { }) .expr } + + fn get_remaining_args(call: &CallExpr) -> Vec { + call.clone() + .args + .into_iter() + .skip(1) + .collect() + } } impl Fold for TransformVisitor { @@ -833,6 +841,7 @@ impl Fold for TransformVisitor { 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") => { @@ -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 { @@ -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, + } } } } diff --git a/tests/__swc_snapshots__/src/lib.rs/t57.js b/tests/__swc_snapshots__/src/lib.rs/t57.js new file mode 100644 index 0000000..2d7b687 --- /dev/null +++ b/tests/__swc_snapshots__/src/lib.rs/t57.js @@ -0,0 +1,3 @@ +const test = always(()=>x + 1, { + allowStatic: true +});