Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken selectors after exactNode #146

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions tests/queryHas.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,29 @@ describe('Parent selector query', function () {
const shallowChildMatches = esquery(conditional, 'IfStatement:has(> LogicalExpression.test, > Identifier[name="x"])');
assert.equal(1, shallowChildMatches.length);
});

it('has(>BlockStatement)', function () {
const matches = esquery(conditional, 'IfStatement:has(>BlockStatement)');
assert.equal(3, matches.length);
});
it('has(BlockStatement>ExpressionStatement)', function () {
const matches = esquery(conditional, 'IfStatement:has(BlockStatement>ExpressionStatement)');
assert.equal(3, matches.length);
});
it('fails has(>BlockStatement>ExpressionStatement)', function () {
const matches = esquery(conditional, 'IfStatement:has(>BlockStatement>ExpressionStatement)');
assert.equal(3, matches.length);
});
Comment on lines +47 to +50
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cause seems to be that child selector (BlockStatement>ExpressionStatement) matches on the nested node (selector.right ExpressionStatement, instead of selector.left BlockStatement). Then exactNode checks if parent node has any ancestors

return left(ancestry[0], ancestry.slice(1), options);

esquery/esquery.js

Lines 130 to 133 in 07ee329

case 'exactNode':
return (node, ancestry) => {
return ancestry.length === 0;
};

within IfStatement:has(>BlockStatement>ExpressionStatement)

selector {
  type: 'child',
  left: { type: 'exactNode' },
  right: {
    type: 'child',
    left: { type: 'identifier', value: 'BlockStatement' },
    right: { type: 'identifier', value: 'ExpressionStatement' }
  }
}

left matches when visiting BlockStatement - print parents and children:
IfStatement,[BlockStatement] ⚟ [ 'ExpressionStatement' ]
right matches when visiting ExpressionStatement - print parents and children:
IfStatement,BlockStatement,[ExpressionStatement] ⚟ [ 'CallExpression' ] 😱

right is false on BlockStatement. left is false on ExpressionStatement. no match 🔴

compare that to within IfStatement:has(>BlockStatement)

selector {
  type: 'child',
  left: { type: 'exactNode' },
  right: { type: 'identifier', value: 'BlockStatement' }
}

left matches when visiting BlockStatement - print parents and children:
IfStatement,[BlockStatement] ⚟ [ 'ExpressionStatement' ]
right matches when visiting BlockStatement - print parents and children:
IfStatement,[BlockStatement] ⚟ [ 'ExpressionStatement' ]

left && right match on the same node ✔️

But even this doesn't look correct (should be [BlockStatement] ⚟ [ 'ExpressionStatement' ]).
:has injects IfStatement as an ancestor ❓ when checking the insides of :has(>BlockStatement). Then child matches when visiting BlockStatement, and it checks left(ancestry[0], ancestry.slice(1), options):

  • ancestry = [IfStatement]
  • left = matcher for exactNode
  • it checks if IfStatement has no ancestors ❓ not BlockStatement

it('workaround has(>BlockStatement:has(>ExpressionStatement))', function () {
const matches = esquery(conditional, 'IfStatement:has(>BlockStatement:has(>ExpressionStatement))');
assert.equal(3, matches.length);
});
it('fails has(>BlockStatement ExpressionStatement)', function () {
const matches = esquery(conditional, 'IfStatement:has(>BlockStatement ExpressionStatement)');
assert.equal(3, matches.length);
});
it('workaround has(>BlockStatement:has(ExpressionStatement))', function () {
const matches = esquery(conditional, 'IfStatement:has(>BlockStatement:has(ExpressionStatement))');
assert.equal(3, matches.length);
});
});