Skip to content

Commit 5927cc5

Browse files
committed
fix(yew-router-macro): reject old :param and *param route syntax
Under matchit, the old route-recognizer syntax silently becomes literal path segments, causing routes to never match. Emit a compile-time error with a suggested fix instead.
1 parent a51aa1c commit 5927cc5

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

packages/yew-router-macro/src/routable_derive.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,29 @@ fn parse_variants_attributes(
120120
));
121121
}
122122

123+
// Reject old route-recognizer `:param` / `*param` syntax that would
124+
// silently become literal path segments under matchit.
125+
for segment in val.split('/') {
126+
if let Some(name) = segment.strip_prefix(':') {
127+
return Err(syn::Error::new_spanned(
128+
&lit,
129+
format!(
130+
"route segments must not start with `:`. Use `{{{name}}}` to capture a \
131+
parameter.",
132+
),
133+
));
134+
}
135+
if let Some(name) = segment.strip_prefix('*') {
136+
return Err(syn::Error::new_spanned(
137+
&lit,
138+
format!(
139+
"route segments must not start with `*`. Use `{{*{name}}}` to capture a \
140+
wildcard.",
141+
),
142+
));
143+
}
144+
}
145+
123146
let route_params = extract_route_params(&val);
124147
if !route_params.is_empty() {
125148
let field_names: std::collections::HashSet<String> = match &variant.fields {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#[derive(yew_router::Routable, Debug, Clone, PartialEq)]
2+
enum Routes {
3+
#[at("/")]
4+
Home,
5+
#[at("/posts/:id")]
6+
Post { id: u32 },
7+
#[at("/files/*path")]
8+
File { path: String },
9+
}
10+
11+
fn main() {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: route segments must not start with `:`. Use `{id}` to capture a parameter.
2+
--> tests/routable_derive/old-param-syntax-fail.rs:5:10
3+
|
4+
5 | #[at("/posts/:id")]
5+
| ^^^^^^^^^^^^

0 commit comments

Comments
 (0)