Skip to content

Commit

Permalink
feature #4010 Mark implicit macro argument default values as such wit…
Browse files Browse the repository at this point in the history
…h an attribute in AST (drjayvee)

This PR was squashed before being merged into the 3.x branch.

Discussion
----------

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

This change causes no difference to compiled templates or to macro argument semantics.

Consider the following macro:
```twig
{% macro marco(po, lo = null) %}{% endmacro %}
```

With this change, the `ConstantExpression` for argument `po` will have an attribute `is_implicit`, whose value will be `true`. (Note that `lo` will not have that attribute.)

This allows node visitors to distinguish between arguments that do and those that do not have explicit default values even if the value is `null`.

This is useful for [static code analysis](#4003).

For example, a static analysis tool might consider arguments with no explicit default value as non-optional.

Commits
-------

e83a802 Mark implicit macro argument default values as such with an attribute in AST
  • Loading branch information
fabpot committed Aug 27, 2024
2 parents 41459e0 + e83a802 commit f9f7b79
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,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 @@ -657,6 +657,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('is_implicit', true);
}
$args[$name] = $value;
} else {
Expand Down
22 changes: 22 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\ArrayLoader;
use Twig\Node\Node;
use Twig\Node\SetNode;
Expand Down Expand Up @@ -177,6 +178,27 @@ public function testGetVarName()
$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(new ArrayLoader()));
Expand Down

0 comments on commit f9f7b79

Please sign in to comment.