feat(wasm)!: keep API in-line with upstream and start aligning with node

This commit is contained in:
Amaan Qureshi 2024-03-09 01:28:35 -05:00
parent c070c92722
commit 728793a160
8 changed files with 743 additions and 107 deletions

View file

@ -71,8 +71,10 @@ describe('Query', () => {
query = JavaScript.query('(identifier) @element');
const matches = query.matches(
tree.rootNode,
{row: 1, column: 1},
{row: 3, column: 1},
{
startPosition: {row: 1, column: 1},
endPosition: {row: 3, column: 1},
},
);
assert.deepEqual(formatMatches(matches), [
{pattern: 0, captures: [{name: 'element', text: 'd'}]},
@ -273,7 +275,7 @@ describe('Query', () => {
(array (identifier) @pre (identifier) @post)
`);
query.captures(tree.rootNode, null, null, {matchLimit: 32});
query.captures(tree.rootNode, {matchLimit: 32});
assert.ok(query.didExceedMatchLimit());
});
@ -295,7 +297,7 @@ describe('Query', () => {
const expectCount = (tree, queryText, expectedCount) => {
query = JavaScript.query(queryText);
captures = query.captures(tree.rootNode, null, null);
captures = query.captures(tree.rootNode);
assert.equal(captures.length, expectedCount);
};
@ -406,6 +408,49 @@ describe('Query', () => {
assert.deepEqual(query.predicatesForPattern(2), []);
});
});
describe('.disableCapture', () => {
it('disables a capture', () => {
const query = JavaScript.query(`
(function_declaration
(identifier) @name1 @name2 @name3
(statement_block) @body1 @body2)
`);
const source = 'function foo() { return 1; }';
const tree = parser.parse(source);
let matches = query.matches(tree.rootNode);
assert.deepEqual(formatMatches(matches), [
{
pattern: 0,
captures: [
{name: 'name1', text: 'foo'},
{name: 'name2', text: 'foo'},
{name: 'name3', text: 'foo'},
{name: 'body1', text: '{ return 1; }'},
{name: 'body2', text: '{ return 1; }'},
],
},
]);
// disabling captures still works when there are multiple captures on a
// single node.
query.disableCapture('name2');
matches = query.matches(tree.rootNode);
assert.deepEqual(formatMatches(matches), [
{
pattern: 0,
captures: [
{name: 'name1', text: 'foo'},
{name: 'name3', text: 'foo'},
{name: 'body1', text: '{ return 1; }'},
{name: 'body2', text: '{ return 1; }'},
],
},
]);
});
});
});
function formatMatches(matches) {