Fix having macro variables starting with an underscore

This commit is contained in:
Fabien Potencier
2024-11-25 18:27:42 +01:00
parent b08968fecf
commit bdb0f3c042
4 changed files with 14 additions and 5 deletions
+1
View File
@@ -1,5 +1,6 @@
# 3.16.0 (2024-XX-XX) # 3.16.0 (2024-XX-XX)
* Fix having macro variables starting with an underscore
* Deprecate not passing a `Source` instance to `TokenStream` * Deprecate not passing a `Source` instance to `TokenStream`
* Deprecate returning `null` from `TwigFilter::getSafe()` and `TwigFunction::getSafe()`, return `[]` instead * Deprecate returning `null` from `TwigFilter::getSafe()` and `TwigFunction::getSafe()`, return `[]` instead
+1 -1
View File
@@ -32,7 +32,7 @@ class TempNameExpression extends AbstractExpression
if (null !== $name && (is_int($name) || ctype_digit($name))) { if (null !== $name && (is_int($name) || ctype_digit($name))) {
$name = (int) $name; $name = (int) $name;
} elseif (in_array($name, self::RESERVED_NAMES)) { } elseif (in_array($name, self::RESERVED_NAMES)) {
$name = '_'.$name.'_'; $name = "\u{035C}".$name;
} }
parent::__construct([], ['name' => $name], $lineno); parent::__construct([], ['name' => $name], $lineno);
+6 -2
View File
@@ -49,7 +49,7 @@ class MacroNode extends Node
} }
foreach ($arguments->getKeyValuePairs() as $pair) { 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()); 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());
} }
} }
@@ -89,9 +89,13 @@ class MacroNode extends Node
foreach ($arguments->getKeyValuePairs() as $pair) { foreach ($arguments->getKeyValuePairs() as $pair) {
$name = $pair['key']; $name = $pair['key'];
$var = $name->getAttribute('name');
if (str_starts_with($var, "\u{035C}")) {
$var = substr($var, \strlen("\u{035C}"));
}
$compiler $compiler
->write('') ->write('')
->string(trim($name->getAttribute('name'), '_')) ->string($var)
->raw(' => ') ->raw(' => ')
->subcompile($name) ->subcompile($name)
->raw(",\n") ->raw(",\n")
+6 -2
View File
@@ -42,6 +42,8 @@ class MacroTest extends NodeTestCase
new ConstantExpression(null, 1), new ConstantExpression(null, 1),
new LocalVariable('bar', 1), new LocalVariable('bar', 1),
new ConstantExpression('Foo', 1), new ConstantExpression('Foo', 1),
new LocalVariable('_underscore', 1),
new ConstantExpression(null, 1),
], 1); ], 1);
$body = new BodyNode([new TextNode('foo', 1)]); $body = new BodyNode([new TextNode('foo', 1)]);
@@ -49,12 +51,13 @@ class MacroTest extends NodeTestCase
yield 'with use_yield = true' => [$node, <<<EOF yield 'with use_yield = true' => [$node, <<<EOF
// line 1 // 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; \$macros = \$this->macros;
\$context = [ \$context = [
"foo" => \$foo, "foo" => \$foo,
"bar" => \$bar, "bar" => \$bar,
"_underscore" => \$_underscore,
"varargs" => \$varargs, "varargs" => \$varargs,
] + \$this->env->getGlobals(); ] + \$this->env->getGlobals();
@@ -71,12 +74,13 @@ EOF
yield 'with use_yield = false' => [$node, <<<EOF yield 'with use_yield = false' => [$node, <<<EOF
// line 1 // 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; \$macros = \$this->macros;
\$context = [ \$context = [
"foo" => \$foo, "foo" => \$foo,
"bar" => \$bar, "bar" => \$bar,
"_underscore" => \$_underscore,
"varargs" => \$varargs, "varargs" => \$varargs,
] + \$this->env->getGlobals(); ] + \$this->env->getGlobals();