Skip to content

Commit 42c68ec

Browse files
committed
feat: also catch if-else
1 parent a7dd727 commit 42c68ec

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

packages/yew-macro/src/html_tree/html_block.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,40 @@ fn check_deprecated_html_call(expr: &Expr) -> TokenStream {
7979
}
8080
}
8181

82+
// Pattern 3: { if cond { html! { ... } } else { html! { ... } } }
83+
if let Expr::If(if_expr) = expr {
84+
if let Some(span) = if_branch_html_macro_span(if_expr) {
85+
return super::deprecated_call(
86+
span,
87+
"`html!` is not needed inside `if`/`else` branches. Use bare elements directly",
88+
);
89+
}
90+
}
91+
8292
TokenStream::new()
8393
}
8494

95+
/// Walk through an `if`/`else if`/`else` chain and return the span of the first tail `html!` call.
96+
fn if_branch_html_macro_span(if_expr: &syn::ExprIf) -> Option<proc_macro2::Span> {
97+
if let Some(span) = if_expr
98+
.then_branch
99+
.stmts
100+
.last()
101+
.and_then(stmt_tail_html_macro_span)
102+
{
103+
return Some(span);
104+
}
105+
match if_expr.else_branch.as_ref().map(|(_, expr)| expr.as_ref()) {
106+
Some(Expr::Block(block_expr)) => block_expr
107+
.block
108+
.stmts
109+
.last()
110+
.and_then(stmt_tail_html_macro_span),
111+
Some(Expr::If(nested)) => if_branch_html_macro_span(nested),
112+
_ => None,
113+
}
114+
}
115+
85116
/// Check if a statement is a tail `html!`/`html_nested!` macro call (no trailing semicolon).
86117
fn stmt_tail_html_macro_span(stmt: &Stmt) -> Option<proc_macro2::Span> {
87118
match stmt {

packages/yew-macro/tests/html_deprecation/fail.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ fn nested_block_with_html_tail() {
8181
};
8282
}
8383

84+
fn if_else_block_with_html_branches() {
85+
let cond = true;
86+
let _ = html! {
87+
<div>{ if cond { html! { <span>{"yes"}</span> } } else { html! { <span>{"no"}</span> } } }</div>
88+
};
89+
}
90+
8491
fn main() {
8592
compile_error!("This macro call exists to deliberately fail the compilation of the test so we can verify output of deprecation lints");
8693
}

packages/yew-macro/tests/html_deprecation/fail.stderr

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
error: This macro call exists to deliberately fail the compilation of the test so we can verify output of deprecation lints
2-
--> tests/html_deprecation/fail.rs:85:5
2+
--> tests/html_deprecation/fail.rs:92:5
33
|
4-
85 | compile_error!("This macro call exists to deliberately fail the compilation of the test so we can verify output of deprecation lints...
4+
92 | compile_error!("This macro call exists to deliberately fail the compilation of the test so we can verify output of deprecation lints...
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66

77
warning: use of deprecated function `root_level_fragment::__yew_deprecated`: unnecessary `<>...</>`. Children can be placed directly in the body
@@ -81,3 +81,9 @@ warning: use of deprecated function `nested_block_with_html_tail::__yew_deprecat
8181
|
8282
80 | <div>{{ let processed = item.to_uppercase(); html! { <span>{processed}</span> } }}</div>
8383
| ^^^^
84+
85+
warning: use of deprecated function `if_else_block_with_html_branches::__yew_deprecated`: `html!` is not needed inside `if`/`else` branches. Use bare elements directly
86+
--> tests/html_deprecation/fail.rs:87:26
87+
|
88+
87 | <div>{ if cond { html! { <span>{"yes"}</span> } } else { html! { <span>{"no"}</span> } } }</div>
89+
| ^^^^

0 commit comments

Comments
 (0)