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 arrow functions everywhere #4378

Merged
merged 1 commit into from
Oct 8, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 3.15.0 (2024-XX-XX)

* Allow arrow functions everywhere
* Deprecate passing a string or an array to Twig callable arguments accepting arrow functions (pass a `\Closure`)
* Add support for triggering deprecations for future operator precedence changes
* Deprecate using the `not` unary operator in an expression with ``*``, ``/``, ``//``, or ``%`` without using explicit parentheses to clarify precedence
Expand Down
26 changes: 26 additions & 0 deletions doc/templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,32 @@ The following operators don't fit into any of the other categories:
Support for expanding the arguments of a function call was introduced in
Twig 3.15.

* ``=>``: The arrow operator allows the creation of functions. A function is
made of arguments (use parentheses for multiple arguments) and an arrow
(``=>``) followed by an expression to execute. The expression has access to
all passed arguments. Arrow functions are supported as arguments for filters,
functions, tests, macros, and method calls.

For instance, the built-in ``map``, ``reduce``, ``sort``, ``filter``, and
``find`` filters accept arrow functions as arguments:

.. code-block:: twig

{{ people|map(p => p.first_name)|join(', ') }}

Arrow functions can be stored in variables:

.. code-block:: twig

{% set first_name_fn = (p) => p.first_name %}

{{ people|map(first_name_fn)|join(', ') }}

.. versionadded:: 3.15

Arrow function support for functions, macros, and method calls was added in
Twig 3.15 (filters and tests were already supported).

Operators
~~~~~~~~~

Expand Down
24 changes: 16 additions & 8 deletions src/ExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ public function __construct(
}
}

public function parseExpression($precedence = 0, $allowArrow = false)
public function parseExpression($precedence = 0)
{
if ($allowArrow && $arrow = $this->parseArrow()) {
if (func_num_args() > 1) {
trigger_deprecation('twig/twig', '3.15', 'Passing a second argument ($allowArrow) to "%s()" is deprecated.', __METHOD__);
}

if ($arrow = $this->parseArrow()) {
return $arrow;
}

Expand All @@ -108,7 +112,7 @@ public function parseExpression($precedence = 0, $allowArrow = false)
} else {
$previous = $this->setDeprecationCheck(true);
try {
$expr1 = $this->parseExpression(self::OPERATOR_LEFT === $op['associativity'] ? $op['precedence'] + 1 : $op['precedence'], true);
$expr1 = $this->parseExpression(self::OPERATOR_LEFT === $op['associativity'] ? $op['precedence'] + 1 : $op['precedence']);
} finally {
$this->setDeprecationCheck($previous);
}
Expand Down Expand Up @@ -672,7 +676,7 @@ public function parseFilterExpressionRaw($node)
if (!$this->parser->getStream()->test(Token::PUNCTUATION_TYPE, '(')) {
$arguments = new EmptyNode();
} else {
$arguments = $this->parseArguments(true, false, true);
$arguments = $this->parseArguments(true);
}

$filter = $this->getFilter($token->getValue(), $token->getLine());
Expand Down Expand Up @@ -708,8 +712,12 @@ public function parseFilterExpressionRaw($node)
*
* @throws SyntaxError
*/
public function parseArguments($namedArguments = false, $definition = false, $allowArrow = false)
public function parseArguments($namedArguments = false, $definition = false)
{
if (func_num_args() > 2) {
trigger_deprecation('twig/twig', '3.15', 'Passing a third argument ($allowArrow) to "%s()" is deprecated.', __METHOD__);
}

$args = [];
$stream = $this->parser->getStream();

Expand All @@ -731,11 +739,11 @@ public function parseArguments($namedArguments = false, $definition = false, $al
} else {
if ($stream->nextIf(Token::SPREAD_TYPE)) {
$hasSpread = true;
$value = new SpreadUnary($this->parseExpression(0, $allowArrow), $stream->getCurrent()->getLine());
$value = new SpreadUnary($this->parseExpression(), $stream->getCurrent()->getLine());
} elseif ($hasSpread) {
throw new SyntaxError('Normal arguments must be placed before argument unpacking.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
} else {
$value = $this->parseExpression(0, $allowArrow);
$value = $this->parseExpression();
}
}

Expand All @@ -753,7 +761,7 @@ public function parseArguments($namedArguments = false, $definition = false, $al
throw new SyntaxError('A default value for an argument must be a constant (a boolean, a string, a number, a sequence, or a mapping).', $token->getLine(), $stream->getSourceContext());
}
} else {
$value = $this->parseExpression(0, $allowArrow);
$value = $this->parseExpression();
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Fixtures/macros/arrow_as_arg.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
macro
--TEMPLATE--
{% set people = [
{first: "Bob", last: "Smith"},
{first: "Alice", last: "Dupond"},
] %}

{% set first_name_fn = (p) => p.first %}

{{ _self.display_people(people, first_name_fn) }}

{% macro display_people(people, fn) %}
{{ people|map(fn)|join(', ') }}
{% endmacro %}
--DATA--
return []
--EXPECT--
Bob, Alice