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

Conversation

lukaw3d
Copy link

@lukaw3d lukaw3d commented Aug 3, 2024

Add more tests for #145 that show these selectors do not work correctly

  • :has(>BlockStatement>ExpressionStatement)
  • :has(>BlockStatement ExpressionStatement)

but there are workarounds:

  • :has(>BlockStatement:has(>ExpressionStatement))
  • :has(>BlockStatement:has(ExpressionStatement))

Comment on lines +47 to +50
it('fails has(>BlockStatement>ExpressionStatement)', function () {
const matches = esquery(conditional, 'IfStatement:has(>BlockStatement>ExpressionStatement)');
assert.equal(3, matches.length);
});
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

@lukaw3d
Copy link
Author

lukaw3d commented Aug 6, 2024

An attempt to fix it: #148

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant