rstml-control-flow 0.1.1

Custom nodes with control flow implementation for rstml. Usefull when you need to implement If, For, etc.
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
//!
//! This module contain implementation of flow-controll based on rstml custom
//! nodes.
//!
//! This is usefull when one need to define optional or repetetive html(*ml)
//! elements. It contain implementation of If, Match and For constructions that
//! is very simmilar to rust. The main difference in implementation is that body
//! of construction (part inside curly brackets) is expected to be an html(*ml),
//! but condition is expected to be in rust syntax.

use quote::{ToTokens, TokenStreamExt};
use rstml::{
    node::{CustomNode, Node as RNode},
    recoverable::{ParseRecoverable, RecoverableContext},
    visitor::Visitor,
};
use syn::{
    braced,
    parse::{Parse, ParseStream},
    token::Brace,
    Expr, Pat, Token,
};

use crate::{Either, TryIntoOrCloneRef};

#[cfg(not(feature = "extendable"))]
type CustomNodeType = EscapeCode;

#[cfg(feature = "extendable")]
type CustomNodeType = crate::ExtendableCustomNode;

type Node = RNode<CustomNodeType>;

#[derive(Clone, Debug, syn_derive::ToTokens)]
pub struct Block {
    #[syn(braced)]
    pub brace_token: Brace,
    #[syn(in = brace_token)]
    #[to_tokens(|tokens, val| tokens.append_all(val))]
    pub body: Vec<Node>,
}

impl ParseRecoverable for Block {
    fn parse_recoverable(parser: &mut RecoverableContext, input: ParseStream) -> Option<Self> {
        // we use this closure, because `braced!`
        // has private api and force it's usage inside methods that return Result
        let inner_parser = |parser: &mut RecoverableContext, input: ParseStream| {
            let content;
            let brace_token = braced!(content in input);
            let mut body = vec![];
            while !content.is_empty() {
                let Some(val) = parser.parse_recoverable(&content) else {
                    return Ok(None);
                };
                body.push(val);
            }
            Ok(Some(Block { brace_token, body }))
        };
        parser.parse_mixed_fn(input, inner_parser)?
    }
}

#[derive(Clone, Debug, syn_derive::ToTokens)]
pub struct ElseIf {
    pub else_token: Token![else],
    pub if_token: Token![if],
    pub condition: Expr,
    pub then_branch: Block,
}

impl ParseRecoverable for ElseIf {
    fn parse_recoverable(parser: &mut RecoverableContext, input: ParseStream) -> Option<Self> {
        Some(ElseIf {
            else_token: parser.parse_simple(input)?,
            if_token: parser.parse_simple(input)?,
            condition: parser.parse_mixed_fn(input, |_, input| {
                input.call(Expr::parse_without_eager_brace)
            })?,
            then_branch: parser.parse_recoverable(input)?,
        })
    }
}

#[derive(Clone, Debug, syn_derive::ToTokens)]
pub struct Else {
    pub else_token: Token![else],
    pub then_branch: Block,
}
impl ParseRecoverable for Else {
    fn parse_recoverable(parser: &mut RecoverableContext, input: ParseStream) -> Option<Self> {
        Some(Else {
            else_token: parser.parse_simple(input)?,
            then_branch: parser.parse_recoverable(input)?,
        })
    }
}

/// If construction:
/// Condition is any valid rust syntax, while body is expected yo be *ml
/// element.
///
/// Example:
/// `@if x > 2 { <div></div> }`
/// `@if let Some(x) = foo { <div></div> }`
///
/// As in rust can contain arbitrary amount of `else if .. {..}` constructs and
/// one `else {..}`.
#[derive(Clone, Debug, syn_derive::ToTokens)]
pub struct IfExpr {
    pub keyword: Token![if],
    pub condition: Expr,
    pub then_branch: Block,
    #[to_tokens(TokenStreamExt::append_all)]
    pub else_ifs: Vec<ElseIf>,
    pub else_branch: Option<Else>,
}

