Merge branch '3.x' into 4.x

* 3.x:
  Add support for named arguments on macro calls and dot operator arguments
  Improve deprecation messages
  Simplify expression parser code
  Refactor expression parser
  chore: fix version added
  Cleanup code
This commit is contained in:
Fabien Potencier
2024-10-11 11:46:56 +02:00
13 changed files with 139 additions and 93 deletions
+10 -5
View File
@@ -14,6 +14,7 @@ namespace Twig\Node;
use Twig\Attribute\YieldReady;
use Twig\Compiler;
use Twig\Error\SyntaxError;
use Twig\Node\Expression\TempNameExpression;
/**
* Represents a macro node.
@@ -31,6 +32,10 @@ class MacroNode extends Node
if (self::VARARGS_NAME === $argumentName) {
throw new SyntaxError(\sprintf('The argument "%s" in macro "%s" cannot be defined because the variable "%s" is reserved for arbitrary arguments.', self::VARARGS_NAME, $name, self::VARARGS_NAME), $argument->getTemplateLine(), $argument->getSourceContext());
}
if (in_array($argumentName, TempNameExpression::RESERVED_NAMES)) {
$arguments->setNode('_'.$argumentName.'_', $argument);
$arguments->removeNode($argumentName);
}
}
parent::__construct(['body' => $body, 'arguments' => $arguments], ['name' => $name], $lineno);
@@ -47,7 +52,7 @@ class MacroNode extends Node
$pos = 0;
foreach ($this->getNode('arguments') as $name => $default) {
$compiler
->raw('$__'.$name.'__ = ')
->raw('$'.$name.' = ')
->subcompile($default)
;
@@ -61,7 +66,7 @@ class MacroNode extends Node
}
$compiler
->raw('...$__varargs__')
->raw('...$varargs')
->raw(")\n")
->write("{\n")
->indent()
@@ -73,8 +78,8 @@ class MacroNode extends Node
foreach ($this->getNode('arguments') as $name => $default) {
$compiler
->write('')
->string($name)
->raw(' => $__'.$name.'__')
->string(trim($name, '_'))
->raw(' => $'.$name)
->raw(",\n")
;
}
@@ -85,7 +90,7 @@ class MacroNode extends Node
->write('')
->string(self::VARARGS_NAME)
->raw(' => ')
->raw("\$__varargs__,\n")
->raw("\$varargs,\n")
->outdent()
->write("] + \$this->env->getGlobals();\n\n")
->write("\$blocks = [];\n\n")