From 66df00231a8fc5bd54888a24524f896048bd0137 Mon Sep 17 00:00:00 2001 From: jviide Date: Thu, 16 Feb 2023 15:43:28 +0200 Subject: [PATCH] Check that the node has a parent before matching a 'child' selector (#138) Co-authored-by: Michael Ficarra --- esquery.js | 2 +- tests/queryComplex.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) 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); + }); });