vertigo-macro 0.11.4

Reactive Real-DOM library with SSR for Rust - macros
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
use proc_macro2::Punct;
use proc_macro2::TokenStream as TokenStream2;
use quote::{ToTokens, quote};
use rstml::node::{
    KeyedAttribute, KeyedAttributeValue, Node, NodeAttribute, NodeBlock, NodeName, NodeNameFragment,
};
use std::collections::BTreeMap;
use syn::punctuated::Punctuated;
use syn::{ExprPath, spanned::Spanned};

use crate::{
    component::get_group_attrs_replace_method_name, trace_tailwind::add_to_tailwind,
    utils::release_build,
};

use super::commons::{
    dereferenced_assignment, extract_spread_block, is_default_block, parse_block_of_statements,
    take_block_or_literal_expr,
};
use super::group_attrs::convert_group_attrs;

pub(super) const COMPONENT_ATTR_FORMAT_ERROR: &str = "in component. Expected key=\"value\", key={value}, key={}, group:key=\"value\", group:key={value} {value} or {..value} attribute.";

pub(super) fn convert_child_to_component(node: &Node) -> TokenStream2 {
    let cmp_stream = convert_to_component(node);
    quote! {
        .child(#cmp_stream)
    }
}

pub(super) fn convert_to_component(node: &Node) -> TokenStream2 {
    let element = match node {
        Node::Element(el) => el,
        _ => {
            emit_error!(node.span(), "Can't convert to component");
            return quote! {};
        }
    };
    let constructor_name = element.name();
    let component_name_string = constructor_name.to_string();

    let mut groupped_attrs = BTreeMap::<String, BTreeMap<String, KeyedAttributeValue>>::new();
    let mut spread_attrs = Vec::new();

    let attributes = element
        .attributes()
        .iter()
        .filter_map(|attr_node| {
            attribute_to_tokens(attr_node, &mut spread_attrs, &mut groupped_attrs)
        })
        .collect::<Vec<_>>();

    let mut grouped_attrs_stream = quote! {};

    for (group, attrs) in groupped_attrs {
        convert_group_attrs(&group, &attrs, constructor_name).to_tokens(&mut grouped_attrs_stream);
    }

    let debug_info = quote! {
        match &cmp {
            vertigo::DomNode::Node { node } => {
                node.add_attr("v-component", #component_name_string);
            }
            _ => {}
        };
    };

    let effective_debug_info = if release_build() {
        quote! {
            #[cfg(test)]
            #debug_info
        }
    } else {
        debug_info
    };

    quote! {
        {
            let cmp = #constructor_name {
                #(#attributes)*
            };
            let cmp = cmp.into_component()
                #grouped_attrs_stream
                #(#spread_attrs)*
            ;
            let cmp = cmp.mount();
            #effective_debug_info
            cmp
        }
    }
}

fn attribute_to_tokens(
    attr_node: &NodeAttribute,
    spread_attrs: &mut Vec<TokenStream2>,
    groupped_attrs: &mut BTreeMap<String, BTreeMap<String, KeyedAttributeValue>>,
) -> Option<TokenStream2> {
    let span = attr_node.span();
    match attr_node {
        // Key-value attribute
        NodeAttribute::Attribute(KeyedAttribute {
            key,
            possible_value,
        }) => {
            match key {
                // Regular attribute name
                NodeName::Path(key) => {
                    regular_attribute_to_tokens(key, possible_value, spread_attrs)
                }
                // Attribute name prefixed by group name and colon
                NodeName::Punctuated(p) => {
                    punctuated_attribute_to_tokens(p, possible_value, groupped_attrs)
                }
                _ => {
                    emit_error!(
                        key.span(),
                        "Invalid attribute key {}",
                        COMPONENT_ATTR_FORMAT_ERROR
                    );
                    None
                }
            }
        }
        // Try to use attribute value as key if no key provided
        NodeAttribute::Block(block) => {
            if let NodeBlock::ValidBlock(block) = block {
                if block.stmts.is_empty() {
                    emit_error!(span, "Empty block {}", COMPONENT_ATTR_FORMAT_ERROR);
                    None
                } else {
                    let (key, value, method) = parse_block_of_statements(block);
                    Some(quote! { #key: #value.#method, })
                }
            } else {
                emit_error!(span, "Invalid block {}", COMPONENT_ATTR_FORMAT_ERROR);
                None
            }
        }
    }
}

fn regular_attribute_to_tokens(
    key: &ExprPath,
    possible_value: &KeyedAttributeValue,
    spread_attrs: &mut Vec<TokenStream2>,
) -> Option<TokenStream2> {
    let matches = take_block_or_literal_expr(possible_value, COMPONENT_ATTR_FORMAT_ERROR);
    match matches {
        (Some(value), None) => {
            // If attribute is 'tw' then register tailwind classes
            if key.to_token_stream().to_string() == "tw"
                && let Err(err) = add_to_tailwind(value.to_token_stream())
            {
                emit_error!(value.span(), err);
                return None;
            }

            if is_default_block(&value.block) {
                Some(quote! { #key: Default::default(), })
            } else if let Some(new_block) =
                extract_spread_block(&value.block, |inmost_value| quote! { #inmost_value })
            {
                let replace_method_name = get_group_attrs_replace_method_name(key);
                spread_attrs.push(quote! {
                    .#replace_method_name({
                        #new_block
                    })
                });
                None
            } else {
                dereferenced_assignment(key.to_token_stream(), value)
            }
        }
        (None, Some(lit)) => {
            // If attribute is 'tw' then register tailwind classes
            if key.to_token_stream().to_string() == "tw"
                && let Err(err) = add_to_tailwind(lit.to_token_stream())
            {
                emit_error!(lit.span(), err);
                return None;
            }
            Some(quote! { #key: #lit.into(), })
        }
        _ => None,
    }
}

fn punctuated_attribute_to_tokens(
    p: &Punctuated<NodeNameFragment, Punct>,
    possible_value: &KeyedAttributeValue,
    groupped_attrs: &mut BTreeMap<String, BTreeMap<String, KeyedAttributeValue>>,
) -> Option<TokenStream2> {
    let mut i = p.pairs();
    let group = i.next();
    let have_colon = group.is_some_and(|pair| pair.punct().is_some_and(|p| p.as_char() == ':'));

    if p.len() > 1 && have_colon {
        // Strip colon group name
        let group = group.map(|p| *p.value());
        // Convert whole punctuated to string without spacing
        let key_str = p.to_token_stream().to_string().replace(' ', "");
        let key = key_str
            .trim_start_matches(&format!("{}:", group.to_token_stream()))
            .to_string();

        // If value name is 'tw' then register tailwind classes
        if key == "tw"
            && let Err(err) = add_to_tailwind(possible_value.to_token_stream())
        {
            emit_error!(possible_value.span(), err);
            return None;
        };

        match group {
            Some(group) => {
                let group_entry = groupped_attrs.entry(group.to_string()).or_default();
                group_entry.insert(key, possible_value.clone());
            }
            _ => {
                emit_error!(
                    key.span(),
                    "Invalid punctuated attribute key {}",
                    COMPONENT_ATTR_FORMAT_ERROR
                );
            }
        }
        None
    } else {
        // No colon, add regular attribute
        let key = p.to_token_stream();
        let matches = take_block_or_literal_expr(possible_value, COMPONENT_ATTR_FORMAT_ERROR);
        match matches {
            (Some(value), None) => {
                if is_default_block(&value.block) {
                    Some(quote! { #key: Default::default(), })
                } else {
                    dereferenced_assignment(key, value)
                }
            }
            (None, Some(lit)) => Some(quote! { #key: #lit.into(), }),
            _ => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use quote::quote;
    use rstml::parse2;
    use std::error::Error;

    fn format_token_stream(tokens: TokenStream2) -> Result<String, Box<dyn Error>> {
        let file = syn::parse2::<syn::File>(quote! {
            fn dummy() {
                #tokens
            }
        })?;
        Ok(prettyplease::unparse(&file))
    }

    #[test]
    fn test_convert_to_component_simple() -> Result<(), Box<dyn Error>> {
        let input = quote! { <MyComponent /> };
        let nodes = parse2(input)?;
        let node = &nodes[0];
        let result = convert_to_component(node);

        let expected = quote! {
            {
                let cmp = MyComponent {};
                let cmp = cmp.into_component();
                let cmp = cmp.mount();
                match &cmp {
                    vertigo::DomNode::Node { node } => {
                        node.add_attr("v-component", "MyComponent");
                    }
                    _ => {}
                };
                cmp
            }
        };

        pretty_assertions::assert_eq!(format_token_stream(result)?, format_token_stream(expected)?);
        Ok(())
    }

    #[test]
    fn test_convert_to_component_with_attributes() -> Result<(), Box<dyn Error>> {
        let input = quote! { <MyComponent attr1="val1" attr2={42} /> };
        let nodes = parse2(input)?;
        let node = &nodes[0];
        let result = convert_to_component(node);

        let expected = quote! {
            {
                let cmp = MyComponent {
                    attr1: "val1".into(),
                    attr2: { 42 }.into(),
                };
                let cmp = cmp.into_component();
                let cmp = cmp.mount();
                match &cmp {
                    vertigo::DomNode::Node { node } => {
                        node.add_attr("v-component", "MyComponent");
                    }
                    _ => {}
                };
                cmp
            }
        };

        pretty_assertions::assert_eq!(format_token_stream(result)?, format_token_stream(expected)?);
        Ok(())
    }

    #[test]
    fn test_convert_to_component_with_grouped_attributes() -> Result<(), Box<dyn Error>> {
        let input = quote! { <MyComponent css:color="red" css:margin={10} /> };
        let nodes = parse2(input)?;
        let node = &nodes[0];
        let result = convert_to_component(node);

        // Grouped attributes are handled via convert_group_attrs which is called for each group.
        // It results in .group_{group}_push(...) calls.
        // Literals are handled directly, while blocks use attr_to_group_variant.
        let expected = quote! {
            {
                let cmp = MyComponent {};
                let cmp = cmp.into_component()
                    .group_css_push("color".into(), "red".into())
                    .group_css_push("margin".into(), { 10 }.into());
                let cmp = cmp.mount();
                match &cmp {
                    vertigo::DomNode::Node { node } => {
                        node.add_attr("v-component", "MyComponent");
                    }
                    _ => {}
                };
                cmp
            }
        };

        pretty_assertions::assert_eq!(format_token_stream(result)?, format_token_stream(expected)?);
        Ok(())
    }

    #[test]
    fn test_convert_to_component_with_block_attribute() -> Result<(), Box<dyn Error>> {
        let input = quote! { <MyComponent { some_value } /> };
        let nodes = parse2(input)?;
        let node = &nodes[0];
        let result = convert_to_component(node);

        // parse_block_of_statements for { some_value } results in (some_value, some_value, into())
        let expected = quote! {
            {
                let cmp = MyComponent {
                    some_value: some_value.into(),
                };
                let cmp = cmp.into_component();
                let cmp = cmp.mount();
                match &cmp {
                    vertigo::DomNode::Node { node } => {
                        node.add_attr("v-component", "MyComponent");
                    }
                    _ => {}
                };
                cmp
            }
        };

        pretty_assertions::assert_eq!(format_token_stream(result)?, format_token_stream(expected)?);
        Ok(())
    }

    #[test]
    fn test_convert_to_component_with_tw_attribute() -> Result<(), Box<dyn Error>> {
        let input = quote! { <MyComponent tw="text-red-500" /> };
        let nodes = parse2(input)?;
        let node = &nodes[0];
        let result = convert_to_component(node);

        let expected = quote! {
            {
                let cmp = MyComponent {
                    tw: "text-red-500".into(),
                };
                let cmp = cmp.into_component();
                let cmp = cmp.mount();
                match &cmp {
                    vertigo::DomNode::Node { node } => {
                        node.add_attr("v-component", "MyComponent");
                    }
                    _ => {}
                };
                cmp
            }
        };

        pretty_assertions::assert_eq!(format_token_stream(result)?, format_token_stream(expected)?);
        Ok(())
    }

    #[test]
    fn test_convert_to_component_with_spread_attribute() -> Result<(), Box<dyn Error>> {
        let input = quote! { <MyComponent some_attr={..spread_val} /> };
        let nodes = parse2(input)?;
        let node = &nodes[0];
        let result = convert_to_component(node);

        let expected = quote! {
            {
                let cmp = MyComponent {};
                let cmp = cmp.into_component()
                    .group_some_attr_replace({
                        spread_val
                    });
                let cmp = cmp.mount();
                match &cmp {
                    vertigo::DomNode::Node { node } => {
                        node.add_attr("v-component", "MyComponent");
                    }
                    _ => {}
                };
                cmp
            }
        };

        pretty_assertions::assert_eq!(format_token_stream(result)?, format_token_stream(expected)?);
        Ok(())
    }

    #[test]
    fn test_convert_to_component_with_default_attribute() -> Result<(), Box<dyn Error>> {
        let input = quote! { <MyComponent some_attr={ Default::default() } /> };
        let nodes = parse2(input)?;
        let node = &nodes[0];
        let result = convert_to_component(node);

        let expected = quote! {
            {
                let cmp = MyComponent {
                    some_attr: Default::default(),
                };
                let cmp = cmp.into_component();
                let cmp = cmp.mount();
                match &cmp {
                    vertigo::DomNode::Node { node } => {
                        node.add_attr("v-component", "MyComponent");
                    }
                    _ => {}
                };
                cmp
            }
        };

        pretty_assertions::assert_eq!(format_token_stream(result)?, format_token_stream(expected)?);
        Ok(())
    }
}