estools / esquery

ECMAScript AST query library.
http://estools.github.io/esquery/
Other
816 stars 88 forks source link

Feature Request: Add support for custom visitor keys. #111

Closed ota-meshi closed 3 years ago

ota-meshi commented 4 years ago

I want esquery to add support for custom visitor keys.
Currently, some queries crash when using a node type unknown to estraverse. e.g. ":nth-child(1)" (See also to #110, #67)

I think that the node type decided by estree will be fixed by version upgrade, but using JSX and TypeScript nodes will continue to crash.

If esquery can support custom visitor keys, passing custom visitor keys for JSX and TypeScript allows people to use esquery in JSX and TypeScript nodes without crashing.

octogonz commented 3 years ago

Another option would be to simply fail to match unrecognized nodes.

Where esquery does this: https://github.com/estools/esquery/blob/e27e73d8cde63a2eb1aba3424510ce1b714d207e/esquery.js#L309-L312

...it could avoid crashing by simply doing this:

  const keys = estraverse.VisitorKeys[parent.type]; 

  if (keys === undefined) {
    return false;  // <--------------------
  }

  for (const key of keys) { 
    const listProp = parent[key]; 
    if (Array.isArray(listProp)) { 
octogonz commented 3 years ago

Also, fixing https://github.com/estools/esquery/issues/114 would probably cause the query engine to skip a lot of cases where this is encountered.