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

Allow trailing commas in macros + functions + filters #3363

Merged
merged 1 commit into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 1.43.1 (2020-XX-XX)

* n/a
* Allow trailing commas in argument lists (in calls as well as definitions)

# 1.43.0 (2020-07-05)

Expand Down
5 changes: 5 additions & 0 deletions src/ExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,11 @@ public function parseArguments($namedArguments = false, $definition = false, $al
while (!$stream->test(Token::PUNCTUATION_TYPE, ')')) {
if (!empty($args)) {
$stream->expect(Token::PUNCTUATION_TYPE, ',', 'Arguments must be separated by a comma');

// if the comma above was a trailing comma, early exit the argument parse loop
if ($stream->test(/* Token::PUNCTUATION_TYPE */ 9, ')')) {
break;
}
Copy link
Contributor Author

@apfelbox apfelbox Jul 6, 2020

Choose a reason for hiding this comment

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

The nice part of this addition here is that it prevents usage with no arguments.

So _self.test(a,) is allowed as macro call, _self.test(,) isn't however.

}

if ($definition) {
Expand Down
8 changes: 8 additions & 0 deletions tests/Fixtures/filters/trailing_commas.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
filters allow trailing commas in their argument list
--TEMPLATE--
{{ 42.55|round(1, 'floor',) }}
--DATA--
return []
--EXPECT--
42.5
8 changes: 8 additions & 0 deletions tests/Fixtures/functions/trailing_commas.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
functions allow trailing commas in their argument list
--TEMPLATE--
{{ max(1, 2, 3,) }}
--DATA--
return []
--EXPECT--
3
25 changes: 25 additions & 0 deletions tests/Fixtures/macros/trailing_commas.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
macros allow trailing commas in their argument and parameter list
--TEMPLATE--
{% import _self as test %}

{% macro test(a, b,) -%}
{{ a|default('a') }}<br />
{{- b|default('b') }}<br />
{%- endmacro %}
{% macro test2(a, b) -%}
{{ a|default('a') }}<br />
{{- b|default('b') }}<br />
{%- endmacro %}

{{ test.test(1, 2,) }}
{{ test.test(3, 4) }}
{{ test.test2(5, 6,) }}
{{ test.test2(7, 8) }}
--DATA--
return []
--EXPECT--
1<br />2<br />
3<br />4<br />
5<br />6<br />
7<br />8<br />