wasm: Allow arbitrary predicates in queries

This commit is contained in:
Max Brunsfeld 2020-06-01 13:28:52 -07:00
parent 47d607da8d
commit 28a779d6a0
2 changed files with 66 additions and 10 deletions

View file

@ -45,9 +45,6 @@ describe("Query", () => {
assert.throws(() => {
JavaScript.query("((identifier) @a (eq? @a @a @a))");
}, "Wrong number of arguments to `#eq?` predicate. Expected 2, got 3");
assert.throws(() => {
JavaScript.query("((identifier) @a (#something-else? @a))");
}, "Unknown query predicate `#something-else?`");
});
});
@ -207,6 +204,52 @@ describe("Query", () => {
]);
});
});
describe(".predicatesForPattern(index)", () => {
it("returns all of the predicates as objects", () => {
query = JavaScript.query(`
(
(binary_expression
left: (identifier) @a
right: (identifier) @b)
(#something? @a @b)
(#match? @a "c")
(#something-else? @a "A" @b "B")
)
((identifier) @c
(#hello! @c))
"if" @d
`);
assert.deepEqual(query.predicatesForPattern(0), [
{
operator: "something?",
operands: [
{ type: "capture", name: "a" },
{ type: "capture", name: "b" },
],
},
{
operator: "something-else?",
operands: [
{ type: "capture", name: "a" },
{ type: "string", value: "A" },
{ type: "capture", name: "b" },
{ type: "string", value: "B" },
],
},
]);
assert.deepEqual(query.predicatesForPattern(1), [
{
operator: "hello!",
operands: [{ type: "capture", name: "c" }],
},
]);
assert.deepEqual(query.predicatesForPattern(2), []);
});
});
});
function formatMatches(matches) {