Skip to content

Commit 3b67861

Browse files
authored
fix(wasm): restore passing in ERROR to descendantsOfType (tree-sitter#4226)
1 parent b26b7f8 commit 3b67861

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/binding_web/src/node.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,11 @@ export class Node {
416416
// Convert the type strings to numeric type symbols
417417
const symbols: number[] = [];
418418
const typesBySymbol = this.tree.language.types;
419+
for (const node_type of types) {
420+
if (node_type == "ERROR") {
421+
symbols.push(65535); // Internally, ts_builtin_sym_error is -1, which is UINT_16MAX
422+
}
423+
}
419424
for (let i = 0, n = typesBySymbol.length; i < n; i++) {
420425
if (types.includes(typesBySymbol[i])) {
421426
symbols.push(i);

lib/binding_web/test/node.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('Node', () => {
6363
tree = parser.parse('x10 + 1000')!;
6464
expect(tree.rootNode.children).toHaveLength(1);
6565
const sumNode = tree.rootNode.firstChild!.firstChild!;
66-
expect(sumNode.children.map(child => child!.type)).toEqual(['identifier', '+', 'number' ]);
66+
expect(sumNode.children.map(child => child!.type)).toEqual(['identifier', '+', 'number']);
6767
});
6868
});
6969

@@ -449,6 +449,24 @@ describe('Node', () => {
449449
});
450450
});
451451

452+
describe('.descendantsOfType("ERROR")', () => {
453+
it('finds all of the descendants of an ERROR node', () => {
454+
tree = parser.parse(
455+
`if ({a: 'b'} {c: 'd'}) {
456+
// ^ ERROR
457+
x = function(a) { b; } function(c) { d; }
458+
}`
459+
)!;
460+
const errorNode = tree.rootNode;
461+
const descendants = errorNode.descendantsOfType('ERROR');
462+
expect(
463+
descendants.map((node) => node!.startIndex)
464+
).toEqual(
465+
[4]
466+
);
467+
});
468+
});
469+
452470
describe('.descendantsOfType', () => {
453471
it('finds all descendants of a given type in the given range', () => {
454472
tree = parser.parse('a + 1 * b * 2 + c + 3')!;

0 commit comments

Comments
 (0)