diff --git a/CHANGELOG b/CHANGELOG index a6e4a9dc1..08799ef54 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -366,7 +366,7 @@ # 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) diff --git a/src/ExpressionParser.php b/src/ExpressionParser.php index 6784ebfd1..6b1fc44fb 100644 --- a/src/ExpressionParser.php +++ b/src/ExpressionParser.php @@ -579,6 +579,11 @@ class ExpressionParser while (!$stream->test(/* Token::PUNCTUATION_TYPE */ 9, ')')) { if (!empty($args)) { $stream->expect(/* Token::PUNCTUATION_TYPE */ 9, ',', '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; + } } if ($definition) { diff --git a/tests/Fixtures/filters/trailing_commas.test b/tests/Fixtures/filters/trailing_commas.test new file mode 100644 index 000000000..25f1584ff --- /dev/null +++ b/tests/Fixtures/filters/trailing_commas.test @@ -0,0 +1,8 @@ +--TEST-- +filters allow trailing commas in their argument list +--TEMPLATE-- +{{ 42.55|round(1, 'floor',) }} +--DATA-- +return [] +--EXPECT-- +42.5 diff --git a/tests/Fixtures/functions/trailing_commas.test b/tests/Fixtures/functions/trailing_commas.test new file mode 100644 index 000000000..64bf4caf1 --- /dev/null +++ b/tests/Fixtures/functions/trailing_commas.test @@ -0,0 +1,8 @@ +--TEST-- +functions allow trailing commas in their argument list +--TEMPLATE-- +{{ max(1, 2, 3,) }} +--DATA-- +return [] +--EXPECT-- +3 diff --git a/tests/Fixtures/macros/trailing_commas.test b/tests/Fixtures/macros/trailing_commas.test new file mode 100644 index 000000000..600247a82 --- /dev/null +++ b/tests/Fixtures/macros/trailing_commas.test @@ -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') }}
+ {{- b|default('b') }}
+{%- endmacro %} +{% macro test2(a, b) -%} + {{ a|default('a') }}
+ {{- b|default('b') }}
+{%- endmacro %} + +{{ test.test(1, 2,) }} +{{ test.test(3, 4) }} +{{ test.test2(5, 6,) }} +{{ test.test2(7, 8) }} +--DATA-- +return [] +--EXPECT-- +1
2
+3
4
+5
6
+7
8