Fix NPE when calling macros inside ternary with variable condition#1310
Merged
mnobile-hs merged 2 commits intomasterfrom Apr 10, 2026
Merged
Fix NPE when calling macros inside ternary with variable condition#1310mnobile-hs merged 2 commits intomasterfrom
mnobile-hs merged 2 commits intomasterfrom
Conversation
The `else` keyword in Python-style ternaries (`x if cond else y`) is
registered with `Symbol.COLON`, causing `nonliteral()` to greedily
interpret `var else resolve_url(...)` as the namespace-prefixed function
`var:resolve_url`. This made `AstMacroFunction.eval()` fail to find the
macro by name, falling through to `super.eval()` with a null base → NPE.
The fix adds an image check (`getImage().equals(":")`) so the parser only
enters namespace-prefix logic for a literal colon token, not `else`.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
jasmith-hs
reviewed
Apr 10, 2026
| public void itCallsMacroInTernaryWithVariableCondition() { | ||
| String template = | ||
| "{% macro greet(name) %}Hello {{ name }}{% endmacro %}" + | ||
| "{{ greet('world') if myVar else greet('nobody') }}"; |
Contributor
There was a problem hiding this comment.
Could you add an adjacent test for a ternary using ? and : too to ensure that's also parsing properly
{{ myVar ? greet('world') : greet('nobody') }}
Addresses review feedback to ensure the `?`/`:` ternary also works correctly with macro calls and variable conditions. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
jasmith-hs
approved these changes
Apr 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Using a macro in a ternary would lead to the macro name failing to parse correctly and throw an NPE.
For ternary like:
The
elsekeyword in a ternary is registered withSymbol.COLON, causingnonliteral()to greedily interpretmyVar else greet('nobody')as the namespace-prefixed functionmyVar:greetinstead ofgreet.The fix adds an image check (
getImage().equals(":")) so the parser only enters namespace-prefix logic for a literal colon token, notelse.