query: Add not-eq? predicate in rust & wasm binding

This commit is contained in:
Max Brunsfeld 2020-02-19 21:33:58 -08:00
parent f67c0526fd
commit 33492ca9df
2 changed files with 21 additions and 10 deletions

View file

@ -742,7 +742,10 @@ class Language {
throw new Error('Predicates must begin with a literal value');
}
const operator = steps[0].value;
let isPositive = true;
switch (operator) {
case 'not-eq?':
isPositive = false;
case 'eq?':
if (steps.length !== 3) throw new Error(
`Wrong number of arguments to \`eq?\` predicate. Expected 2, got ${steps.length - 1}`
@ -759,14 +762,16 @@ class Language {
if (c.name === captureName1) node1 = c.node;
if (c.name === captureName2) node2 = c.node;
}
return node1.text === node2.text
return (node1.text === node2.text) === isPositive;
});
} else {
const captureName = steps[1].name;
const stringValue = steps[2].value;
predicates[i].push(function(captures) {
for (const c of captures) {
if (c.name === captureName) return c.node.text === stringValue;
if (c.name === captureName) {
return (c.node.text === stringValue) === isPositive;
};
}
return false;
});