From 831e69b77e20efe49e102f322b602321a655c8b0 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 14 Oct 2024 10:41:48 +0200 Subject: [PATCH] Do not allow : as macro definition separator --- src/ExpressionParser.php | 2 +- .../colon_not_supported_as_default_separator.test | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 tests/Fixtures/tags/macro/colon_not_supported_as_default_separator.test diff --git a/src/ExpressionParser.php b/src/ExpressionParser.php index ddfbd2a47..9e3debeea 100644 --- a/src/ExpressionParser.php +++ b/src/ExpressionParser.php @@ -735,7 +735,7 @@ class ExpressionParser } $name = null; - if ($namedArguments && (($token = $stream->nextIf(Token::OPERATOR_TYPE, '=')) || ($token = $stream->nextIf(Token::PUNCTUATION_TYPE, ':')))) { + if ($namedArguments && (($token = $stream->nextIf(Token::OPERATOR_TYPE, '=')) || (!$definition && $token = $stream->nextIf(Token::PUNCTUATION_TYPE, ':')))) { if (!$value instanceof NameExpression) { throw new SyntaxError(\sprintf('A parameter name must be a string, "%s" given.', \get_class($value)), $token->getLine(), $stream->getSourceContext()); } diff --git a/tests/Fixtures/tags/macro/colon_not_supported_as_default_separator.test b/tests/Fixtures/tags/macro/colon_not_supported_as_default_separator.test new file mode 100644 index 000000000..6b0bb25fb --- /dev/null +++ b/tests/Fixtures/tags/macro/colon_not_supported_as_default_separator.test @@ -0,0 +1,10 @@ +--TEST-- +"macro" tag does not support : as a separator in definition, only = is supported +--TEMPLATE-- +{% macro test(foo: "foo") -%} + {{ foo }} +{%- endmacro %} +--DATA-- +return [] +--EXCEPTION-- +Twig\Error\SyntaxError: Arguments must be separated by a comma. Unexpected token "punctuation" of value ":" ("punctuation" expected with value ",") in "index.twig" at line 2.