diff --git a/esquery.js b/esquery.js index 715919e..a7fe363 100644 --- a/esquery.js +++ b/esquery.js @@ -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; diff --git a/tests/queryComplex.js b/tests/queryComplex.js index d71aab1..c596f7f 100644 --- a/tests/queryComplex.js +++ b/tests/queryComplex.js @@ -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); + }); });