mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-30 12:06:56 +00:00
Mark implicit macro argument default values as such with an attribute in AST
This commit is contained in:
committed by
Fabien Potencier
parent
b46e93c725
commit
e83a8028f0
@@ -591,7 +591,7 @@ class ExpressionParser
|
||||
* Parses arguments.
|
||||
*
|
||||
* @param bool $namedArguments Whether to allow named arguments or not
|
||||
* @param bool $definition Whether we are parsing arguments for a function definition
|
||||
* @param bool $definition Whether we are parsing arguments for a function (or macro) definition
|
||||
*
|
||||
* @return Node
|
||||
*
|
||||
@@ -642,6 +642,7 @@ class ExpressionParser
|
||||
if (null === $name) {
|
||||
$name = $value->getAttribute('name');
|
||||
$value = new ConstantExpression(null, $this->parser->getCurrentToken()->getLine());
|
||||
$value->setAttribute('is_implicit', true);
|
||||
}
|
||||
$args[$name] = $value;
|
||||
} else {
|
||||
|
||||
@@ -14,6 +14,8 @@ namespace Twig\Tests;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Twig\Environment;
|
||||
use Twig\Error\SyntaxError;
|
||||
use Twig\Lexer;
|
||||
use Twig\Loader\ArrayLoader;
|
||||
use Twig\Loader\LoaderInterface;
|
||||
use Twig\Node\Node;
|
||||
use Twig\Node\SetNode;
|
||||
@@ -175,6 +177,27 @@ EOF
|
||||
$this->addToAssertionCount(1);
|
||||
}
|
||||
|
||||
public function testImplicitMacroArgumentDefaultValues()
|
||||
{
|
||||
$template = '{% macro marco (po, lo = true) %}{% endmacro %}';
|
||||
$lexer = new Lexer(new Environment(new ArrayLoader()));
|
||||
$stream = $lexer->tokenize(new Source($template, 'index'));
|
||||
|
||||
$argumentNodes = $this->getParser()
|
||||
->parse($stream)
|
||||
->getNode('macros')
|
||||
->getNode('marco')
|
||||
->getNode('arguments')
|
||||
;
|
||||
|
||||
$this->assertTrue($argumentNodes->getNode('po')->hasAttribute('is_implicit'));
|
||||
$this->assertTrue($argumentNodes->getNode('po')->getAttribute('is_implicit'));
|
||||
$this->assertNull($argumentNodes->getNode('po')->getAttribute('value'));
|
||||
|
||||
$this->assertFalse($argumentNodes->getNode('lo')->hasAttribute('is_implicit'));
|
||||
$this->assertSame(true, $argumentNodes->getNode('lo')->getAttribute('value'));
|
||||
}
|
||||
|
||||
protected function getParser()
|
||||
{
|
||||
$parser = new Parser(new Environment($this->createMock(LoaderInterface::class)));
|
||||
|
||||
Reference in New Issue
Block a user