Enable linting in your React App
Mark Sta Ana
Dec 5 '18
γ»1 min read
Photo by rawpixel on Unsplash
Linters helps you write better code (along with tests).
When you remove the dependency on the built-in linting that comes with your IDE/Code editor you can wire up the checks to your continuous delivery pipeline. Now your build quality is improved!
In short: linting is good.
The choice of linter I've gone with is eslint, this is because it's extensible (plugins) and should continue to evolve when others won't (looking at you jshint).
Nearly all linters use some form of ruleset. These are available as plugins and I've opted for the one used bundled when you run create-react-app
.
Install
yarn add --dev eslint@latest \
babel-eslint@latest \
eslint-config-react-app \
eslint-plugin-flowtype@latest \
eslint-plugin-import@latest \
eslint-plugin-jsx-a11y@latest \
eslint-plugin-react@latest
Create the following file .eslintrc
in your react project
{
"parser": "babel-eslint",
"env": {
"browser": true,
"node": true
},
"extends": "react-app"
}
Add to your package.json
"scripts": {
"lint": "eslint src"
}
Test
yarn run lint
Bonus: enable in Visual Studio Code
Install this extension: ESLint.
This is mostly a post to let y'all know I'm still alive ;)
+1 for using eslint on your JS projects. Have you checked out prettier for keeping your code in check as you go? You can configure it to auto format your code on save in certain editors (I use Atom). Definitely worth checking out! github.com/prettier/prettier
A friend just put me on to prettier, with its integration w/ eslint. It looks like a match made in heaven!
Imagine now a world with eslint, prettier and a package like pre-commit where your linting runs automatically before being able to push your code to a branch! npmjs.com/package/pre-commit