Skip to content

Fix ?? operator with array predicate on LHS#774

Merged
andrew-coleman merged 1 commit intojsonata-js:masterfrom
doolse:fix/coalescing-operator-predicate-duplication
Mar 18, 2026
Merged

Fix ?? operator with array predicate on LHS#774
andrew-coleman merged 1 commit intojsonata-js:masterfrom
doolse:fix/coalescing-operator-predicate-duplication

Conversation

@doolse
Copy link
Contributor

@doolse doolse commented Mar 17, 2026

Summary

  • Fix the ?? (nullish coalescing) operator returning incorrect results when the LHS
    contains an array predicate (e.g. array[1] ?? 3)
  • The parser's ?? handler reused the same left AST node reference for both the
    $exists() condition and the then branch, causing filter stages to be duplicated
    during AST post-processing
  • Deep-clone left for the condition argument so the two branches have independent
    AST nodes

Fixes #773

Test plan

  • Added test case for LHS predicate that exists (case013)
  • Added test case for LHS predicate that doesn't exist (case014)
  • All 1763 tests passing

Signed-off-by: Jolse Maginnis doolse@gmail.com

The ?? infix handler reused the same `left` AST node reference for both
the $exists() condition and the `then` branch. During AST post-processing,
filter stages were pushed onto the shared node twice, turning e.g.
`array[1]` into `array[1][1]`.

Deep-clone `left` for the condition argument so the two branches have
independent AST nodes.

Fixes jsonata-js#773
@andrew-coleman andrew-coleman merged commit fd47061 into jsonata-js:master Mar 18, 2026
8 checks passed
@andrew-coleman
Copy link
Member

Many thanks :)

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.

Nullish coalescing (??) operator returns wrong result when LHS has array predicate

2 participants