ESLint now supports many different types of AST selectors
ESLint AST selectors just got super powers 👍
With the new release of ESLint (v3.18.0), rule AST selectors just got super powers!
Previously, selectors were limited to matching against single node types. For example, if you were creating a custom ESLint rule, you could match against all
FunctionDeclaration nodes like this:
But if you wanted to match, for example, against
FunctionDeclaration nodes that have more than three parameters, you had to first match against all
FunctionDeclaration nodes, and then filter them out inside your callback:
Starting with version 3.18.0, ESLint now supports many different types of AST selectors. Here are just a few:
- by attribute (
- by first child (
- by descendant (
- by adjacent sibling (
VariableDeclaration ~ VariableDeclaration)
- by “classes” of AST nodes (
:function, for all "function” types of nodes, such as
- by negation (
- by matches-any, to match multiple selectors (
For a full list of supported selectors, see the official documentation.
With the new selectors support, the previous example can be greatly simplified:
That’s it! Now our function will only be called for
FunctionDeclaration nodes that have more than 3 parameters.
Another great use of the new selectors feature is in combination with the no-restricted-syntax rule, which just became insanely more powerful. Here are just a few examples:
Disallow usage of
fit in your tests:
Enforce that calls to
setTimeout always have two arguments:
My favorite part of the new selector support is how much boilerplate can be cleaned up from some rules thanks to it.
Here’s just a few examples of refactorings the team did on some of the core rules, thanks to this feature:
Thanks for reading! I hope you liked learning more about the brand-new AST selector support f0r ESLint!