|
6 | 6 | use proc_macro2::TokenStream;
|
7 | 7 | use quote::quote;
|
8 | 8 | use sycamore_view_parser::ir::{DynNode, Node, Prop, PropType, Root, TagIdent, TagNode, TextNode};
|
9 |
| -use syn::Expr; |
| 9 | +use syn::{Expr, Pat}; |
10 | 10 |
|
11 | 11 | pub struct Codegen {
|
12 | 12 | // TODO: configure mode: Client, Hydrate, SSR
|
@@ -42,7 +42,7 @@ impl Codegen {
|
42 | 42 | ::std::convert::Into::<::sycamore::rt::View>::into(#value)
|
43 | 43 | },
|
44 | 44 | Node::Dyn(DynNode { value }) => {
|
45 |
| - let is_dynamic = !matches!(value, Expr::Lit(_) | Expr::Closure(_) | Expr::Path(_)); |
| 45 | + let is_dynamic = is_dyn(value); |
46 | 46 | if is_dynamic {
|
47 | 47 | quote! {
|
48 | 48 | ::sycamore::rt::View::from_dynamic(
|
@@ -92,7 +92,7 @@ impl Codegen {
|
92 | 92 |
|
93 | 93 | pub fn attribute(&self, attr: &Prop) -> TokenStream {
|
94 | 94 | let value = &attr.value;
|
95 |
| - let is_dynamic = !matches!(value, Expr::Lit(_) | Expr::Closure(_) | Expr::Path(_)); |
| 95 | + let is_dynamic = is_dyn(value); |
96 | 96 | let dyn_value = if is_dynamic {
|
97 | 97 | quote! { move || #value }
|
98 | 98 | } else {
|
@@ -196,3 +196,108 @@ fn is_component(ident: &TagIdent) -> bool {
|
196 | 196 | TagIdent::Hyphenated(_) => false,
|
197 | 197 | }
|
198 | 198 | }
|
| 199 | + |
| 200 | +fn is_dyn(ex: &Expr) -> bool { |
| 201 | + match ex { |
| 202 | + Expr::Lit(_) | Expr::Closure(_) | Expr::Path(_) | Expr::Field(_) => false, |
| 203 | + |
| 204 | + Expr::Paren(p) => is_dyn(&p.expr), |
| 205 | + Expr::Group(g) => is_dyn(&g.expr), |
| 206 | + Expr::Tuple(t) => t.elems.iter().any(is_dyn), |
| 207 | + Expr::Array(a) => a.elems.iter().any(is_dyn), |
| 208 | + Expr::Repeat(r) => is_dyn(&r.expr) || is_dyn(&r.len), |
| 209 | + Expr::Struct(s) => s.fields.iter().any(|fv: &syn::FieldValue| is_dyn(&fv.expr)), |
| 210 | + |
| 211 | + Expr::Cast(c) => is_dyn(&c.expr), |
| 212 | + Expr::Macro(m) => is_dyn_macro(&m.mac), |
| 213 | + Expr::Block(b) => is_dyn_block(&b.block), |
| 214 | + Expr::Const(_const_block) => false, |
| 215 | + |
| 216 | + Expr::Loop(l) => is_dyn_block(&l.body), |
| 217 | + Expr::While(w) => is_dyn(&w.cond) || is_dyn_block(&w.body), |
| 218 | + Expr::ForLoop(f) => is_dyn_pattern(&f.pat) || is_dyn(&f.expr) || is_dyn_block(&f.body), |
| 219 | + Expr::Break(_) | Expr::Continue(_) => false, |
| 220 | + |
| 221 | + Expr::Let(e) => is_dyn_pattern(&e.pat) || is_dyn(&e.expr), |
| 222 | + |
| 223 | + Expr::Match(m) => { |
| 224 | + is_dyn(&m.expr) |
| 225 | + || m.arms.iter().any(|a: &syn::Arm| { |
| 226 | + is_dyn_pattern(&a.pat) |
| 227 | + || a.guard.as_ref().is_some_and(|(_, g_expr)| is_dyn(g_expr)) |
| 228 | + || is_dyn(&a.body) |
| 229 | + }) |
| 230 | + } |
| 231 | + |
| 232 | + Expr::If(i) => { |
| 233 | + is_dyn(&i.cond) |
| 234 | + || is_dyn_block(&i.then_branch) |
| 235 | + || i.else_branch.as_ref().is_some_and(|(_, e)| is_dyn(e)) |
| 236 | + } |
| 237 | + |
| 238 | + Expr::Unary(u) => is_dyn(&u.expr), |
| 239 | + Expr::Binary(b) => is_dyn(&b.left) || is_dyn(&b.right), |
| 240 | + Expr::Index(i) => is_dyn(&i.expr) || is_dyn(&i.index), |
| 241 | + Expr::Range(r) => { |
| 242 | + r.start.as_deref().is_some_and(is_dyn) || r.end.as_deref().is_some_and(is_dyn) |
| 243 | + } |
| 244 | + |
| 245 | + _ => true, |
| 246 | + } |
| 247 | +} |
| 248 | + |
| 249 | +fn is_dyn_pattern(pat: &Pat) -> bool { |
| 250 | + match pat { |
| 251 | + Pat::Wild(_) | Pat::Lit(_) | Pat::Path(_) | Pat::Rest(_) | Pat::Type(_) | Pat::Const(_) => { |
| 252 | + false |
| 253 | + } |
| 254 | + |
| 255 | + Pat::Paren(p) => is_dyn_pattern(&p.pat), |
| 256 | + Pat::Or(o) => o.cases.iter().any(is_dyn_pattern), |
| 257 | + Pat::Tuple(t) => t.elems.iter().any(is_dyn_pattern), |
| 258 | + Pat::TupleStruct(s) => s.elems.iter().any(is_dyn_pattern), |
| 259 | + Pat::Slice(s) => s.elems.iter().any(is_dyn_pattern), |
| 260 | + Pat::Range(r) => { |
| 261 | + r.start.as_deref().is_some_and(is_dyn) || r.end.as_deref().is_some_and(is_dyn) |
| 262 | + } |
| 263 | + |
| 264 | + Pat::Reference(r) => r.mutability.is_some(), |
| 265 | + Pat::Ident(id) => { |
| 266 | + (id.by_ref.is_some() && id.mutability.is_some()) |
| 267 | + || id |
| 268 | + .subpat |
| 269 | + .as_ref() |
| 270 | + .is_some_and(|(_, pat)| is_dyn_pattern(pat)) |
| 271 | + } |
| 272 | + |
| 273 | + Pat::Struct(s) => s |
| 274 | + .fields |
| 275 | + .iter() |
| 276 | + .any(|fp: &syn::FieldPat| is_dyn_pattern(&fp.pat)), |
| 277 | + |
| 278 | + // syn::Pat is non-exhaustive |
| 279 | + _ => true, |
| 280 | + } |
| 281 | +} |
| 282 | + |
| 283 | +fn is_dyn_macro(m: &syn::Macro) -> bool { |
| 284 | + // Bodies of nested inner view! macros will be checked for dynamic |
| 285 | + // parts when their own codegen is run. |
| 286 | + !m.path |
| 287 | + .get_ident() |
| 288 | + .is_some_and(|ident| "view" == &ident.to_string()) |
| 289 | +} |
| 290 | + |
| 291 | +fn is_dyn_block(block: &syn::Block) -> bool { |
| 292 | + block.stmts.iter().any(|s: &syn::Stmt| match s { |
| 293 | + syn::Stmt::Expr(ex, _) => is_dyn(ex), |
| 294 | + syn::Stmt::Macro(m) => is_dyn_macro(&m.mac), |
| 295 | + syn::Stmt::Local(loc) => { |
| 296 | + is_dyn_pattern(&loc.pat) |
| 297 | + || loc.init.as_ref().is_some_and(|i| { |
| 298 | + is_dyn(&i.expr) || i.diverge.as_ref().is_some_and(|(_, ex)| is_dyn(ex)) |
| 299 | + }) |
| 300 | + } |
| 301 | + syn::Stmt::Item(_) => false, |
| 302 | + }) |
| 303 | +} |
0 commit comments