Add wasm bindings for predicates
This commit is contained in:
parent
096126d039
commit
d4d554b2ae
6 changed files with 249 additions and 41 deletions
|
|
@ -19,7 +19,7 @@ describe("Query", () => {
|
|||
});
|
||||
|
||||
describe('construction', () => {
|
||||
it('throws an error on invalid syntax', () => {
|
||||
it('throws an error on invalid patterns', () => {
|
||||
assert.throws(() => {
|
||||
JavaScript.query("(function_declaration wat)")
|
||||
}, "Bad syntax at offset 22: \'wat)\'...");
|
||||
|
|
@ -33,6 +33,24 @@ describe("Query", () => {
|
|||
JavaScript.query("(function_declaration non_existent:(identifier))")
|
||||
}, "Bad field name 'non_existent'");
|
||||
});
|
||||
|
||||
it('throws an error on invalid predicates', () => {
|
||||
assert.throws(() => {
|
||||
JavaScript.query("((identifier) @abc (eq? @ab hi))")
|
||||
}, "Bad capture name @ab");
|
||||
assert.throws(() => {
|
||||
JavaScript.query("((identifier) @abc (eq? @ab hi))")
|
||||
}, "Bad capture name @ab");
|
||||
assert.throws(() => {
|
||||
JavaScript.query("((identifier) @abc (eq?))")
|
||||
}, "Wrong number of arguments to `eq?` predicate. Expected 2, got 0");
|
||||
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?`");
|
||||
});
|
||||
});
|
||||
|
||||
describe('.matches', () => {
|
||||
|
|
@ -119,6 +137,66 @@ describe("Query", () => {
|
|||
]
|
||||
);
|
||||
});
|
||||
|
||||
it('handles conditions that compare the text of capture to literal strings', () => {
|
||||
tree = parser.parse(`
|
||||
const ab = require('./ab');
|
||||
new Cd(EF);
|
||||
`);
|
||||
|
||||
query = JavaScript.query(`
|
||||
(identifier) @variable
|
||||
|
||||
((identifier) @function.builtin
|
||||
(eq? @function.builtin "require"))
|
||||
|
||||
((identifier) @constructor
|
||||
(match? @constructor "^[A-Z]"))
|
||||
|
||||
((identifier) @constant
|
||||
(match? @constant "^[A-Z]{2,}$"))
|
||||
`);
|
||||
|
||||
const captures = query.captures(tree.rootNode);
|
||||
assert.deepEqual(
|
||||
formatCaptures(captures),
|
||||
[
|
||||
{name: "variable", text: "ab"},
|
||||
{name: "variable", text: "require"},
|
||||
{name: "function.builtin", text: "require"},
|
||||
{name: "variable", text: "Cd"},
|
||||
{name: "constructor", text: "Cd"},
|
||||
{name: "variable", text: "EF"},
|
||||
{name: "constructor", text: "EF"},
|
||||
{name: "constant", text: "EF"},
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
it('handles conditions that compare the text of capture to each other', () => {
|
||||
tree = parser.parse(`
|
||||
const ab = abc + 1;
|
||||
const def = de + 1;
|
||||
const ghi = ghi + 1;
|
||||
`);
|
||||
|
||||
query = JavaScript.query(`
|
||||
((variable_declarator
|
||||
name: (identifier) @id1
|
||||
value: (binary_expression
|
||||
left: (identifier) @id2))
|
||||
(eq? @id1 @id2))
|
||||
`);
|
||||
|
||||
const captures = query.captures(tree.rootNode);
|
||||
assert.deepEqual(
|
||||
formatCaptures(captures),
|
||||
[
|
||||
{name: "id1", text: "ghi"},
|
||||
{name: "id2", text: "ghi"},
|
||||
]
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue