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

"Child in specific slot" selector? #72

Closed
papandreou opened this issue Aug 14, 2019 · 1 comment
Closed

"Child in specific slot" selector? #72

papandreou opened this issue Aug 14, 2019 · 1 comment

Comments

@papandreou
Copy link
Contributor

Hi, I'm thinking about using esquery for matching some pretty deeply nested constructs. I need to differentiate between eg. the object and property of a MemberExpression, so I cannot use the child selector, as it will match a child in either position:

const q = esquery.parse('MemberExpression > Identifier[name=foo]');
const ast = require('acorn').parse("foo.foo()")
console.log(esquery.match(ast, q));
[ Node { type: 'Identifier', start: 0, end: 3, name: 'foo' },
  Node { type: 'Identifier', start: 4, end: 7, name: 'foo' } ]

I know that I drop into "attribute mode" and achieve what I need using a query like:

MemberExpression[property.name=foo]

... but since the asts I need to match are much deeper than that, it would be really awkward to have to spell out the rest of the subtree like that with lots of repeated [property.foo.bar=123][property.foo.quux=abc]. It would be much nicer if I could use something like the > selector to cover the nesting aspects.

Am I overlooking a nice way to achieve that?

@papandreou
Copy link
Contributor Author

Oh, turns out this is exactly what the "field" selector is supposed to do. I didn't understand that when I first tried because the subject indicator bug (#60) caused me to get the wrong nodes. I tried switching to jupenur's fork, and now everything seems to work as described.

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

1 participant