From ccc2dbb043418cc0575552938a97d11da12f19d9 Mon Sep 17 00:00:00 2001 From: Jannik Zschiesche Date: Mon, 6 Jul 2020 10:34:46 +0200 Subject: [PATCH] Allow trailing commas in macros + functions + filters --- CHANGELOG | 2 +- src/ExpressionParser.php | 5 ++++ tests/Fixtures/filters/trailing_commas.test | 8 ++++++ tests/Fixtures/functions/trailing_commas.test | 8 ++++++ tests/Fixtures/macros/trailing_commas.test | 25 +++++++++++++++++++ 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/Fixtures/filters/trailing_commas.test create mode 100644 tests/Fixtures/functions/trailing_commas.test create mode 100644 tests/Fixtures/macros/trailing_commas.test diff --git a/CHANGELOG b/CHANGELOG index 41022f515..c0ebddbce 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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) diff --git a/src/ExpressionParser.php b/src/ExpressionParser.php index 8bddaf577..1acd48a53 100644 --- a/src/ExpressionParser.php +++ b/src/ExpressionParser.php @@ -598,6 +598,11 @@ class ExpressionParser 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; + } } 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