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

:has(selector) matches nodes matched by selector #122

Open
mdjermanovic opened this issue Jun 25, 2021 · 1 comment
Open

:has(selector) matches nodes matched by selector #122

mdjermanovic opened this issue Jun 25, 2021 · 1 comment

Comments

@mdjermanovic
Copy link

esquery v1.4.0

const literal = espree.parse("1").body[0].expression;
const selector = esquery.parse(":has(Literal)");

esquery.matches(literal, selector); // true

Or. in the esquery demo, enter 1 as code, :has(Literal) as selector. It finds "Program", "ExpressionStatement", and "Literal".

I was expecting that selectors in :has() can match only descendants of the node, but in the above examples :has(Literal) matches a Literal node. Is this the intended behavior?

@lukaw3d
Copy link

lukaw3d commented Aug 6, 2024

Can still reproduce this, e.g. if (y) { y += 'z'; } matches BlockStatement:has(BlockStatement:has(BlockStatement))

The cause is

esquery/esquery.js

Lines 179 to 191 in 07ee329

estraverse.traverse(node, {
enter (node, parent) {
if (parent != null) { a.unshift(parent); }
for (let i = 0; i < matchers.length; ++i) {
if (matchers[i](node, a, options)) {
result = true;
this.break();
return;
}
}
},
leave () { a.shift(); },

enter and leave visit node too. A simple fix is master...lukaw3d:esquery:remove-parent-from-has

                estraverse.traverse(node, {
                    enter (descendant, parent) {
                        if (node === descendant) return;
                        ...

                    leave (descendant) {
                        if (node === descendant) return;
                        ...

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

No branches or pull requests

2 participants