impl ParseRecoverable for IfExpr {
    fn parse_recoverable(parser: &mut RecoverableContext, input: ParseStream) -> Option<Self> {
        let keyword = parser.parse_simple(input)?;

        let condition = parser.parse_mixed_fn(input, |_, input| {
            input.call(Expr::parse_without_eager_brace)
        })?;

        let then_branch = parser.parse_recoverable(input)?;
        let mut else_ifs = vec![];

        while input.peek(Token![else]) && input.peek2(Token![if]) {
            else_ifs.push(parser.parse_recoverable(input)?)
        }
        let mut else_branch = None;
        if input.peek(Token![else]) {
            else_branch = Some(parser.parse_recoverable(input)?)
        }
        Some(IfExpr {
            keyword,
            condition,
            then_branch,
            else_ifs,
            else_branch,
        })
    }
}

#[derive(Clone, Debug, syn_derive::ToTokens)]
pub struct ForExpr {
    pub keyword: Token![for],
    pub pat: Pat,
    pub token_in: Token![in],
    pub expr: Expr,
    pub block: Block,
}

impl ParseRecoverable for ForExpr {
    fn parse_recoverable(parser: &mut RecoverableContext, input: ParseStream) -> Option<Self> {
        let keyword = parser.parse_simple(input)?;
        let pat = parser.parse_mixed_fn(input, |_parse, input| {
            Pat::parse_multi_with_leading_vert(input)
        })?;
        let token_in = parser.parse_simple(input)?;
        let expr = parser.parse_mixed_fn(input, |_, input| {
            input.call(Expr::parse_without_eager_brace)
        })?;
        let block = parser.parse_recoverable(input)?;
        Some(ForExpr {
            keyword,
            pat,
            token_in,
            expr,
            block,
        })
    }
}

#[derive(Clone, Debug, syn_derive::ToTokens)]
pub struct Arm {
    pub pat: Pat,
    // pub guard: Option<(If, Box<Expr>)>,
    pub fat_arrow_token: Token![=>],
    pub body: Block,
    pub comma: Option<Token![,]>,
}

impl ParseRecoverable for Arm {
    fn parse_recoverable(parser: &mut RecoverableContext, input: ParseStream) -> Option<Self> {
        Some(Arm {
            pat: parser
                .parse_mixed_fn(input, |_, input| Pat::parse_multi_with_leading_vert(input))?,
            fat_arrow_token: parser.parse_simple(input)?,
            body: parser.parse_recoverable(input)?,
            comma: parser.parse_simple(input)?,
        })
    }
}

// match foo {
//     1|3 => {},
//     x if x > 10 => {},
//     | x => {}
// }

#[derive(Clone, Debug, syn_derive::ToTokens)]
pub struct MatchExpr {
    pub keyword: Token![match],
    pub expr: Expr,
    #[syn(braced)]
    pub brace_token: Brace,
    #[syn(in = brace_token)]
    #[to_tokens(TokenStreamExt::append_all)]
    pub arms: Vec<Arm>,
}

impl ParseRecoverable for MatchExpr {
    fn parse_recoverable(parser: &mut RecoverableContext, input: ParseStream) -> Option<Self> {
        // we use this closure, because `braced!`
        // has private api and force it's usage inside methods that return Result
        let inner_parser = |parser: &mut RecoverableContext, input: ParseStream| {
            let Some(keyword) = parser.parse_simple(input) else {
                return Ok(None);
            };
            let content;
            let expr = Expr::parse_without_eager_brace(input)?;
            let brace_token = braced!(content in input);
            let mut arms = Vec::new();
            while !content.is_empty() {
                let Some(val) = parser.parse_recoverable(&content) else {
                    return Ok(None);
                };
                arms.push(val);
            }
            Ok(Some(MatchExpr {
                keyword,
                expr,
                brace_token,
                arms,
            }))
        };
        parser.parse_mixed_fn(input, inner_parser)?
    }
}

// Minimal version of syn::Expr, that uses custom `Block` with `Node` array
// instead of `syn::Block` that contain valid rust code.

#[derive(Clone, Debug, syn_derive::ToTokens)]
pub enum EscapedExpr {
    If(IfExpr),
    For(ForExpr),
    Match(MatchExpr),
}

