fix: properly handle Query.matches when filtering out results
This commit is contained in:
parent
7e0dd7b9c1
commit
4303ab99c9
3 changed files with 42 additions and 4 deletions
|
|
@ -1182,13 +1182,14 @@ class Query {
|
|||
const captures = new Array(captureCount);
|
||||
address = unmarshalCaptures(this, node.tree, address, captures);
|
||||
if (this.textPredicates[pattern].every((p) => p(captures))) {
|
||||
result[filteredCount++] = {pattern, captures};
|
||||
result[filteredCount] = {pattern, captures};
|
||||
const setProperties = this.setProperties[pattern];
|
||||
if (setProperties) result[i].setProperties = setProperties;
|
||||
if (setProperties) result[filteredCount].setProperties = setProperties;
|
||||
const assertedProperties = this.assertedProperties[pattern];
|
||||
if (assertedProperties) result[i].assertedProperties = assertedProperties;
|
||||
if (assertedProperties) result[filteredCount].assertedProperties = assertedProperties;
|
||||
const refutedProperties = this.refutedProperties[pattern];
|
||||
if (refutedProperties) result[i].refutedProperties = refutedProperties;
|
||||
if (refutedProperties) result[filteredCount].refutedProperties = refutedProperties;
|
||||
filteredCount++;
|
||||
}
|
||||
}
|
||||
result.length = filteredCount;
|
||||
|
|
|
|||
|
|
@ -107,6 +107,23 @@ describe('Query', () => {
|
|||
{pattern: 0, captures: [{name: 'name', text: 'gross'}]},
|
||||
]);
|
||||
});
|
||||
|
||||
it('handles multiple matches where the first one is filtered', () => {
|
||||
tree = parser.parse(`
|
||||
const a = window.b;
|
||||
`);
|
||||
|
||||
query = JavaScript.query(`
|
||||
((identifier) @variable.builtin
|
||||
(#match? @variable.builtin "^(arguments|module|console|window|document)$")
|
||||
(#is-not? local))
|
||||
`);
|
||||
|
||||
const matches = query.matches(tree.rootNode);
|
||||
assert.deepEqual(formatMatches(matches), [
|
||||
{pattern: 0, captures: [{name: 'variable.builtin', text: 'window'}]},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.captures', () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue