ESLint for Better Productivity and Higher Accuracy

What is focus? Concentration of power. When writing Javascript code, most of us focus on solving very complex problems. While dedicating intellectual capacity to the solution, small code quality mistakes are often made. It takes time and effort to fix these mistakes, provided that you notice them. If you don't, your teammates will definitely point them out. On your worst days, you may end up feeling embarrassed: despite the fact that you stick to the highest possible standards, you are still human and you make mistakes you should not be making.

Many people have realized in the past that code quality checks have to be automated. For this reason, Javascript linters were invented. Some of you may already use a linter like JsLint or JsHint. They are all great tools for increasing your accuracy. In this article, I will introduce you to an alternative: ESLint. ESLint provides you with full control over the linting process, including the application of your own custom rules.

Never Violate Agreements

Excellent code quality is a necessary condition for excellent maintainability. If you work in a team, in order to avoid confusing each other, you have to stick to standards. If you think you don't work in teams, think again! Unless you create hobby projects for yourself, you have to write code under the assumption that someone else will contribute to your code base. Your coding standards have to be enforced and automatized. This is where ESLint comes in: it gives you the freedom to focus on the problem you would like to solve, and let the rest happen on autopilot. You may make mistakes such as:

  • small formatting and indentation errors
  • forgetting to remove debuggers and console.log statements
  • typos in variable names
  • using the wrong type of quotes
  • using == instead of ===

This list is not exhaustive and can never be exhaustive. Some teams have specific agreements that no-one has ever thought about. ESLint gives you the chance to create and enforce these agreements via custom rules.

Get started

Similarly to my last post on ES6 modules and Webpack, you will need NodeJs to install ESLint.

You will also need a configuration file .eslintrc, wherever you would like to run ESLint. The documentation describes the possible settings you can include in your JSON object in your configuration file. It would be useless to include a list here as new options are continuously added as new versions come out.

I will write an example file with a couple of injected errors ESLint should catch. This function should calculate n! according to the recursive definition n! = n * (n - 1)!, including the exit criterion 0! = 1.

How many errors can you spot in this code? Regardless of the quality standards you stick to, there is one error that does not even let the code execute.

Automatic semicolon insertion and the return statement: Semicolons are inserted automatically at the end of each line where the statement is not supposed to continue. This happens to our first return statement too. The code


after the semicolon insertion. This results in a syntax error. Let's verify the error with ESLint:

The solution to this problem is that the open brace of the returned object has to be on the same line as the return statement. After making this change, ESLint can start detecting other problems:

Eleven linting errors. We have some work to do.

First of all, I did not use strict mode. We can easily address this problem by adding "use strict" to the beginning of the file. It is good practice anyway.

We were also told that factorial was never used in the code. ESLint is intelligent enough to detect that a recursive call does not count as a usage. For the sake of simplicity, I will encapsulate factorial function in a self-invoking function.

The variable utils is only there for demonstration purposes. Let's continue with the errors.

An unexpected debugger statement was found in the code. I have seen people push debugger statements to remote repositories and it's not a nice experience to load an application just to find out that it enters debug mode under some circumstances. ESLint is intelligent enough to detect debuggers, therefore you will never have to worry about pushing a forgotten debugger statement.

Warning: Even if executing ESLint is an integral part of your workflow, it should not be made responsible for handling debugger statements. You have to have a security net making sure that debuggers never end up on production. When preparing production code, make sure you automatically remove all debugger statements.

We have two errors with the name curly. ESLint does not like code of format

The reason is that it is very easy to shut off our brain and write the following:

In practice, statement2 is executed regardless the value of the condition as only statement1 belongs to the if statement. Although this error has never happened to me, it could happen to others, so I mostly respect the curly rule. The only exception could be the shorthand

This shorthand is more compact, more readable, and the above reasoning does not apply as we cannot trick ourselves with wrong indentation.

The semi errors indicate missing semicolons. Although Javascript inserts them automatically, it is still good practice to go the extra mile (extra millimeter to be exact) and write those semicolons explicitly. Just like free text, Javascript code should also be readable. Imagine reading this article without full stops. It would not be convenient, right?

The rule eol-last is not a big deal. It just indicates the lack of terminating new line at the end of the file. Let's add it.

We are almost done. If you prefer strict equality to ==, don't turn eqeqeq off! I erroneously wrote the condition n == 0. Let's correct it to n === 0.

Our beautified code looks like this:

Another linting reveals that most errors are gone:

Let's make the conscious decision that this rule is not for us because we prefer single quotes to double quotes. We can easily change our preferences by adding a rule to .eslintrc.

The new rule specifies that we have to use single quotes for strings. Double quotes are only tolerated inside a string for the purpose of avoiding escaping. Let's run the linter again!

Surprisingly, we used single quotes as well as double quotes in this function. Mixing the two is not advised as it indicates lack of consistency. The guilty double codes were added with the string "use strict". After changing the quotes and running ESLint again, all errors will disappear.

Let's add a built in ESLint rule to our configuration file. As you might have noticed, I used the form if( condition ). Some coding standards require a space between the keyword and the open parenthesis so that it is easier to tell the difference between a statement and a function. Let's add this rule to the .eslintrc file.

You can find the space-after-keywords rule in the list of all ESLint rules in the official documentation.

Re-linting the code gives you two new errors.

Fixing the indentation errors will make linting succeed. Let's have a look at the final result.

The code is a lot nicer to read than at the start of the article, isn't it?

Always Use a Linter

Automation has a key role in increasing productivity and accuracy. If you want to write maintainable code, make linting a must for yourself and your team! A safety net that catches your errors gives you the freedom to focus on finding the most efficient, elegant and maintainable solution to the problems you face with.

I have very ambitious goals with ESLint, namely covering my coding standards as much as possible. This will definitely include writing some custom rules. If you get stuck with ESLint or you would like to read about a specific topic related to linting, write a comment below.