Skip to content

Commit

Permalink
Fix filter query existence tests in logical expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jg-rp committed Mar 11, 2024
1 parent 4d7160b commit 0e580b9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/path/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,25 @@ export class PrefixExpression extends FilterExpression {
}

export class InfixExpression extends FilterExpression {
readonly logical: boolean;

constructor(
readonly token: Token,
readonly left: FilterExpression,
readonly operator: string,
readonly right: FilterExpression,
) {
super(token);
this.logical = operator === "&&" || operator === "||";
}

public evaluate(context: FilterContext): boolean {
let left = this.left.evaluate(context);
if (left instanceof JSONPathNodeList && left.nodes.length === 1)
if (!this.logical && left instanceof JSONPathNodeList && left.nodes.length === 1)

Check failure on line 135 in src/path/expression.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `!this.logical·&&·left·instanceof·JSONPathNodeList·&&·left.nodes.length·===·1` with `⏎······!this.logical·&&⏎······left·instanceof·JSONPathNodeList·&&⏎······left.nodes.length·===·1⏎····`

Check failure on line 135 in src/path/expression.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `!this.logical·&&·left·instanceof·JSONPathNodeList·&&·left.nodes.length·===·1` with `⏎······!this.logical·&&⏎······left·instanceof·JSONPathNodeList·&&⏎······left.nodes.length·===·1⏎····`
left = left.nodes[0].value;

let right = this.right.evaluate(context);
if (right instanceof JSONPathNodeList && right.nodes.length === 1)
if (!this.logical && right instanceof JSONPathNodeList && right.nodes.length === 1)

Check failure on line 139 in src/path/expression.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `!this.logical·&&·right·instanceof·JSONPathNodeList·&&·right.nodes.length·===·1` with `⏎······!this.logical·&&⏎······right·instanceof·JSONPathNodeList·&&⏎······right.nodes.length·===·1⏎····`

Check failure on line 139 in src/path/expression.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `!this.logical·&&·right·instanceof·JSONPathNodeList·&&·right.nodes.length·===·1` with `⏎······!this.logical·&&⏎······right·instanceof·JSONPathNodeList·&&⏎······right.nodes.length·===·1⏎····`
right = right.nodes[0].value;

if (this.operator === "&&") {
Expand All @@ -148,7 +151,7 @@ export class InfixExpression extends FilterExpression {
}

public toString(): string {
if (this.operator === "&&" || this.operator === "||") {
if (this.logical) {
return `(${this.left.toString()} ${
this.operator
} ${this.right.toString()})`;
Expand Down
10 changes: 10 additions & 0 deletions tests/path/query.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { query } from "../../src/path";

describe("JSONPath query", () => {
test("logical expression, existence tests", () => {

Check failure on line 4 in tests/path/query.test.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`

Check failure on line 4 in tests/path/query.test.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
const path = "$[[email protected] && @.b]";

Check failure on line 5 in tests/path/query.test.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`

Check failure on line 5 in tests/path/query.test.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
const data = [{"a": false, "b": false}];

Check failure on line 6 in tests/path/query.test.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `··const·data·=··[{"a":·false,·"b":·false` with `const·data·=·[{·a:·false,·b:·false·`

Check failure on line 6 in tests/path/query.test.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `··const·data·=··[{"a":·false,·"b":·false` with `const·data·=·[{·a:·false,·b:·false·`
expect(query(path, data).values()).toStrictEqual([{"a": false, "b": false}]);

Check failure on line 7 in tests/path/query.test.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `······expect(query(path,·data).values()).toStrictEqual([{"a":·false,·"b":·false` with `····expect(query(path,·data).values()).toStrictEqual([{·a:·false,·b:·false·`

Check failure on line 7 in tests/path/query.test.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `······expect(query(path,·data).values()).toStrictEqual([{"a":·false,·"b":·false` with `····expect(query(path,·data).values()).toStrictEqual([{·a:·false,·b:·false·`
});

Check failure on line 8 in tests/path/query.test.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`

Check failure on line 8 in tests/path/query.test.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
}

Check failure on line 9 in tests/path/query.test.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `··}⏎);` with `});⏎`

Check failure on line 9 in tests/path/query.test.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `··}⏎);` with `});⏎`
);

0 comments on commit 0e580b9

Please sign in to comment.