Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark implicit macro argument default values as such with an attribute in AST #4010

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ public function parseFilterExpressionRaw($node, $tag = null)
* 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
*
Expand Down Expand Up @@ -642,6 +642,7 @@ public function parseArguments($namedArguments = false, $definition = false, $al
if (null === $name) {
$name = $value->getAttribute('name');
$value = new ConstantExpression(null, $this->parser->getCurrentToken()->getLine());
$value->setAttribute('isImplicit', true);
drjayvee marked this conversation as resolved.
Show resolved Hide resolved
}
$args[$name] = $value;
} else {
Expand Down
32 changes: 32 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Error\SyntaxError;
use Twig\Lexer;
use Twig\Loader\LoaderInterface;
use Twig\Node\Node;
use Twig\Node\SetNode;
Expand Down Expand Up @@ -175,6 +176,37 @@ public function testGetVarName()
$this->addToAssertionCount(1);
}

public function testImplicitMacroArgumentDefaultValues()
{
$template = '{% macro marco (po, lo = null) %}{% endmacro %}';
drjayvee marked this conversation as resolved.
Show resolved Hide resolved
$lexer = new Lexer(new Environment($this->createMock(LoaderInterface::class)));
fabpot marked this conversation as resolved.
Show resolved Hide resolved
$stream = $lexer->tokenize(new Source($template, 'index'));

$argumentNodes = $this->getParser()
->parse($stream)
->getNode('macros')
->getNode('marco')
->getNode('arguments')
;

self::assertTrue(
$argumentNodes->getNode('po')->hasAttribute('isImplicit')
);
drjayvee marked this conversation as resolved.
Show resolved Hide resolved
self::assertTrue(
$argumentNodes->getNode('po')->getAttribute('isImplicit')
);
self::assertNull(
$argumentNodes->getNode('po')->getAttribute('value')
);

self::assertFalse(
$argumentNodes->getNode('lo')->hasAttribute('isImplicit')
);
self::assertNull(
$argumentNodes->getNode('lo')->getAttribute('value')
);
}

protected function getParser()
{
$parser = new Parser(new Environment($this->createMock(LoaderInterface::class)));
Expand Down
Loading