From e6054780769e0511e9c77843b02ba268395efc5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Wed, 15 May 2024 23:44:42 +0200 Subject: [PATCH] Better error message for `{% macro super %}` --- askama_parser/src/node.rs | 20 +++++++++----------- testing/tests/ui/macro-super.stderr | 5 +++-- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/askama_parser/src/node.rs b/askama_parser/src/node.rs index 545037a0..520485dd 100644 --- a/askama_parser/src/node.rs +++ b/askama_parser/src/node.rs @@ -7,7 +7,7 @@ use nom::character::complete::char; use nom::combinator::{ complete, consumed, cut, eof, map, map_res, not, opt, peek, recognize, value, }; -use nom::error::{Error, ErrorKind}; +use nom::error::ErrorKind; use nom::error_position; use nom::multi::{fold_many0, many0, many1, separated_list0, separated_list1}; use nom::sequence::{delimited, pair, preceded, terminated, tuple}; @@ -520,7 +520,13 @@ impl<'a> Macro<'a> { |i| s.tag_block_end(i), ))), )); - let (i, (pws1, _, (name, params, nws1, _))) = start(i)?; + let (j, (pws1, _, (name, params, nws1, _))) = start(i)?; + if name == "super" { + return Err(nom::Err::Failure(ErrorContext { + input: i, + message: Some(Cow::Borrowed("'super' is not a valid name for a macro")), + })); + } let mut end = cut(tuple(( |i| Node::many(i, s), @@ -537,15 +543,7 @@ impl<'a> Macro<'a> { )), ))), ))); - let (i, (contents, (_, pws2, _, nws2))) = end(i)?; - - if name == "super" { - // TODO: yield a a better error message here - return Err(ErrorContext::from_err(nom::Err::Failure(Error::new( - i, - ErrorKind::Fail, - )))); - } + let (i, (contents, (_, pws2, _, nws2))) = end(j)?; Ok(( i, diff --git a/testing/tests/ui/macro-super.stderr b/testing/tests/ui/macro-super.stderr index 0ff22b6e..2437cdf4 100644 --- a/testing/tests/ui/macro-super.stderr +++ b/testing/tests/ui/macro-super.stderr @@ -1,5 +1,6 @@ -error: failed to parse template source at row 1, column 35 near: - "%}" +error: 'super' is not a valid name for a macro + failed to parse template source at row 1, column 2 near: + "- macro super() -%}{%- endmacro -%}" --> tests/ui/macro-super.rs:3:10 | 3 | #[derive(Template)]