Allow trailing comma in macro definition and call

This commit is contained in:
Guillaume Gomez 2023-12-06 17:20:53 +01:00 committed by Dirkjan Ochtman
parent 7f30a657f6
commit e4b8ca3c44
3 changed files with 32 additions and 2 deletions

View File

@ -119,7 +119,7 @@ impl<'a> Expr<'a> {
}
}),
),
char(')'),
tuple((opt(ws(char(','))), char(')'))),
)),
)(i)
}

View File

@ -493,7 +493,7 @@ impl<'a> Macro<'a> {
delimited(
ws(char('(')),
separated_list0(char(','), ws(identifier)),
ws(char(')')),
tuple((opt(ws(char(','))), char(')'))),
)(i)
}

View File

@ -140,3 +140,33 @@ struct OnlyNamedArgument;
fn test_only_named_argument() {
assert_eq!(OnlyNamedArgument.render().unwrap(), "hi");
}
// Check for trailing commas.
#[derive(Template)]
#[template(
source = r#"{% macro button(label , ) %}
{{- label -}}
{% endmacro %}
{%- macro button2(label ,) %}
{% endmacro %}
{%- macro button3(label,) %}
{% endmacro %}
{%- macro button4(label, ) %}
{% endmacro %}
{%- macro button5(label ) %}
{% endmacro %}
{%- call button(label="hi" , ) -%}
{%- call button(label="hi" ,) -%}
{%- call button(label="hi",) -%}
{%- call button(label="hi", ) -%}
{%- call button(label="hi" ) -%}
"#,
ext = "html"
)]
struct TrailingComma;
#[test]
fn test_trailing_comma() {
assert_eq!(TrailingComma.render().unwrap(), "hihihihihi");
}