impl ParseRecoverable for EscapedExpr {
    fn parse_recoverable(parser: &mut RecoverableContext, input: ParseStream) -> Option<Self> {
        let res = if input.peek(Token![if]) {
            EscapedExpr::If(parser.parse_recoverable(input)?)
        } else if input.peek(Token![for]) {
            EscapedExpr::For(parser.parse_recoverable(input)?)
        } else if input.peek(Token![match]) {
            EscapedExpr::Match(parser.parse_recoverable(input)?)
        } else {
            return None;
        };

        Some(res)
    }
}
#[cfg(feature = "extendable")]
impl TryIntoOrCloneRef<EscapeCode> for crate::ExtendableCustomNode {
    fn try_into_or_clone_ref(self) -> Either<EscapeCode, Self> {
        if let Some(val) = self.try_downcast_ref::<EscapeCode>() {
            Either::A(val.clone())
        } else {
            Either::B(self.clone())
        }
    }
    fn new_from_value(value: EscapeCode) -> Self {
        Self::from_value(value)
    }
}

#[derive(Clone, Debug, syn_derive::ToTokens)]
pub struct EscapeCode<T: ToTokens = Token![@]> {
    pub escape_token: T,
    pub expression: EscapedExpr,
}

impl<T: ToTokens + Parse> ParseRecoverable for EscapeCode<T> {
    fn parse_recoverable(parser: &mut RecoverableContext, input: ParseStream) -> Option<Self> {
        let escape_token = parser.parse_simple(input)?;
        let expression = parser.parse_recoverable(input)?;

        Some(Self {
            escape_token,
            expression,
        })
    }
}

impl<T> CustomNode for EscapeCode<T>
where
    T: Parse + ToTokens,
{
    fn peek_element(input: syn::parse::ParseStream) -> bool {
        if input.parse::<T>().is_err() {
            return false;
        }
        input.peek(Token![if]) || input.peek(Token![for]) || input.peek(Token![match])
    }
}

pub mod visitor_impl {
    use rstml::visitor::{CustomNodeWalker, RustCode};

    use super::*;

    pub struct EscapeCodeWalker;
    impl CustomNodeWalker for EscapeCodeWalker {
        type Custom = CustomNodeType;
        fn walk_custom_node_fields<VisitorImpl>(
            visitor: &mut VisitorImpl,
            node: &mut Self::Custom,
        ) -> bool
        where
            VisitorImpl: Visitor<CustomNodeType>,
        {
            EscapeCode::visit_custom_children(visitor, node)
        }
    }

    impl Block {
        pub fn visit_custom_children<V: Visitor<CustomNodeType>>(
            visitor: &mut V,
            block: &mut Self,
        ) -> bool {
            block.body.iter_mut().all(|val| visitor.visit_node(val))
        }
    }

    impl ElseIf {
        pub fn visit_custom_children<V: Visitor<CustomNodeType>>(
            visitor: &mut V,
            expr: &mut Self,
        ) -> bool {
            visitor.visit_rust_code(RustCode::Expr(&mut expr.condition));
            Block::visit_custom_children(visitor, &mut expr.then_branch)
        }
    }

    impl Else {
        pub fn visit_custom_children<V: Visitor<CustomNodeType>>(
            visitor: &mut V,
            expr: &mut Self,
        ) -> bool {
            Block::visit_custom_children(visitor, &mut expr.then_branch)
        }
    }

