Allow idents to start with _

This commit is contained in:
Guillaume Gomez 2024-11-14 23:10:18 +01:00
parent f86feddbb4
commit f39a846598
2 changed files with 16 additions and 1 deletions

View File

@ -633,7 +633,12 @@ fn path_or_identifier(i: &str) -> ParseResult<'_, PathOrIdentifier<'_>> {
path.extend(rest);
Ok((i, PathOrIdentifier::Path(path)))
}
(None, name, []) if name.chars().next().map_or(true, char::is_lowercase) => {
(None, name, [])
if name
.chars()
.next()
.map_or(true, |c| c == '_' || c.is_lowercase()) =>
{
Ok((i, PathOrIdentifier::Identifier(name)))
}
(None, start, tail) => {

View File

@ -21,3 +21,13 @@ fn let_macro() {
let template = A { y: false };
assert_eq!(template.render().unwrap(), "blob")
}
// Ensures that variables name can start with `_`.
#[test]
fn underscore() {
#[derive(Template)]
#[template(source = r#"{% let _x = 7 %}{{ _x }}"#, ext = "html")]
struct X;
assert_eq!(X.render().unwrap(), "7")
}