Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,12 @@ const parser = (() => {
// elvis/default operator
infix("?:", operators['?:'], function (left) {
this.type = 'condition';
this.condition = left;
// Deep-clone `left` so the condition and then branches have
// independent AST nodes. Sharing the same reference causes
// post-parse processing (e.g. predicate stages, unary minus
// folding on number literals) to mutate the same node twice,
// producing wrong results (see #773 for the equivalent ?? fix).
this.condition = JSON.parse(JSON.stringify(left));
this.then = left;
this.else = expression(0);
return this;
Expand Down
7 changes: 7 additions & 0 deletions test/test-suite/groups/default-operator/case014.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"description": "array predicate on lhs exists, should return lhs value",
"expr": "foo.blah[0] ?: 42",
"dataset": "dataset0",
"bindings": {},
"result": {"baz": {"fud": "hello"}}
}
7 changes: 7 additions & 0 deletions test/test-suite/groups/default-operator/case015.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"description": "array predicate on lhs does not exist, should return rhs value",
"expr": "foo.blah[5] ?: 42",
"dataset": "dataset0",
"bindings": {},
"result": 42
}
7 changes: 7 additions & 0 deletions test/test-suite/groups/default-operator/case016.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"description": "negative array index on lhs returns last element",
"expr": "[1,2,3][-1] ?: 42",
"dataset": null,
"bindings": {},
"result": 3
}
7 changes: 7 additions & 0 deletions test/test-suite/groups/default-operator/case017.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"description": "negative array index on single-element array returns the element",
"expr": "[true][-1] ?: \"no\"",
"dataset": null,
"bindings": {},
"result": true
}
7 changes: 7 additions & 0 deletions test/test-suite/groups/default-operator/case018.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"description": "unary minus on number literal LHS is not double-negated",
"expr": "-5 ?: 99",
"dataset": null,
"bindings": {},
"result": -5
}
Loading