mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-01 13:07:22 +00:00
bug #4475 Fix having macro variables starting with an underscore (fabpot)
This PR was merged into the 3.x branch.
Discussion
----------
Fix having macro variables starting with an underscore
Closes #4452
Commits
-------
bdb0f3c042 Fix having macro variables starting with an underscore
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ class TempNameExpression extends AbstractExpression
|
||||
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);
|
||||
|
||||
@@ -48,7 +48,7 @@ class MacroNode extends Node
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -88,9 +88,13 @@ class MacroNode extends Node
|
||||
|
||||
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")
|
||||
|
||||
@@ -42,6 +42,8 @@ class MacroTest extends NodeTestCase
|
||||
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)]);
|
||||
@@ -49,12 +51,13 @@ class MacroTest extends NodeTestCase
|
||||
|
||||
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();
|
||||
|
||||
@@ -71,12 +74,13 @@ EOF
|
||||
|
||||
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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user