feat(web): add missing API functions

Co-authored-by: Will Lillis <will.lillis24@gmail.com>
This commit is contained in:
Amaan Qureshi 2025-01-05 22:06:33 -05:00
parent dcdd6ce2d2
commit 45fa028201
11 changed files with 436 additions and 35 deletions

View file

@ -455,13 +455,112 @@ describe('Query', () => {
describe('Set a timeout', () =>
it('returns less than the expected matches', () => {
tree = parser.parse('function foo() while (true) { } }\n'.repeat(1000));
query = JavaScript.query('(function_declaration name: (identifier) @function)');
const matches = query.matches(tree.rootNode, { timeoutMicros: 1000 });
query = JavaScript.query(
'(function_declaration name: (identifier) @function)',
);
const matches = query.matches(tree.rootNode, {timeoutMicros: 1000});
assert.isBelow(matches.length, 1000);
const matches2 = query.matches(tree.rootNode, { timeoutMicros: 0 });
const matches2 = query.matches(tree.rootNode, {timeoutMicros: 0});
assert.equal(matches2.length, 1000);
})
);
}));
describe('Start and end indices for patterns', () => {
it('Returns the start and end indices for a pattern', () => {
const patterns1 = `
"+" @operator
"-" @operator
"*" @operator
"=" @operator
"=>" @operator
`.trim();
const patterns2 = `
(identifier) @a
(string) @b
`.trim();
const patterns3 = `
((identifier) @b (#match? @b i))
(function_declaration name: (identifier) @c)
(method_definition name: (property_identifier) @d)
`.trim();
const source = patterns1 + patterns2 + patterns3;
const query = JavaScript.query(source);
assert.equal(query.startIndexForPattern(0), 0);
assert.equal(query.endIndexForPattern(0), '"+" @operator\n'.length);
assert.equal(query.startIndexForPattern(5), patterns1.length);
assert.equal(
query.endIndexForPattern(5),
patterns1.length + '(identifier) @a\n'.length,
);
assert.equal(
query.startIndexForPattern(7),
patterns1.length + patterns2.length,
);
assert.equal(
query.endIndexForPattern(7),
patterns1.length +
patterns2.length +
'((identifier) @b (#match? @b i))\n'.length,
);
});
});
describe('Disable pattern', () => {
it('Disables patterns in the query', () => {
const query = JavaScript.query(`
(function_declaration name: (identifier) @name)
(function_declaration body: (statement_block) @body)
(class_declaration name: (identifier) @name)
(class_declaration body: (class_body) @body)
`);
// disable the patterns that match names
query.disablePattern(0);
query.disablePattern(2);
const source = 'class A { constructor() {} } function b() { return 1; }';
tree = parser.parse(source);
const matches = query.matches(tree.rootNode);
assert.deepEqual(formatMatches(matches), [
{
pattern: 3,
captures: [{name: 'body', text: '{ constructor() {} }'}],
},
{pattern: 1, captures: [{name: 'body', text: '{ return 1; }'}]},
]);
});
});
describe('Executes with a timeout', () => {
it('Returns less than the expected matches', () => {
tree = parser.parse('function foo() while (true) { } }\n'.repeat(1000));
query = JavaScript.query(
'(function_declaration) @function',
);
const startTime = performance.now();
const matches = query.matches(
tree.rootNode,
{
progressCallback: (_) => {
if (performance.now() - startTime > 1) {
return true;
}
return false;
},
},
);
assert.isBelow(matches.length, 1000);
const matches2 = query.matches(tree.rootNode);
assert.equal(matches2.length, 1000);
});
});
});
function formatMatches(matches) {