From b141e134d4095b31366e13bc6bd014791f5b4ef5 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sun, 1 Feb 2026 10:21:28 +0100 Subject: [PATCH] Direct text dependencies --- examples/hello_world.rs | 2 +- macros/src/parse.rs | 55 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 19fdab5..78cfc92 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -18,7 +18,7 @@ fn Card(cx: &Arc, content: String) -> View { let content = content.clone(); rsx! { - %ref content "----\n{content}\n----" + "----\n{content}\n----" } .view(&cx) } diff --git a/macros/src/parse.rs b/macros/src/parse.rs index 2908dc2..0bb9f23 100644 --- a/macros/src/parse.rs +++ b/macros/src/parse.rs @@ -2,6 +2,7 @@ //! //! Parses RSX syntax into an AST that can be emitted as Rust code. +use proc_macro2::Span; use syn::braced; use syn::parse::discouraged::Speculative; use syn::{ @@ -174,10 +175,22 @@ impl Parse for RsxNode { // "%$dep for $pat in $expr { $rsx }" if input.peek(LitStr) { - return Ok(RsxNode::Text { - text: input.parse()?, - deps, - }); + let text = input.parse()?; + + let mut deps = deps; + + deps.append( + &mut extract_vars_from_lit(&text) + .into_iter() + .map(|ident| Dep { + ident, + pat: None, + is_dep: false, + }) + .collect(), + ); + + return Ok(RsxNode::Text { text, deps }); } parse_component_invocation(input) @@ -255,3 +268,37 @@ fn parse_children(input: ParseStream) -> Result> { } Ok(nodes) } + +fn extract_vars_from_lit(lit: &LitStr) -> Vec { + let s = lit.value(); + let mut vars = Vec::new(); + + let mut chars = s.chars().peekable(); + while let Some(c) = chars.next() { + if c == '{' { + // skip escaped {{ + if chars.peek() == Some(&'{') { + chars.next(); + continue; + } + + let mut name = String::new(); + while let Some(&ch) = chars.peek() { + chars.next(); + if ch == '}' { + break; + } + name.push(ch); + } + + if !name.is_empty() { + vars.push(Ident::new( + &name.split_once(':').unwrap_or((&name, &name)).0, + Span::call_site(), + )); + } + } + } + + vars +}