Skip to content

Commit

Permalink
add support for static val() wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Benedikt Strehle committed Oct 7, 2024
1 parent b25fca8 commit 371e03a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,3 +717,44 @@ test!(
"#
);


test!(
Syntax::Es(EsSyntax {
jsx: true,
..Default::default()
},),
|_| TransformVisitor,
t49,
r#"
call(function ({title, icon, children}) {
return <div class="card">
<span title="Dialog schließen" onclick:frontend={()=>use(this) && this.closeDialog()} class="desktop-close"><i class="fa-solid fa-xmark"/></span>
<h1>
<span title="Zurück" onclick:frontend={()=> this.closeDialog()} class="mobile-back"><i class="fa-solid fa-chevron-left"/></span>
{ icon && <i class={`fas ${icon}`} style="margin-right:10px"/>}
{title}
</h1>
{...children}
{...(children)}
{...[1,2,3]}
{...([1,2,3].map((item) => <span>{item}</span>))}
</div>
});
"#
);


test!(
Syntax::Es(EsSyntax {
jsx: true,
..Default::default()
},),
|_| TransformVisitor,
t50,
r#"
<div>
<div>{val(x.y)}</div>
<div>{val(x())}</div>
</div>
"#
);
8 changes: 8 additions & 0 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,14 @@ impl TransformVisitor {
)
),

// is val() call expr -> ignore and just return val
Expr::Call(c)
if c.callee.is_expr()
&& c.callee.as_expr().unwrap().is_ident_ref_to("val") =>
{
e
}

// convert array.map(() => {}) to _$method(array, 'map', (() => {})
// TODO
Expr::Call(c)
Expand Down
27 changes: 27 additions & 0 deletions tests/__swc_snapshots__/src/lib.rs/t49.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
call(function({ title, icon, children }) {
return <div class="card">
<span title="Dialog schließen" onclick:frontend={()=>{
return use(this) && this.closeDialog();
}} class="desktop-close"><i class="fa-solid fa-xmark"/></span>
<h1>
<span title="Zurück" onclick:frontend={()=>{
use("silent-errors", this);
return this.closeDialog();
}} class="mobile-back"><i class="fa-solid fa-chevron-left"/></span>
{_$(()=>icon && <i class={`fas ${icon}`} style="margin-right:10px"/>)}
{title}
</h1>
{...children}
{...children}
{..._$(()=>[
1,
2,
3
])}
{..._$method([
1,
2,
3
], "map", (item)=><span>{item}</span>)}
</div>;
});
4 changes: 4 additions & 0 deletions tests/__swc_snapshots__/src/lib.rs/t50.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
<div>{val(x.y)}</div>
<div>{val(x())}</div>
</div>;

0 comments on commit 371e03a

Please sign in to comment.