Skip to content

Commit 95b662d

Browse files
committed
Fix flatten's "not" op handling
Before, `flatten(parse('not a pr'))` would recurse infinitely. Now it returns the correct result.
1 parent 28a8089 commit 95b662d

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

src/flatten.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const valfilter = (f: Filter, path?: string): Filter => {
99
case "or":
1010
return { ...f, filters: f.filters.map(c => valfilter(c, path)) };
1111
case "not":
12-
return { ...f, filter: valfilter(f, path) };
12+
return { ...f, filter: valfilter(f.filter, path) };
1313
case "[]":
1414
return valfilter(f.valFilter, f.attrPath);
1515
}

test/flatten.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { eq, and, or } from "./test_util";
1+
import { eq, and, or, not, pr } from "./test_util";
22
import { Filter, parse, flatten } from "../src";
33
import { assert } from "chai";
44

@@ -35,6 +35,9 @@ describe("flatten", () => {
3535
`xx[${e1} and ((${e2} and ${e4}))]`,
3636
and(eq("xx.n1", 1), eq("xx.n2", 2), eq("xx.n4", 4))
3737
);
38+
test('not a pr', not(pr('a')));
39+
test('not a[b pr]', not(pr('a.b')));
40+
test('a[not b pr]', not(pr('a.b')));
3841
});
3942
describe("andor", () => {
4043
test(`(${e1} or ${e2}) and ${e3}`, and(or(a1, a2), a3));

test/test_util.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Compare, Filter, ValuePath, Suffix } from "../src";
1+
import { Compare, Filter, ValuePath, NotFilter, Suffix } from "../src";
22

33
export const EOT = { literal: "", type: "EOT" };
44
export function eq(attrPath: string, compValue: any): Compare {
@@ -19,3 +19,6 @@ export function v(attrPath: string, valFilter: Filter): ValuePath {
1919
export function pr(attrPath: string): Suffix {
2020
return { op: "pr", attrPath };
2121
}
22+
export function not(filter: Filter): NotFilter {
23+
return { op: "not", filter };
24+
}

0 commit comments

Comments
 (0)