    impl IfExpr {
        pub fn visit_custom_children<V: Visitor<CustomNodeType>>(
            visitor: &mut V,
            expr: &mut Self,
        ) -> bool {
            visitor.visit_rust_code(RustCode::Expr(&mut expr.condition));
            Block::visit_custom_children(visitor, &mut expr.then_branch)
                || expr
                    .else_ifs
                    .iter_mut()
                    .all(|val| ElseIf::visit_custom_children(visitor, val))
                || expr
                    .else_branch
                    .as_mut()
                    .map(|val| Else::visit_custom_children(visitor, val))
                    .unwrap_or(true)
        }
    }
    impl ForExpr {
        pub fn visit_custom_children<V: Visitor<CustomNodeType>>(
            visitor: &mut V,
            expr: &mut Self,
        ) -> bool {
            visitor.visit_rust_code(RustCode::Pat(&mut expr.pat));
            visitor.visit_rust_code(RustCode::Expr(&mut expr.expr));
            Block::visit_custom_children(visitor, &mut expr.block)
        }
    }
    impl Arm {
        pub fn visit_custom_children<V: Visitor<CustomNodeType>>(
            visitor: &mut V,
            expr: &mut Self,
        ) -> bool {
            visitor.visit_rust_code(RustCode::Pat(&mut expr.pat));
            Block::visit_custom_children(visitor, &mut expr.body)
        }
    }
    impl MatchExpr {
        pub fn visit_custom_children<V: Visitor<CustomNodeType>>(
            visitor: &mut V,
            expr: &mut Self,
        ) -> bool {
            visitor.visit_rust_code(RustCode::Expr(&mut expr.expr));

            expr.arms
                .iter_mut()
                .all(|val| Arm::visit_custom_children(visitor, val))
        }
    }
    impl EscapedExpr {
        pub fn visit_custom_children<V: Visitor<CustomNodeType>>(
            visitor: &mut V,
            expr: &mut Self,
        ) -> bool {
            match expr {
                EscapedExpr::If(expr) => IfExpr::visit_custom_children(visitor, expr),
                EscapedExpr::For(expr) => ForExpr::visit_custom_children(visitor, expr),
                EscapedExpr::Match(expr) => MatchExpr::visit_custom_children(visitor, expr),
            }
        }
    }
    impl EscapeCode {
        pub fn visit_custom_children<V: Visitor<CustomNodeType>>(
            visitor: &mut V,
            node: &mut CustomNodeType,
        ) -> bool {
            let Either::A(mut this): Either<EscapeCode, _> = node.clone().try_into_or_clone_ref()
            else {
                return true;
            };
            let result = EscapedExpr::visit_custom_children(visitor, &mut this.expression);
            *node = TryIntoOrCloneRef::new_from_value(this);
            result
        }
    }
}

#[cfg(test)]
#[cfg(not(feature = "extendable"))]
mod test_typed {
    use quote::quote;
    use rstml::{node::Node, recoverable::Recoverable, Parser, ParserConfig};
    use syn::{parse_quote, Token};

    use super::{EscapeCode, EscapedExpr};

    type MyCustomNode = EscapeCode<Token![@]>;
    type MyNode = Node<MyCustomNode>;
    #[test]
    fn if_complex_expression() {
        let actual: Recoverable<MyNode> = parse_quote! {
            @if just && an || expression {
                <div/>
            }
        };
        let Node::Custom(actual) = actual.inner() else {
            panic!()
        };

        let EscapedExpr::If(expr) = actual.expression else {
            panic!()
        };

        assert_eq!(expr.condition, parse_quote!(just && an || expression));
    }

    #[test]
    fn if_let_expr() {
        let actual: Recoverable<MyNode> = parse_quote! {
            @if let (foo) = Bar {
                <div/>
            }
        };
        let Node::Custom(actual) = actual.inner() else {
            panic!()
        };

        let EscapedExpr::If(expr) = actual.expression else {
            panic!()
        };

        assert_eq!(expr.condition, parse_quote!(let (foo) = Bar));
    }

    #[test]
    fn if_simple() {
        let actual: Recoverable<MyNode> = parse_quote! {
            @if foo > bar {
                <a regular="html" component/>
                <div>
                </div>
            }
        };

        let Node::Custom(actual) = actual.inner() else {
            panic!()
        };

        let EscapedExpr::If(expr) = &actual.expression else {
            panic!()
        };

        assert_eq!(expr.condition, parse_quote!(foo > bar));
    }

