Skip to content

Fix NPE when calling macros inside ternary with variable condition#1310

Merged
mnobile-hs merged 2 commits intomasterfrom
mnobile_hubspot/fix-macro-ternary-npe
Apr 10, 2026
Merged

Fix NPE when calling macros inside ternary with variable condition#1310
mnobile-hs merged 2 commits intomasterfrom
mnobile_hubspot/fix-macro-ternary-npe

Conversation

@mnobile-hs
Copy link
Copy Markdown
Contributor

Using a macro in a ternary would lead to the macro name failing to parse correctly and throw an NPE.

For ternary like:

{{ greet('world') if myVar else greet('nobody') }}

The else keyword in a ternary is registered with Symbol.COLON, causing nonliteral() to greedily interpret myVar else greet('nobody') as the namespace-prefixed function myVar:greet instead of greet.

The fix adds an image check (getImage().equals(":")) so the parser only enters namespace-prefix logic for a literal colon token, not else.

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>
@mnobile-hs mnobile-hs requested a review from jasmith-hs April 10, 2026 23:12
public void itCallsMacroInTernaryWithVariableCondition() {
String template =
"{% macro greet(name) %}Hello {{ name }}{% endmacro %}" +
"{{ greet('world') if myVar else greet('nobody') }}";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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>
@mnobile-hs mnobile-hs marked this pull request as ready for review April 10, 2026 23:29
@mnobile-hs mnobile-hs merged commit 6d5e180 into master Apr 10, 2026
7 checks passed
@mnobile-hs mnobile-hs deleted the mnobile_hubspot/fix-macro-ternary-npe branch April 10, 2026 23:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants