Skip to content
This repository has been archived by the owner on Jan 19, 2019. It is now read-only.

Support for visitorKeys #532

Closed
bradzacher opened this issue Oct 30, 2018 · 4 comments
Closed

Support for visitorKeys #532

bradzacher opened this issue Oct 30, 2018 · 4 comments
Labels

Comments

@bradzacher
Copy link

bradzacher commented Oct 30, 2018

eslint v4.14.0 added the ability to extend the set of keys that the traverser visits on a given node.
https://github.com/eslint/eslint/blob/master/docs/developer-guide/working-with-custom-parsers.md

The reason I mention this, is because there are certain nodes that have a definition in the default config which prevents traversing children.

For example, Identifier is explicitly set to an empty array, meaning that the traverser will not traverse into the typeAnnotation.

Rules in eslint-plugin-typescript have worked around this by explicitly handling the nodes (i.e. no-explicit-any defines an Identifier selector).

This causes confusion with new contributors who assume the typeAnnotation AST node should be traversed: bradzacher/eslint-plugin-typescript#117

The confusion is compounded when certain nodes, like ClassProperty don't have entries in the default visitor key config, meaning that they fallback to the default keys, which includes typeAnnotation.

Is it worthwhile adding support for a custom set of visitorKeys, which add support for typeAnnotation where applicable?

@bradzacher
Copy link
Author

An example of why this would be helpful.

Adding support for visitorKeys would make it easier to deeply inspect types, especially on Identifiers.

This bradzacher/eslint-plugin-typescript#152 raises the following code which fails to be linted correctly (specifically for type-annotation-spacing, we should have reported errors before each : without a space preceding it):

const a : (args : {some: string}) => void;

Running this through astexplorer: https://astexplorer.net/#/gist/4ad1b40dcf08d8d10a7b453a0322f8df/e0aff8b19986c7fd05f3e0914532f06a6ee6ef70

The AST gets traversed via the types and properties:

  1. Program.body
  2. VariableDeclaration.declarations
  3. VariableDeclarator.id
  4. Identifier (standard traversal halts here).

If we want to handle this case, we have to then traverse the remaining tree manually:

  1. Identifier.typeAnnotation
  2. TSTypeAnnotation.typeAnnotation
  3. TSFunctionType.parameters
  4. Identifier.typeAnnotation

TSTypeAnnotation => validate the annotation spacing.

  1. TSTypeAnnotation.typeAnnotation
  2. TSTypeLiteral.members
  3. TSPropertySignature.typeAnnotation

TSTypeAnnotation => validate the annotation spacing.

@mysticatea
Copy link
Member

Thank you for this issue!

In fact, we are considering it on #516.

@bradzacher
Copy link
Author

awww yisss 😄

@JamesHenry
Copy link
Member

#516 was merged, thanks!

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

No branches or pull requests

3 participants