    #[test]
    fn if_else_if() {
        let actual: Recoverable<MyNode> = parse_quote! {
            @if foo > bar {
                <first/>
            } else if foo == 2 {
                <second/>
            } else if foo == 3 {
                <third/>
            } else {
                <default/>
            }
        };

        let Node::Custom(actual) = actual.inner() else {
            panic!()
        };

        let EscapedExpr::If(expr) = &actual.expression else {
            panic!()
        };

        assert_eq!(expr.condition, parse_quote!(foo > bar));
        assert_eq!(expr.else_ifs[0].condition, parse_quote!(foo == 2));
        assert_eq!(expr.else_ifs[1].condition, parse_quote!(foo == 3));
        assert!(expr.else_branch.is_some());
    }

    #[test]
    fn for_simple() {
        let actual: Recoverable<MyNode> = parse_quote! {
            @for x in foo {
                <div>
                {x}
                </div>
            }
        };

        let Node::Custom(actual) = actual.inner() else {
            panic!()
        };

        let EscapedExpr::For(expr) = &actual.expression else {
            panic!()
        };

        assert_eq!(expr.pat, parse_quote!(x));
        assert_eq!(expr.expr, parse_quote!(foo));
    }

    #[test]
    fn for_binding() {
        let actual: Recoverable<MyNode> = parse_quote! {
            @for (ref x, f) in foo {
                <div>
                {x}
                </div>
            }
        };

        let Node::Custom(actual) = actual.inner() else {
            panic!()
        };

        let EscapedExpr::For(expr) = &actual.expression else {
            panic!()
        };

        assert_eq!(expr.pat, parse_quote!((ref x, f)));
        assert_eq!(expr.expr, parse_quote!(foo));
    }

    #[test]
    fn match_simple() {
        let actual: Recoverable<MyNode> = parse_quote! {
            @match foo {
                x => {<x/>}
                _ => {<default/>}
            }
        };

        let Node::Custom(actual) = actual.inner() else {
            panic!()
        };

        let EscapedExpr::Match(expr) = &actual.expression else {
            panic!()
        };

        assert_eq!(expr.expr, parse_quote!(foo));
        assert_eq!(expr.arms[0].pat, parse_quote!(x));
        assert_eq!(expr.arms[1].pat, parse_quote!(_));
    }

    #[test]
    fn match_complex_exprs() {
        let actual: Recoverable<MyNode> = parse_quote! {
            @match foo > 2 * 2 {
                Some(x) => {<x/>}
                y|z => {<default/>}
            }
        };

        let Node::Custom(actual) = actual.inner() else {
            panic!()
        };

        let EscapedExpr::Match(expr) = &actual.expression else {
            panic!()
        };

        assert_eq!(expr.expr, parse_quote!(foo > 2 * 2));
        assert_eq!(expr.arms[0].pat, parse_quote!(Some(x)));
        assert_eq!(expr.arms[1].pat, parse_quote!(y | z));
    }

    #[test]
    fn check_if_inside_if() {
        let actual: Recoverable<MyNode> = parse_quote! {
            @if just && an || expression {
                @if foo > bar {
                    <div/>
                }
            }
        };
        let Node::Custom(actual) = actual.inner() else {
            panic!()
        };

        let EscapedExpr::If(expr) = actual.expression else {
            panic!()
        };

        assert_eq!(expr.condition, parse_quote!(just && an || expression));
        let node = expr.then_branch.body.iter().next().unwrap();
        let Node::Custom(actual) = node else { panic!() };
        let EscapedExpr::If(expr) = &actual.expression else {
            panic!()
        };
        assert_eq!(expr.condition, parse_quote!(foo > bar));
    }

    #[test]
    fn for_inside_if() {
        let actual: Recoverable<MyNode> = parse_quote! {
            @if just && an || expression {
                @for x in foo {
                    <div/>
                }
            }
        };
        let Node::Custom(actual) = actual.inner() else {
            panic!()
        };

        let EscapedExpr::If(expr) = actual.expression else {
            panic!()
        };

        assert_eq!(expr.condition, parse_quote!(just && an || expression));
        let node = expr.then_branch.body.iter().next().unwrap();
        let Node::Custom(actual) = node else { panic!() };
        let EscapedExpr::For(expr) = &actual.expression else {
            panic!()
        };
        assert_eq!(expr.pat, parse_quote!(x));
        assert_eq!(expr.expr, parse_quote!(foo));
    }

