Skip to content

Commit

Permalink
Resolve #839 regression issue introduced by #820
Browse files Browse the repository at this point in the history
  • Loading branch information
trivigy committed Jan 4, 2020
1 parent 8859986 commit 34dc935
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
42 changes: 28 additions & 14 deletions crates/macro/src/html_tree/html_iterable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,55 @@ use quote::{quote_spanned, ToTokens};
use syn::buffer::Cursor;
use syn::parse::{Parse, ParseStream, Result as ParseResult};
use syn::spanned::Spanned;
use syn::{Expr, Token};
use syn::{Expr, ExprIf, Token};

use proc_macro2::{Ident, Span};
use proc_macro2::Ident;
use syn::visit_mut::{self, VisitMut};
use syn::Macro;

pub struct HtmlIterable(Expr);

impl PeekValue<()> for HtmlIterable {
fn peek(cursor: Cursor) -> Option<()> {
let (ident, _) = cursor.ident()?;
(ident.to_string() == "for").as_option()
struct HtmlInnerYin;
impl VisitMut for HtmlInnerYin {
fn visit_expr_if_mut(&mut self, node: &mut ExprIf) {
if node.else_branch.is_some() {
HtmlInnerYang.visit_expr_if_mut(node);
} else {
visit_mut::visit_expr_if_mut(self, node);
}
}
}

struct HtmlInnerModifier;
impl VisitMut for HtmlInnerModifier {
fn visit_macro_mut(&mut self, node: &mut Macro) {
if node.path.is_ident("html") {
let ident = &mut node.path.segments.last_mut().unwrap().ident;
*ident = Ident::new("html_nested", Span::call_site());
*ident = Ident::new("html_nested", ident.span());
}

// Delegate to the default impl to visit any nested functions.
visit_mut::visit_macro_mut(self, node);
}
}

struct HtmlInnerYang;
impl VisitMut for HtmlInnerYang {
fn visit_macro_mut(&mut self, node: &mut Macro) {
visit_mut::visit_macro_mut(self, node);
}
}

pub struct HtmlIterable(Expr);

impl PeekValue<()> for HtmlIterable {
fn peek(cursor: Cursor) -> Option<()> {
let (ident, _) = cursor.ident()?;
(ident.to_string() == "for").as_option()
}
}

impl Parse for HtmlIterable {
fn parse(input: ParseStream) -> ParseResult<Self> {
let for_token = input.parse::<Token![for]>()?;

match input.parse() {
Ok(mut expr) => {
HtmlInnerModifier.visit_expr_mut(&mut expr);
HtmlInnerYin.visit_expr_mut(&mut expr);
Ok(HtmlIterable(expr))
}
Err(err) => {
Expand Down
29 changes: 21 additions & 8 deletions crates/macro/src/html_tree/html_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,35 @@ use quote::{quote, quote_spanned, ToTokens};
use syn::buffer::Cursor;
use syn::parse::{Parse, ParseStream, Result};
use syn::spanned::Spanned;
use syn::Expr;
use syn::Lit;
use syn::{Expr, ExprIf, Lit};

use proc_macro2::{Ident, Span};
use proc_macro2::Ident;
use syn::visit_mut::{self, VisitMut};
use syn::Macro;

struct HtmlInnerModifier;
impl VisitMut for HtmlInnerModifier {
struct HtmlInnerYin;
impl VisitMut for HtmlInnerYin {
fn visit_expr_if_mut(&mut self, node: &mut ExprIf) {
if node.else_branch.is_some() {
HtmlInnerYang.visit_expr_if_mut(node);
} else {
visit_mut::visit_expr_if_mut(self, node);
}
}

fn visit_macro_mut(&mut self, node: &mut Macro) {
if node.path.is_ident("html") {
let ident = &mut node.path.segments.last_mut().unwrap().ident;
*ident = Ident::new("html_nested", Span::call_site());
*ident = Ident::new("html_nested", ident.span());
}

// Delegate to the default impl to visit any nested functions.
visit_mut::visit_macro_mut(self, node);
}
}

struct HtmlInnerYang;
impl VisitMut for HtmlInnerYang {
fn visit_macro_mut(&mut self, node: &mut Macro) {
visit_mut::visit_macro_mut(self, node);
}
}
Expand All @@ -37,7 +50,7 @@ impl Parse for HtmlNode {
Node::Literal(lit)
} else {
let mut expr: Expr = input.parse()?;
HtmlInnerModifier.visit_expr_mut(&mut expr);
HtmlInnerYin.visit_expr_mut(&mut expr);
Node::Expression(expr)
};

Expand Down

0 comments on commit 34dc935

Please sign in to comment.