Template for writing eslint rules
Yarn
yarn && yarn run lint
NPM
npm install && npm run lint
ESLint first parses code into an abstract syntax tree (AST) before checking any rules. So to write a rule you must understand the AST representation of your code.
Use https://astexplorer.net/ to view AST of your code and also write new rules.
Configuring https://astexplorer.net/
- set parser (</>) to '@babel/eslint-parser'
- set Transform option to 'ESLint v4'
- paste sample code in the top-left pane
- write eslint rule in bottom-left pane
- eslint rules must be in a separate package with their own package.json file
In this example I have added the package with
yarn add --dev file:./packages/eslint-plugin-sample
- the package name must start with
eslint-plugin-
- add the package to package.json dependencies
- add the package as a plugin in .eslintrc.js without the
eslint-plugin-
prefix