Skip to content

Commit 1f98c59

Browse files
its-the-shrimpgeoffjay
authored andcommitted
Remove deprecated class=(...) syntax (yewstack#3497)
* removed class=(...) syntax * made DynamicName::expr a group instead of a block * fixed tests, silenced all unused_must_use warnings * fixed wasm test
1 parent f32bf41 commit 1f98c59

File tree

17 files changed

+172
-264
lines changed

17 files changed

+172
-264
lines changed

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

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use proc_macro2::{Delimiter, Span, TokenStream};
1+
use proc_macro2::{Delimiter, Group, Span, TokenStream};
22
use proc_macro_error::emit_warning;
33
use quote::{quote, quote_spanned, ToTokens};
44
use syn::buffer::Cursor;
55
use syn::parse::{Parse, ParseStream};
66
use syn::spanned::Spanned;
7-
use syn::{Block, Expr, Ident, Lit, LitStr, Token};
7+
use syn::{Expr, Ident, Lit, LitStr, Token};
88

99
use super::{HtmlChildrenTree, HtmlDashedName, TagTokens};
10-
use crate::props::{ClassesForm, ElementProps, Prop, PropDirective};
10+
use crate::props::{ElementProps, Prop, PropDirective};
1111
use crate::stringify::{Stringify, Value};
1212
use crate::{is_ide_completion, non_capitalized_ascii, Peek, PeekValue};
1313

@@ -190,39 +190,11 @@ impl ToTokens for HtmlElement {
190190
))
191191
},
192192
);
193-
let class_attr = classes.as_ref().and_then(|classes| match classes {
194-
ClassesForm::Tuple(classes) => {
195-
let span = classes.span();
196-
let classes: Vec<_> = classes.elems.iter().collect();
197-
let n = classes.len();
198-
199-
let deprecation_warning = quote_spanned! {span=>
200-
#[deprecated(
201-
note = "the use of `(...)` with the attribute `class` is deprecated and will be removed in version 0.19. Use the `classes!` macro instead."
202-
)]
203-
fn deprecated_use_of_class() {}
204-
205-
if false {
206-
deprecated_use_of_class();
207-
};
208-
};
209193

210-
Some((
211-
LitStr::new("class", span),
212-
Value::Dynamic(quote! {
213-
{
214-
#deprecation_warning
215-
216-
let mut __yew_classes = ::yew::html::Classes::with_capacity(#n);
217-
#(__yew_classes.push(#classes);)*
218-
__yew_classes
219-
}
220-
}),
221-
None,
222-
))
223-
}
224-
ClassesForm::Single(classes) => {
225-
match classes.try_into_lit() {
194+
let class_attr =
195+
classes
196+
.as_ref()
197+
.and_then(|classes| match classes.value.try_into_lit() {
226198
Some(lit) => {
227199
if lit.value().is_empty() {
228200
None
@@ -235,17 +207,16 @@ impl ToTokens for HtmlElement {
235207
}
236208
}
237209
None => {
210+
let expr = &classes.value;
238211
Some((
239-
LitStr::new("class", classes.span()),
212+
LitStr::new("class", classes.label.span()),
240213
Value::Dynamic(quote! {
241-
::std::convert::Into::<::yew::html::Classes>::into(#classes)
214+
::std::convert::Into::<::yew::html::Classes>::into(#expr)
242215
}),
243216
None,
244217
))
245218
}
246-
}
247-
}
248-
});
219+
});
249220

250221
fn apply_as(directive: Option<&PropDirective>) -> TokenStream {
251222
match directive {
@@ -383,7 +354,7 @@ impl ToTokens for HtmlElement {
383354
}
384355
TagName::Expr(name) => {
385356
let vtag = Ident::new("__yew_vtag", name.span());
386-
let expr = &name.expr;
357+
let expr = name.expr.as_ref().map(Group::stream);
387358
let vtag_name = Ident::new("__yew_vtag_name", expr.span());
388359

389360
let void_children = Ident::new("__yew_void_children", Span::mixed_site());
@@ -411,10 +382,6 @@ impl ToTokens for HtmlElement {
411382
// this way we get a nice error message (with the correct span) when the expression
412383
// doesn't return a valid value
413384
quote_spanned! {expr.span()=> {
414-
#[allow(unused_braces)]
415-
// e.g. html!{<@{"div"}/>} will set `#expr` to `{"div"}`
416-
// (note the extra braces). Hence the need for the `allow`.
417-
// Anyways to remove the braces?
418385
let mut #vtag_name = ::std::convert::Into::<
419386
::yew::virtual_dom::AttrValue
420387
>::into(#expr);
@@ -500,7 +467,7 @@ fn wrap_attr_value<T: ToTokens>(value: T) -> TokenStream {
500467

501468
pub struct DynamicName {
502469
at: Token![@],
503-
expr: Option<Block>,
470+
expr: Option<Group>,
504471
}
505472

506473
impl Peek<'_, ()> for DynamicName {
@@ -524,12 +491,7 @@ impl Parse for DynamicName {
524491
fn parse(input: ParseStream) -> syn::Result<Self> {
525492
let at = input.parse()?;
526493
// the expression block is optional, closing tags don't have it.
527-
let expr = if input.cursor().group(Delimiter::Brace).is_some() {
528-
Some(input.parse()?)
529-
} else {
530-
None
531-
};
532-
494+
let expr = input.parse().ok();
533495
Ok(Self { at, expr })
534496
}
535497
}

packages/yew-macro/src/props/element.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,13 @@ use std::collections::HashSet;
22

33
use once_cell::sync::Lazy;
44
use syn::parse::{Parse, ParseStream};
5-
use syn::{Expr, ExprTuple};
65

76
use super::{Prop, Props, SpecialProps};
87

9-
pub enum ClassesForm {
10-
Tuple(ExprTuple),
11-
Single(Box<Expr>),
12-
}
13-
impl ClassesForm {
14-
fn from_expr(expr: Expr) -> Self {
15-
match expr {
16-
Expr::Tuple(expr_tuple) => ClassesForm::Tuple(expr_tuple),
17-
expr => ClassesForm::Single(Box::new(expr)),
18-
}
19-
}
20-
}
21-
228
pub struct ElementProps {
239
pub attributes: Vec<Prop>,
2410
pub listeners: Vec<Prop>,
25-
pub classes: Option<ClassesForm>,
11+
pub classes: Option<Prop>,
2612
pub booleans: Vec<Prop>,
2713
pub value: Option<Prop>,
2814
pub checked: Option<Prop>,
@@ -42,9 +28,7 @@ impl Parse for ElementProps {
4228
let booleans =
4329
props.drain_filter(|prop| BOOLEAN_SET.contains(prop.label.to_string().as_str()));
4430

45-
let classes = props
46-
.pop("class")
47-
.map(|prop| ClassesForm::from_expr(prop.value));
31+
let classes = props.pop("class");
4832
let value = props.pop("value");
4933
let checked = props.pop("checked");
5034
let special = props.special;

packages/yew-macro/tests/html_macro/block-pass.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,27 @@ pub struct u8;
3737
pub struct usize;
3838

3939
fn main() {
40-
::yew::html! { <>{ "Hi" }</> };
41-
::yew::html! { <>{ ::std::format!("Hello") }</> };
42-
::yew::html! { <>{ ::std::string::ToString::to_string("Hello") }</> };
40+
_ = ::yew::html! { <>{ "Hi" }</> };
41+
_ = ::yew::html! { <>{ ::std::format!("Hello") }</> };
42+
_ = ::yew::html! { <>{ ::std::string::ToString::to_string("Hello") }</> };
4343

4444
let msg = "Hello";
45-
::yew::html! { <div>{ msg }</div> };
45+
_ = ::yew::html! { <div>{ msg }</div> };
4646

4747
let subview = ::yew::html! { "subview!" };
48-
::yew::html! { <div>{ subview }</div> };
48+
_ = ::yew::html! { <div>{ subview }</div> };
4949

5050
let subview = || ::yew::html! { "subview!" };
51-
::yew::html! { <div>{ subview() }</div> };
51+
_ = ::yew::html! { <div>{ subview() }</div> };
5252

53-
::yew::html! {
53+
_ = ::yew::html! {
5454
<ul>
5555
{ for ::std::iter::Iterator::map(0..3, |num| { ::yew::html! { <span>{ num }</span> }}) }
5656
</ul>
5757
};
5858

5959
let item = |num| ::yew::html! { <li>{ ::std::format!("item {}!", num) }</li> };
60-
::yew::html! {
60+
_ = ::yew::html! {
6161
<ul>
6262
{ for ::std::iter::Iterator::map(0..3, item) }
6363
</ul>

packages/yew-macro/tests/html_macro/component-any-children-pass.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@ pub fn RenderPropComp(_props: &RenderPropProps) -> ::yew::Html {
159159
}
160160

161161
fn compile_pass() {
162-
::yew::html! { <Child int=1 /> };
163-
::yew::html! { <Child int=1 r#fn=1 /> };
162+
_ = ::yew::html! { <Child int=1 /> };
163+
_ = ::yew::html! { <Child int=1 r#fn=1 /> };
164164

165-
::yew::html! {
165+
_ = ::yew::html! {
166166
<>
167167
<Child int=1 />
168168
<scoped::Child int=1 />
@@ -171,7 +171,7 @@ fn compile_pass() {
171171

172172
let props = <<Child as ::yew::Component>::Properties as ::std::default::Default>::default();
173173
let node_ref = <::yew::NodeRef as ::std::default::Default>::default();
174-
::yew::html! {
174+
_ = ::yew::html! {
175175
<>
176176
<Child ..::std::clone::Clone::clone(&props) />
177177
<Child int={1} ..props />
@@ -183,7 +183,7 @@ fn compile_pass() {
183183
</>
184184
};
185185

186-
::yew::html! {
186+
_ = ::yew::html! {
187187
<>
188188
<Child int=1 string="child" />
189189
<Child int=1 />
@@ -199,17 +199,17 @@ fn compile_pass() {
199199
};
200200

201201
let name_expr = "child";
202-
::yew::html! {
202+
_ = ::yew::html! {
203203
<Child int=1 string={name_expr} />
204204
};
205205

206206
let string = "child";
207207
let int = 1;
208-
::yew::html! {
208+
_ = ::yew::html! {
209209
<Child {int} {string} />
210210
};
211211

212-
::yew::html! {
212+
_ = ::yew::html! {
213213
<>
214214
<Child int=1 />
215215
<Child int=1 optional_callback={::std::option::Option::Some(<::yew::Callback<()> as ::std::convert::From<_>>::from(|_| ()))} />
@@ -219,15 +219,15 @@ fn compile_pass() {
219219
};
220220

221221
let node_ref = <::yew::NodeRef as ::std::default::Default>::default();
222-
::yew::html! {
222+
_ = ::yew::html! {
223223
<>
224224
<Child int=1 r#ref={node_ref} />
225225
</>
226226
};
227227

228228
let int = 1;
229229
let node_ref = <::yew::NodeRef as ::std::default::Default>::default();
230-
::yew::html! {
230+
_ = ::yew::html! {
231231
<>
232232
<Child {int} r#ref={node_ref} />
233233
</>
@@ -236,7 +236,7 @@ fn compile_pass() {
236236
let props = <<Container as ::yew::Component>::Properties as ::std::default::Default>::default();
237237
let child_props =
238238
<<Child as ::yew::Component>::Properties as ::std::default::Default>::default();
239-
::yew::html! {
239+
_ = ::yew::html! {
240240
<>
241241
<Container int=1 />
242242
<Container int=1></Container>
@@ -292,7 +292,7 @@ fn compile_pass() {
292292
]
293293
};
294294

295-
::yew::html! {
295+
_ = ::yew::html! {
296296
<>
297297
{
298298
::std::iter::Iterator::collect::<::yew::virtual_dom::VNode>(
@@ -321,9 +321,9 @@ fn compile_pass() {
321321
</>
322322
};
323323

324-
::yew::html_nested! { 1 };
324+
_ = ::yew::html_nested! { 1 };
325325

326-
::yew::html! {
326+
_ = ::yew::html! {
327327
<RenderPropComp>
328328
{|_arg| {}}
329329
</RenderPropComp>

0 commit comments

Comments
 (0)