Skip to content

Commit

Permalink
use _ for reactive array methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Benedikt Strehle committed Sep 20, 2024
1 parent 3e15357 commit 2e9eefd
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 74 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
^target/
target
_test
node_modules
node_modules
uix-tests/.datex-cache
Binary file modified jusix.wasm
Binary file not shown.
91 changes: 59 additions & 32 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl TransformVisitor {
)
),

// convert array.map(() => {}) to array.$.map(() => {})
// convert array.map(() => {}) to _$method(array, 'map', (() => {})
// TODO
Expr::Call(c)
if c.callee.is_expr()
Expand All @@ -124,40 +124,67 @@ impl TransformVisitor {
let obj = member.obj.clone();
let prop = member.prop.clone();

let mut args: Vec::<ExprOrSpread> = vec![
ExprOrSpread {
expr: obj,
spread: None
},
ExprOrSpread {
expr: Box::new(Expr::Lit(Lit::Str(Str {
span: DUMMY_SP,
value: prop.as_ident().unwrap().sym.clone().into(),
raw: None
}))),
spread: None
}
];

for arg in c.args.clone().into_iter().map(|a| {
match a {
ExprOrSpread {
expr: e,
spread: None,
} => match e.unwrap_parens() {
Expr::Arrow(a1) => ExprOrSpread {
expr: {
let mut a2 = a1.clone();
a2.body = Box::new(
*a2.body.fold_with(self)
);
Box::new(Expr::Arrow(a2))
},
spread: None,
},
_ => ExprOrSpread {
expr: e,
spread: None,
}
},
_ => a
}
}) {
args.push(arg);
}


Box::new(Expr::Call(CallExpr {
span: c.span,
callee: Callee::Expr(Box::new(Expr::Member(MemberExpr {
span: DUMMY_SP,
obj: obj,
prop: MemberProp::Ident(IdentName::from(
format!("$.{}", prop.as_ident().unwrap().sym).to_string()
)),
}))),
// callee: Callee::Expr(Box::new(Expr::Member(MemberExpr {
// span: DUMMY_SP,
// obj: obj,
// prop: MemberProp::Ident(IdentName::from(
// format!("$.{}", prop.as_ident().unwrap().sym).to_string()
// )),
// }))),
callee: Callee::Expr(Box::new(Expr::Ident(
Ident::new(
"_$method".into(),
DUMMY_SP,
Default::default(),
)
))),
// transform first arg if it's a function, keep others
args: c.args.clone().into_iter().map(|a| {
match a {
ExprOrSpread {
expr: e,
spread: None,
} => match e.unwrap_parens() {
Expr::Arrow(a1) => ExprOrSpread {
expr: {
let mut a2 = a1.clone();
a2.body = Box::new(
*a2.body.fold_with(self)
);
Box::new(Expr::Arrow(a2))
},
spread: None,
},
_ => ExprOrSpread {
expr: e,
spread: None,
}
},
_ => a
}
}).collect(),
args,
type_args: Take::dummy(),
ctxt: Default::default(),
}))
Expand Down
2 changes: 1 addition & 1 deletion tests/__swc_snapshots__/src/lib.rs/t18.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div>
{array.$.map((item)=>{
{_$method(array, "map", (item)=>{
return <span>{item}</span>;
})}
</div>;
12 changes: 6 additions & 6 deletions tests/__swc_snapshots__/src/lib.rs/t19.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<div>
{array.$.map((item)=>item)}
{array.$.map((item)=>item * x)}
{array.$.map((item)=>{
{_$method(array, "map", (item)=>item)}
{_$method(array, "map", (item)=>item * x)}
{_$method(array, "map", (item)=>{
return item * x;
})}
{array.$.map((item)=>{
{_$method(array, "map", (item)=>{
return <span>{_$(()=>item * x)}</span>;
})}
{array.$.map((item)=><div>{_$(()=>item * x)}</div>)}
{array.$.filter((item)=>{
{_$method(array, "map", (item)=><div>{_$(()=>item * x)}</div>)}
{_$method(array, "filter", (item)=>{
return <span>{_$(()=>item * x)}</span>;
})}
{_$(()=>array.normalMethod((item)=>{
Expand Down
2 changes: 1 addition & 1 deletion tests/__swc_snapshots__/src/lib.rs/t23.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
const x = arr.$.map((a)=>a * 2);
const x = _$method(arr, "map", (a)=>a * 2);
1 change: 0 additions & 1 deletion uix-tests/.datex-cache/.dx

This file was deleted.

23 changes: 0 additions & 23 deletions uix-tests/.datex-cache/importmap.lock.json

This file was deleted.

1 change: 0 additions & 1 deletion uix-tests/.datex-cache/uix-fallback-port

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file removed uix-tests/.datex-cache/uix/jusix/jusix.wasm
Binary file not shown.
36 changes: 34 additions & 2 deletions uix-tests/common/tests.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Component1, { Car } from "common/Component1.tsx";
import { ObjectRef, Ref } from "datex-core-legacy/runtime/pointers.ts";

export default {

Expand Down Expand Up @@ -63,6 +62,39 @@ export default {

},

'/test3': () => <input type="number" value={Promise.resolve('xy')}/>
'/test3': () => <input type="number" value={Promise.resolve('123')}/>,

'/test4': () => {
const arr = $$([0]);
setInterval(() => {
arr.push(Math.random());
}, 1000);

return <div>
<ul>
{
[1,2,3].map((i) => <li>{i}</li>)
}
</ul>
<ul>
{
arr.map((i) => <li>{i}</li>)
}
</ul>
</div>
},

'/test5': () => {
const x = $(0);
const y = 0;
setInterval(() => {
x.val++;
});
return <main>
<div>{x + 1}</div>
<div>{y + 1}</div>
</main>

}

}
17 changes: 11 additions & 6 deletions uix-tests/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2e9eefd

Please sign in to comment.