Give onlyIf(Not)Type ability…

…to accept multiple node types.
This commit is contained in:
Andrew Dupont 2023-05-04 21:28:39 -07:00
parent ef90e16bda
commit 31f67b9fd6
2 changed files with 26 additions and 4 deletions

View File

@ -659,6 +659,24 @@ describe('ScopeResolver', () => {
})).toBe(true);
});
it('supports onlyIfType with multiple types', async () => {
await grammar.setQueryForTest('highlightsQuery', `
(formal_parameters _ @thing
(#set! test.onlyIfType ", identifier"))
`);
const languageMode = new WASMTreeSitterLanguageMode({ grammar, buffer });
buffer.setLanguageMode(languageMode);
buffer.setText(dedent`
function foo (bar, baz, thud) {}
`);
await languageMode.ready;
let matched = await getAllMatches(grammar, languageMode);
expect(matched.length).toBe(5);
});
it('supports onlyIfHasError', async () => {
await grammar.setQueryForTest('highlightsQuery', `
((statement_block) @messed-up-statement-block

View File

@ -436,14 +436,18 @@ ScopeResolver.TESTS = {
return existingData === undefined;
},
// Passes only if the node is of the given type.
// Passes only if the node is of the given type. Can accept multiple
// space-separated types.
onlyIfType(node, nodeType) {
return node.type === nodeType;
if (!nodeType.includes(' ')) { return node.type === nodeType }
let nodeTypes = nodeType.split(/\s+/);
return nodeTypes.some(t => t === node.type);
},
// Negates `onlyIfType`.
onlyIfNotType(node, nodeType) {
return node.type !== nodeType;
onlyIfNotType(...args) {
let isType = ScopeResolver.TESTS.onlyIfType(...args);
return !isType;
},
// Passes only if the node contains any descendant ERROR nodes.