Intro to ESLint (ft. Daft Punk)Published on
If you’ve never tried ESLint, I strongly recommend it. Setting it up will make things:
It takes some getting used to. Configuring countless rules, checking them up online, accidentally pushing code with linting errors… but you’ll get there.
One gotcha is that ESLint doesn’t support experimental ECMAScript features out of the box, except for the highly requested object rest/spread feature. If you want experimental features, you could plug in the babel-eslint parser, but you should avoid using those features in production anyway, because they might not end up in the spec.
You’ll become a better developer because you’ll learn why some rules are enforced, and you’ll quickly learn to follow them. ESLint and many of its plugins are very well documented, so you can learn a lot there.
For example, if you’re a React developer, you might be tempted to use bind and arrow functions in your component’s render method, but this hurts performance because it creates new functions on each render, as described in the jsx-no-bind rule of eslint-plugin-react.
Before you even run your script, the linter can warn you if you made a silly mistake. For example, if you mess up an import/require statement, eslint-plugin-import can warn you about this ahead of time.
Using a linter also eliminates some of the choice fatigue. Things can be written in multiple ways, and often one choice is not particularly better than the other. ESLint can enforce a certain style so you don’t have to waste time trying to decide. Remember, there are no crazy rules, what’s important is that everyone sticks to them.
To be even faster, you should install an ESLint plugin for your editor, it will improve the speed of catching errors because you’ll see warnings as you type, instead of being welcomed by 50 errors when you finally run the lint command.
Instead of making your project’s contributors read a style guide (let’s be real, who remembers to read
CONTRIBUTING.md), make linting a part of testing so your contributors won’t be able to skip it.
If you’d like to enforce a rule which ESLint or its plugins don’t offer yet, you can write your own! If you’ve never worked with an AST before, it can be quite a learning curve, but this skill will be really come in handy for using some other tools as well, like JSCodeShift. I suggest reading the documentation of ast-types a few times, along with playing with the AST explorer, then try contributing to an existing ESLint plugin.
A great example of extending ESLint is eslint-plugin-ava. The team behind the awesome test runner AVA made this plugin to warn you about mistakes you might be making, which the framework itself cannot catch.