    #[test]
    fn custom_node_using_config() {
        let actual = Parser::new(
            ParserConfig::new()
                .element_close_use_default_wildcard_ident(false)
                .custom_node::<MyCustomNode>(),
        )
        .parse_simple(quote! {
            @if just && an || expression {
                <a regular="html" component/>
                <div>
                </div>
            }
        })
        .unwrap();
        let Node::Custom(actual) = &actual[0] else {
            panic!()
        };

        let EscapedExpr::If(expr) = &actual.expression else {
            panic!()
        };

        assert_eq!(expr.condition, parse_quote!(just && an || expression));
    }
}

#[cfg(test)]
mod test_universal {
    use proc_macro2::TokenStream;
    use quote::quote;
    use rstml::{
        recoverable::Recoverable, visitor::visit_nodes_with_custom, ParserConfig, ParsingResult,
    };
    use syn::{parse_quote, visit_mut::VisitMut};

    use super::*;
    use crate::escape::visitor_impl::EscapeCodeWalker;

    // #[cfg(feature="extendable")]
    #[cfg(not(feature = "extendable"))]
    fn parse_universal(input: TokenStream) -> ParsingResult<Vec<Node>> {
        use rstml::Parser;

        let actual =
            Parser::new(ParserConfig::new().custom_node::<EscapeCode>()).parse_recoverable(input);

        return actual;
    }
    #[cfg(feature = "extendable")]
    fn parse_universal(input: TokenStream) -> ParsingResult<Vec<Node>> {
        use crate::ExtendableCustomNode;

        let result =
            ExtendableCustomNode::parse2_with_config::<(EscapeCode,)>(ParserConfig::new(), input);
        return result;
    }

    // For extendable custom node it is safe to use only after parsing.
    fn reparse_concrete<A: ToTokens>(val: A) -> EscapeCode {
        let recoverable: Recoverable<_> = parse_quote!(#val);
        recoverable.inner()
    }

    #[test]
    fn if_node_reparsable() {
        let tokens = quote! {
            @if just && an || expression {
                <div/>
            }
        };

        let actual = &parse_universal(tokens).into_result().unwrap()[0];

        let Node::Custom(actual) = actual else {
            panic!()
        };
        let actual = reparse_concrete(actual);

        let EscapedExpr::If(expr) = actual.expression else {
            panic!()
        };

        assert_eq!(expr.condition, parse_quote!(just && an || expression));
    }
    #[test]
    fn if_node_visitor_rstml() {
        let tokens = quote! {
            @if just && an || expression {
                <div/>
            }
        };

        let mut actual = parse_universal(tokens).into_result().unwrap();

        struct ElementVisitor {
            elements: Vec<String>,
        }
        impl<C: std::fmt::Debug> Visitor<C> for ElementVisitor {
            fn visit_element(&mut self, node: &mut rstml::node::NodeElement<C>) -> bool {
                self.elements.push(node.open_tag.name.to_string());
                true
            }
        }
        impl VisitMut for ElementVisitor {}

        let visitor = ElementVisitor {
            elements: Vec::new(),
        };
        let elements =
            visit_nodes_with_custom::<_, _, EscapeCodeWalker>(&mut actual, visitor).elements;

        assert_eq!(&elements, &["div"]);
    }

    #[test]
    fn if_node_visitor_syn_expr() {
        let tokens = quote! {
            @if just && an || expression {
                <div/>
            }
        };

        let mut actual = parse_universal(tokens).into_result().unwrap();

        struct ElementVisitor {
            exprs: Vec<String>,
        }
        impl<C: std::fmt::Debug> Visitor<C> for ElementVisitor {}
        impl VisitMut for ElementVisitor {
            fn visit_expr_mut(&mut self, exprs: &mut syn::Expr) {
                self.exprs.push(exprs.to_token_stream().to_string());
            }
        }

        let visitor = ElementVisitor { exprs: Vec::new() };
        let elements =
            visit_nodes_with_custom::<_, _, EscapeCodeWalker>(&mut actual, visitor).exprs;

        assert_eq!(&elements, &["just && an || expression"]);
    }
}