-
Notifications
You must be signed in to change notification settings - Fork 90
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
Add support for custom visitor keys. #112
Conversation
This strikes me as a pretty useful issue to have resolved, so one is not locked in. Could we get a review perhaps, @michaelficarra ? This is apparently currently blocking eslint/eslint#13639 as per eslint/eslint#13639 (comment) . And as such, this appears to be coming up for us in |
esquery.js
Outdated
candidates = options.fallback(node); | ||
} else { | ||
// 'iteration' fallback | ||
candidates = Object.keys(node); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we omit type
from here?
esquery.js
Outdated
@@ -224,19 +237,55 @@ function matches(node, selector, ancestry) { | |||
throw new Error(`Unknown selector type: ${selector.type}`); | |||
} | |||
|
|||
/** | |||
* Get visitor keys of a given node. | |||
* @param {external:AST} node node The AST node to get keys. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @param {external:AST} node node The AST node to get keys. | |
* @param {external:AST} node The AST node to get keys. |
esquery.js
Outdated
let candidates; | ||
if (options && options.visitorKeys && options.visitorKeys[nodeType]) { | ||
candidates = options.visitorKeys[nodeType]; | ||
} else { | ||
candidates = estraverse.VisitorKeys[nodeType]; | ||
} | ||
if(!candidates) { | ||
if (options && typeof options.fallback === 'function') { | ||
candidates = options.fallback(node); | ||
} else { | ||
// 'iteration' fallback | ||
candidates = Object.keys(node); | ||
} | ||
} | ||
return candidates; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would structure this function as early returns instead of a bunch of assignments.
let candidates; | |
if (options && options.visitorKeys && options.visitorKeys[nodeType]) { | |
candidates = options.visitorKeys[nodeType]; | |
} else { | |
candidates = estraverse.VisitorKeys[nodeType]; | |
} | |
if(!candidates) { | |
if (options && typeof options.fallback === 'function') { | |
candidates = options.fallback(node); | |
} else { | |
// 'iteration' fallback | |
candidates = Object.keys(node); | |
} | |
} | |
return candidates; | |
if (options && options.visitorKeys && options.visitorKeys[nodeType]) { | |
return options.visitorKeys[nodeType]; | |
} | |
if (estraverse.VisitorKeys[nodeType]) { | |
return estraverse.VisitorKeys[nodeType]; | |
} | |
if (options && typeof options.fallback === 'function') { | |
return options.fallback(node); | |
} | |
// 'iteration' fallback | |
return Object.keys(node); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise LGTM.
@michaelficarra Thank you for reviewing! But travis-ci doesn't seem to work. We may need to migrate to travis-ci.com or GitHub Actions. |
I opened a PR that uses GitHub Actions #117. |
Great to have this merged. Could we get an npm release too for it? |
@brettz9 Published |
This PR supports custom visitor keys by adding options to
query
,traverse
,matches
andmatch
.Also, I changed to traverse using
Object.keys
when no option is specified. (Same as when using'iteration'
forestraverse.Visitor.fallback
.)closes #111
You can set the following keys for the options:
visitorKeys
... if specified, extend the properties of the nodes that traverse the node.fallback
... if specified, control the properties of traversing nodes when encountering unknown nodes. If not specified, useObject.keys
.This option allows you to use queries such as
':nth-child'
,':first-child'
,' ~ '
and' + '
in JSX and TypeScript ASTs.