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

Conversation

drjayvee
Copy link
Contributor

@drjayvee drjayvee commented Mar 16, 2024

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

Consider the following macro:

{% 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.

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

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 `isImplicit`, 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](twigphp#4003).

For example, a static analysis tool might consider arguments with no explicit default value as non-optional.
@drjayvee
Copy link
Contributor Author

Similar to #4009, this is a prerequisite for a desired static analysis inspection in TwigStan.

I can extend this feature by adding this attribute to every function/macro argument to make it more consistent.

@drjayvee drjayvee changed the title Mark implicit macro argument default values as such with an attribute Mark implicit macro argument default values as such with an attribute in AST Aug 16, 2024
src/ExpressionParser.php Outdated Show resolved Hide resolved
tests/ParserTest.php Outdated Show resolved Hide resolved
tests/ParserTest.php Outdated Show resolved Hide resolved
@drjayvee
Copy link
Contributor Author

@fabpot thanks for the review. The latest commit includes all your suggestions.

@drjayvee
Copy link
Contributor Author

@fabpot since you're working on Twig these days, could you please have another look at this PR? 😊

Copy link
Contributor

@fabpot fabpot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

tests/ParserTest.php Show resolved Hide resolved
@fabpot
Copy link
Contributor

fabpot commented Aug 27, 2024

Thank you @drjayvee.

fabpot added a commit that referenced this pull request Aug 27, 2024
…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
@fabpot
Copy link
Contributor

fabpot commented Aug 27, 2024

Merged.

@fabpot fabpot closed this Aug 27, 2024
@drjayvee drjayvee deleted the implicit-default-value branch August 27, 2024 11:37
drjayvee added a commit to alisqi/TwigQI that referenced this pull request Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants