Skip to content
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

Improve error when whitespace control is used on extends #954

Merged
merged 3 commits into from
Jan 22, 2024

Conversation

GuillaumeGomez
Copy link
Contributor

Took me a while to understand why askama was complaining about an extends. Realized that it was because whitespace control is not allowed on it. Even though it does nothing, I think it's not great user experience to that this opaque error with valid jinja code so I updated the parser to allow it.

@djc
Copy link
Owner

djc commented Jan 22, 2024

I think I'd prefer to improve the error message instead of allowing it in the parser as it could be confusing to have it doing nothing.

@GuillaumeGomez
Copy link
Contributor Author

I just realized:

#[template(source = r#"{% extends "bla.html" %}

hello"#, ext = "html")]

What if I wanted to keep the whitespaces before "hello"? I'd need to add an empty comment right after extends, which isn't great either. With this, shouldn't it be better to actually handle whitespace control in extends?

@djc
Copy link
Owner

djc commented Jan 22, 2024

I think the whitespace outside extends is always ignored because the top-level flow comes from the base template, and the child template only picks up things inside blocks. So it is actually ignored AFAIK.

@GuillaumeGomez
Copy link
Contributor Author

Yep, seems like a weird way to handle it. Well, what do you want me to do in this case?

@djc
Copy link
Owner

djc commented Jan 22, 2024

How hard would it be to emit a good error message for this? Maybe just parse it like it's allowed but then emit an error from the parser if any of the whitespace controls are non-default for extends?

@GuillaumeGomez
Copy link
Contributor Author

Pretty easy. Updating.

@GuillaumeGomez GuillaumeGomez changed the title Allow to have whitespace control on extends Improve error when whitespace control is used on extends Jan 22, 2024
@GuillaumeGomez
Copy link
Contributor Author

GuillaumeGomez commented Jan 22, 2024

Done.

Comment on lines 938 to 946
if pws.is_some() || nws.is_some() {
Err(nom::Err::Failure(ErrorContext {
input: start,
message: Some(Cow::Borrowed(
"whitespace control is not allowed on `extends`",
)),
}))
} else {
Ok((i, Self { path }))
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'd prefer to either write this with an early return (probably for the Ok case) or just as a match:

match (pws, nws) {
     (None, None) => Ok((i, Self { path })),
     (_, _) => Err(..),
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Err(nom::Err::Failure(ErrorContext {
input: start,
message: Some(Cow::Borrowed(
"whitespace control is not allowed on `extends`",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this mention something about why this is the case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum... I think it'd better to mention this in the book instead.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can live with that.

@GuillaumeGomez
Copy link
Contributor Author

Done.

Copy link
Owner

@djc djc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for all the nits!

book/src/template_syntax.md Outdated Show resolved Hide resolved
@GuillaumeGomez
Copy link
Contributor Author

Sorry for all the nits!

Nah it's fine. I don't mind the review rounds.

@GuillaumeGomez GuillaumeGomez merged commit ef53238 into djc:main Jan 22, 2024
20 checks passed
@GuillaumeGomez GuillaumeGomez deleted the extends-ws branch January 22, 2024 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants