mirror of
https://github.com/twigphp/Twig.git
synced 2026-10-15 08:16:19 +00:00
Use SpreadUnary everywhere
This commit is contained in:
@@ -12,7 +12,6 @@
|
||||
|
||||
namespace Twig;
|
||||
|
||||
use Twig\Attribute\FirstClassTwigCallableReady;
|
||||
use Twig\Error\SyntaxError;
|
||||
use Twig\Node\Expression\AbstractExpression;
|
||||
use Twig\Node\Expression\ArrayExpression;
|
||||
@@ -333,9 +332,7 @@ class ExpressionParser
|
||||
$first = false;
|
||||
|
||||
if ($stream->nextIf(Token::SPREAD_TYPE)) {
|
||||
$expr = $this->parseExpression();
|
||||
$expr->setAttribute('spread', true);
|
||||
$node->addElement($expr);
|
||||
$node->addElement(new SpreadUnary($this->parseExpression(), $stream->getCurrent()->getLine()));
|
||||
} else {
|
||||
$node->addElement($this->parseExpression());
|
||||
}
|
||||
@@ -364,9 +361,7 @@ class ExpressionParser
|
||||
$first = false;
|
||||
|
||||
if ($stream->nextIf(Token::SPREAD_TYPE)) {
|
||||
$value = $this->parseExpression();
|
||||
$value->setAttribute('spread', true);
|
||||
$node->addElement($value);
|
||||
$node->addElement(new SpreadUnary($this->parseExpression(), $stream->getCurrent()->getLine()));
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -82,29 +82,24 @@ class ArrayExpression extends AbstractExpression
|
||||
}
|
||||
$first = false;
|
||||
|
||||
if ($pair['value']->hasAttribute('spread')) {
|
||||
$compiler->raw('...')->subcompile($pair['value']);
|
||||
++$nextIndex;
|
||||
} else {
|
||||
$key = $pair['key'] instanceof ConstantExpression ? $pair['key']->getAttribute('value') : null;
|
||||
if ($pair['key'] instanceof NameExpression) {
|
||||
$pair['key']->setAttribute('stringify', true);
|
||||
}
|
||||
|
||||
if ($nextIndex !== $key) {
|
||||
if (\is_int($key)) {
|
||||
$nextIndex = $key + 1;
|
||||
}
|
||||
$compiler
|
||||
->subcompile($pair['key'])
|
||||
->raw(' => ')
|
||||
;
|
||||
} else {
|
||||
++$nextIndex;
|
||||
}
|
||||
|
||||
$compiler->subcompile($pair['value']);
|
||||
$key = $pair['key'] instanceof ConstantExpression ? $pair['key']->getAttribute('value') : null;
|
||||
if ($pair['key'] instanceof NameExpression) {
|
||||
$pair['key']->setAttribute('stringify', true);
|
||||
}
|
||||
|
||||
if ($nextIndex !== $key) {
|
||||
if (\is_int($key)) {
|
||||
$nextIndex = $key + 1;
|
||||
}
|
||||
$compiler
|
||||
->subcompile($pair['key'])
|
||||
->raw(' => ')
|
||||
;
|
||||
} else {
|
||||
++$nextIndex;
|
||||
}
|
||||
|
||||
$compiler->subcompile($pair['value']);
|
||||
}
|
||||
if (!$reopenAfterMergeSpread) {
|
||||
$compiler->raw(']');
|
||||
|
||||
@@ -21,6 +21,7 @@ use Twig\Node\Expression\ArrayExpression;
|
||||
use Twig\Node\Expression\Binary\ConcatBinary;
|
||||
use Twig\Node\Expression\ConstantExpression;
|
||||
use Twig\Node\Expression\NameExpression;
|
||||
use Twig\Node\Expression\Unary\SpreadUnary;
|
||||
use Twig\Parser;
|
||||
use Twig\Source;
|
||||
use Twig\TwigFilter;
|
||||
@@ -177,7 +178,7 @@ class ExpressionParserTest extends TestCase
|
||||
new ConstantExpression(2, 1),
|
||||
|
||||
new ConstantExpression(2, 1),
|
||||
self::createNameExpression('foo', ['spread' => true]),
|
||||
new SpreadUnary(new NameExpression('foo', 1), 1),
|
||||
], 1)],
|
||||
|
||||
// mapping with spread operator
|
||||
@@ -190,7 +191,7 @@ class ExpressionParserTest extends TestCase
|
||||
new ConstantExpression('c', 1),
|
||||
|
||||
new ConstantExpression(0, 1),
|
||||
self::createNameExpression('otherLetters', ['spread' => true]),
|
||||
new SpreadUnary(new NameExpression('otherLetters', 1), 1),
|
||||
], 1)],
|
||||
];
|
||||
}
|
||||
@@ -457,14 +458,4 @@ class ExpressionParserTest extends TestCase
|
||||
$parser->parse($env->tokenize(new Source('{{ 1 is empty element }}', 'index')));
|
||||
$this->expectNotToPerformAssertions();
|
||||
}
|
||||
|
||||
private static function createNameExpression(string $name, array $attributes): NameExpression
|
||||
{
|
||||
$expression = new NameExpression($name, 1);
|
||||
foreach ($attributes as $key => $value) {
|
||||
$expression->setAttribute($key, $value);
|
||||
}
|
||||
|
||||
return $expression;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user