Skip to content

Commit

Permalink
bug #4475 Fix having macro variables starting with an underscore (fab…
Browse files Browse the repository at this point in the history
…pot)

This PR was merged into the 3.x branch.

Discussion
----------

Fix having macro variables starting with an underscore

Closes #4452

Commits
-------

bdb0f3c Fix having macro variables starting with an underscore
  • Loading branch information
fabpot committed Nov 26, 2024
2 parents 0176de0 + bdb0f3c commit c384fb4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 3.16.0 (2024-XX-XX)

* Fix having macro variables starting with an underscore
* Deprecate not passing a `Source` instance to `TokenStream`
* Deprecate returning `null` from `TwigFilter::getSafe()` and `TwigFunction::getSafe()`, return `[]` instead

Expand Down
2 changes: 1 addition & 1 deletion src/Node/Expression/TempNameExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(string|int|null $name, int $lineno)
if (null !== $name && (is_int($name) || ctype_digit($name))) {
$name = (int) $name;
} elseif (in_array($name, self::RESERVED_NAMES)) {
$name = '_'.$name.'_';
$name = "\u{035C}".$name;
}

parent::__construct([], ['name' => $name], $lineno);
Expand Down
8 changes: 6 additions & 2 deletions src/Node/MacroNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(string $name, Node $body, Node $arguments, int $line
}

foreach ($arguments->getKeyValuePairs() as $pair) {
if ('_'.self::VARARGS_NAME.'_' === $pair['key']->getAttribute('name')) {
if ("\u{035C}".self::VARARGS_NAME === $pair['key']->getAttribute('name')) {
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), $pair['value']->getTemplateLine(), $pair['value']->getSourceContext());
}
}
Expand Down Expand Up @@ -88,9 +88,13 @@ public function compile(Compiler $compiler): void

foreach ($arguments->getKeyValuePairs() as $pair) {
$name = $pair['key'];
$var = $name->getAttribute('name');
if (str_starts_with($var, "\u{035C}")) {
$var = substr($var, \strlen("\u{035C}"));
}
$compiler
->write('')
->string(trim($name->getAttribute('name'), '_'))
->string($var)
->raw(' => ')
->subcompile($name)
->raw(",\n")
Expand Down
8 changes: 6 additions & 2 deletions tests/Node/MacroTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,22 @@ public static function provideTests(): iterable
new ConstantExpression(null, 1),
new LocalVariable('bar', 1),
new ConstantExpression('Foo', 1),
new LocalVariable('_underscore', 1),
new ConstantExpression(null, 1),
], 1);

$body = new BodyNode([new TextNode('foo', 1)]);
$node = new MacroNode('foo', $body, $arguments, 1);

yield 'with use_yield = true' => [$node, <<<EOF
// line 1
public function macro_foo(\$foo = null, \$bar = "Foo", ...\$varargs): string|Markup
public function macro_foo(\$foo = null, \$bar = "Foo", \$_underscore = null, ...\$varargs): string|Markup
{
\$macros = \$this->macros;
\$context = [
"foo" => \$foo,
"bar" => \$bar,
"_underscore" => \$_underscore,
"varargs" => \$varargs,
] + \$this->env->getGlobals();
Expand All @@ -71,12 +74,13 @@ public function macro_foo(\$foo = null, \$bar = "Foo", ...\$varargs): string|Mar

yield 'with use_yield = false' => [$node, <<<EOF
// line 1
public function macro_foo(\$foo = null, \$bar = "Foo", ...\$varargs): string|Markup
public function macro_foo(\$foo = null, \$bar = "Foo", \$_underscore = null, ...\$varargs): string|Markup
{
\$macros = \$this->macros;
\$context = [
"foo" => \$foo,
"bar" => \$bar,
"_underscore" => \$_underscore,
"varargs" => \$varargs,
] + \$this->env->getGlobals();
Expand Down

0 comments on commit c384fb4

Please sign in to comment.