eslint/typescript-eslint-parser

Support for `visitorKeys`

bradzacher opened this issue · 4 comments

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?

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.

Thank you for this issue!

In fact, we are considering it on #516.

awww yisss 😄

#516 was merged, thanks!