mirror of
https://github.com/askama-rs/askama.git
synced 2025-10-02 15:25:19 +00:00
Improve error for invalid name used in endblock
This commit is contained in:
parent
6dae920a45
commit
c3281e353d
@ -101,7 +101,7 @@ impl<'a> Ast<'a> {
|
||||
let msg = format!(
|
||||
"{}problems parsing template source at row {}, column {} near:\n{}",
|
||||
if let Some(message) = message {
|
||||
format!("error: {message}\n")
|
||||
format!("{message}\n")
|
||||
} else {
|
||||
String::new()
|
||||
},
|
||||
|
@ -1,3 +1,4 @@
|
||||
use std::borrow::Cow;
|
||||
use std::str;
|
||||
|
||||
use nom::branch::alt;
|
||||
@ -650,7 +651,13 @@ impl<'a> BlockDef<'a> {
|
||||
|i| s.tag_block_start(i),
|
||||
opt(Whitespace::parse),
|
||||
ws(keyword("endblock")),
|
||||
cut(tuple((opt(ws(keyword(name))), opt(Whitespace::parse)))),
|
||||
cut(tuple((
|
||||
opt(|before| {
|
||||
let (after, end_name) = ws(identifier)(before)?;
|
||||
check_end_name(before, after, name, end_name, "block")
|
||||
}),
|
||||
opt(Whitespace::parse),
|
||||
))),
|
||||
))),
|
||||
)));
|
||||
let (i, (nodes, (_, pws2, _, (_, nws2)))) = end(i)?;
|
||||
@ -667,6 +674,27 @@ impl<'a> BlockDef<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn check_end_name<'a>(
|
||||
before: &'a str,
|
||||
after: &'a str,
|
||||
name: &'a str,
|
||||
end_name: &'a str,
|
||||
kind: &str,
|
||||
) -> IResult<&'a str, &'a str, ErrorContext<&'a str>> {
|
||||
if name == end_name {
|
||||
return Ok((after, end_name));
|
||||
}
|
||||
let message = if name.is_empty() && !end_name.is_empty() {
|
||||
format!("unexpected name `{end_name}` in `end{kind}` tag for unnamed `{kind}`")
|
||||
} else {
|
||||
format!("expected name `{name}` in `end{kind}` tag, found `{end_name}`")
|
||||
};
|
||||
Err(nom::Err::Failure(ErrorContext {
|
||||
input: before,
|
||||
message: Some(Cow::Owned(message)),
|
||||
}))
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Lit<'a> {
|
||||
pub lws: &'a str,
|
||||
|
@ -1,4 +1,5 @@
|
||||
error: problems parsing template source at row 1, column 27 near:
|
||||
error: expected name `foo` in `endblock` tag, found `not_foo`
|
||||
problems parsing template source at row 1, column 27 near:
|
||||
"not_foo %}"
|
||||
--> tests/ui/name_mismatch_endblock.rs:3:10
|
||||
|
|
||||
|
Loading…
x
Reference in New Issue
Block a user