query: Handle #not-match? in rust, wasm bindings

This commit is contained in:
Max Brunsfeld 2020-07-24 10:49:20 -07:00
parent 32099050d6
commit 1ae5cbc851
4 changed files with 25 additions and 7 deletions

View file

@ -784,6 +784,8 @@ class Language {
}
break;
case 'not-match?':
isPositive = false;
case 'match?':
if (steps.length !== 3) throw new Error(
`Wrong number of arguments to \`#match?\` predicate. Expected 2, got ${steps.length - 1}.`
@ -798,7 +800,7 @@ class Language {
const regex = new RegExp(steps[2].value);
textPredicates[i].push(function(captures) {
for (const c of captures) {
if (c.name === captureName) return regex.test(c.node.text);
if (c.name === captureName) return regex.test(c.node.text) === isPositive;
}
return false;
});

View file

@ -126,12 +126,17 @@ describe("Query", () => {
it("handles conditions that compare the text of capture to literal strings", () => {
tree = parser.parse(`
lambda
panda
load
toad
const ab = require('./ab');
new Cd(EF);
`);
query = JavaScript.query(`
(identifier) @variable
((identifier) @variable
(#not-match? @variable "^(lambda|load)$"))
((identifier) @function.builtin
(#eq? @function.builtin "require"))
@ -145,6 +150,8 @@ describe("Query", () => {
const captures = query.captures(tree.rootNode);
assert.deepEqual(formatCaptures(captures), [
{ name: "variable", text: "panda" },
{ name: "variable", text: "toad" },
{ name: "variable", text: "ab" },
{ name: "variable", text: "require" },
{ name: "function.builtin", text: "require" },