Skip to content

Commit

Permalink
Check that the node has a parent before matching a 'child' selector (#…
Browse files Browse the repository at this point in the history
…138)

Co-authored-by: Michael Ficarra <github@michael.ficarra.me>
  • Loading branch information
jviide and michaelficarra committed Feb 16, 2023
1 parent 64c05ae commit 66df002
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion esquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function generateMatcher(selector) {
const left = getMatcher(selector.left);
const right = getMatcher(selector.right);
return (node, ancestry, options) => {
if (right(node, ancestry, options)) {
if (ancestry.length > 0 && right(node, ancestry, options)) {
return left(ancestry[0], ancestry.slice(1), options);
}
return false;
Expand Down
6 changes: 6 additions & 0 deletions tests/queryComplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ describe('Complex selector query', function () {
simpleProgram.body[2]
]);
});

it('can not match a top level node', function () {
// Test fix for issue #135: half of a child selector matches a top-level node.
const matches = esquery(simpleProgram, 'NonExistingNodeType > *');
assert.isEmpty(matches);
});
});

0 comments on commit 66df002

Please sign in to comment.