-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: while, break/continue/return, and preamble statements in html! #4124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ee86510
feat: support break and continue in for loops
Madoshakalaka 1f2de66
Merge branch 'master' into break-continue
Madoshakalaka e60aab5
feat: while loop support and website docs
Madoshakalaka 1b04187
fix: emit break/continue as bare statements for edition 2021 compact
Madoshakalaka 6643760
feat: dedup while and for loop code and admit label limitation
Madoshakalaka f509aed
feat(yew-macro): Any Rust statement in html! loop/if/match preamble
Madoshakalaka 5a18a72
feat(yew-macro): labeled break/continue without `;` and bare return a…
Madoshakalaka eb71e51
fix: clearer error for `break`/`continue`/`return` with HTML in match…
Madoshakalaka b9fe62a
feat(yew-macro): hint imperative for/while/loop in html preamble
Madoshakalaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| use proc_macro2::{Ident, TokenStream}; | ||
| use quote::{ToTokens, quote}; | ||
| use syn::buffer::Cursor; | ||
| use syn::parse::{Parse, ParseStream}; | ||
| use syn::spanned::Spanned; | ||
| use syn::token::While; | ||
| use syn::{Expr, Local, Stmt, Token, braced}; | ||
|
|
||
| use super::{HtmlChildrenTree, ToNodeIterator}; | ||
| use crate::PeekValue; | ||
| use crate::html_tree::HtmlTree; | ||
| use crate::html_tree::html_for::is_contextless_pure; | ||
|
|
||
| pub struct HtmlWhile { | ||
| cond: Box<Expr>, | ||
| let_stmts: Vec<Local>, | ||
| body: HtmlChildrenTree, | ||
| deprecations: TokenStream, | ||
| } | ||
|
|
||
| impl PeekValue<()> for HtmlWhile { | ||
| fn peek(cursor: Cursor) -> Option<()> { | ||
| let (ident, _) = cursor.ident()?; | ||
| (ident == "while").then_some(()) | ||
| } | ||
| } | ||
|
|
||
| impl Parse for HtmlWhile { | ||
| fn parse(input: ParseStream) -> syn::Result<Self> { | ||
| While::parse(input)?; | ||
| let cond = Box::new(input.call(Expr::parse_without_eager_brace)?); | ||
| match &*cond { | ||
| Expr::Block(syn::ExprBlock { block, .. }) if block.stmts.is_empty() => { | ||
| return Err(syn::Error::new( | ||
| cond.span(), | ||
| "missing condition for `while` expression", | ||
| )); | ||
| } | ||
| _ => {} | ||
| } | ||
| if input.is_empty() { | ||
| return Err(syn::Error::new( | ||
| cond.span(), | ||
| "this `while` expression has a condition, but no block", | ||
| )); | ||
| } | ||
|
|
||
| let body_stream; | ||
| braced!(body_stream in input); | ||
|
|
||
| let mut let_stmts = Vec::new(); | ||
| while body_stream.peek(Token![let]) { | ||
| let stmt: Stmt = body_stream.parse()?; | ||
| match stmt { | ||
| Stmt::Local(local) => let_stmts.push(local), | ||
| _ => unreachable!("peeked Token![let] but parsed non-local statement"), | ||
| } | ||
| } | ||
|
|
||
| let body = HtmlChildrenTree::parse_delimited_with_nodes(&body_stream)?; | ||
| let deprecations = super::check_unnecessary_fragment(&body); | ||
| // TODO: more concise code by using if-let guards (MSRV 1.95) | ||
| for child in body.0.iter() { | ||
| let HtmlTree::Element(element) = child else { | ||
| continue; | ||
| }; | ||
|
|
||
| let Some(key) = &element.props.special.key else { | ||
| continue; | ||
| }; | ||
|
|
||
| if is_contextless_pure(&key.value) { | ||
| return Err(syn::Error::new( | ||
| key.value.span(), | ||
| "duplicate key for a node in a `while`-loop\nthis will create elements with \ | ||
| duplicate keys if the loop iterates more than once", | ||
| )); | ||
| } | ||
| } | ||
| Ok(Self { | ||
| cond, | ||
| let_stmts, | ||
| body, | ||
| deprecations, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl ToTokens for HtmlWhile { | ||
| fn to_tokens(&self, tokens: &mut TokenStream) { | ||
| let Self { | ||
| cond, | ||
| let_stmts, | ||
| body, | ||
| deprecations, | ||
| } = self; | ||
| let acc = Ident::new("__yew_v", cond.span()); | ||
|
|
||
| let alloc_opt = body | ||
| .size_hint() | ||
| .filter(|&size| size > 1) // explicitly reserving space for 1 more element is redundant | ||
| .map(|size| quote!( #acc.reserve(#size) )); | ||
|
|
||
| let vlist_gen = match body.fully_keyed() { | ||
| Some(true) => quote! { | ||
| ::yew::virtual_dom::VList::__macro_new( | ||
| #acc, | ||
| ::std::option::Option::None, | ||
| ::yew::virtual_dom::FullyKeyedState::KnownFullyKeyed | ||
| ) | ||
| }, | ||
| Some(false) => quote! { | ||
| ::yew::virtual_dom::VList::__macro_new( | ||
| #acc, | ||
| ::std::option::Option::None, | ||
| ::yew::virtual_dom::FullyKeyedState::KnownMissingKeys | ||
| ) | ||
| }, | ||
| None => quote! { | ||
| ::yew::virtual_dom::VList::with_children(#acc, ::std::option::Option::None) | ||
| }, | ||
| }; | ||
|
|
||
| let body = body | ||
| .0 | ||
| .iter() | ||
| .map(|child| match child.to_node_iterator_stream() { | ||
| Some(child) => { | ||
| quote!( #acc.extend(#child) ) | ||
| } | ||
| _ => { | ||
| quote!( #acc.push(::std::convert::Into::into(#child)) ) | ||
|
WorldSEnder marked this conversation as resolved.
Outdated
|
||
| } | ||
| }); | ||
|
|
||
| tokens.extend(quote!({ | ||
| #deprecations | ||
| let mut #acc = ::std::vec::Vec::<::yew::virtual_dom::VNode>::new(); | ||
| while #cond { | ||
| #(#let_stmts)* #alloc_opt; #(#body);* | ||
| } | ||
| #vlist_gen | ||
| })) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| mod smth { | ||
| const KEY: u32 = 42; | ||
| } | ||
|
|
||
| fn main() { | ||
| _ = ::yew::html!{while}; | ||
| _ = ::yew::html!{while true}; | ||
| _ = ::yew::html!{while {} { <div/> }}; | ||
|
|
||
| _ = ::yew::html!{while true { | ||
| <div key="duplicate" /> | ||
| }}; | ||
|
|
||
| _ = ::yew::html!{while true { | ||
| <div key={smth::KEY} /> | ||
| }}; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| error: unexpected end of input, expected an expression | ||
| --> tests/html_macro/while-fail.rs:6:9 | ||
| | | ||
| 6 | _ = ::yew::html!{while}; | ||
| | ^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this error originates in the macro `::yew::html` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
|
||
| error: this `while` expression has a condition, but no block | ||
| --> tests/html_macro/while-fail.rs:7:28 | ||
| | | ||
| 7 | _ = ::yew::html!{while true}; | ||
| | ^^^^ | ||
|
|
||
| error: missing condition for `while` expression | ||
| --> tests/html_macro/while-fail.rs:8:28 | ||
| | | ||
| 8 | _ = ::yew::html!{while {} { <div/> }}; | ||
| | ^^ | ||
|
|
||
| error: duplicate key for a node in a `while`-loop | ||
| this will create elements with duplicate keys if the loop iterates more than once | ||
| --> tests/html_macro/while-fail.rs:11:18 | ||
| | | ||
| 11 | <div key="duplicate" /> | ||
| | ^^^^^^^^^^^ | ||
|
|
||
| error: duplicate key for a node in a `while`-loop | ||
| this will create elements with duplicate keys if the loop iterates more than once | ||
| --> tests/html_macro/while-fail.rs:15:19 | ||
| | | ||
| 15 | <div key={smth::KEY} /> | ||
| | ^^^^ |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.