Writing custom EsLint rules

Introduction

Kenneth Truyers

Writing custom EsLint rules

Writing custom EsLint rules

In statically compiled languages, we usually lean on the compiler to catch out common errors (or plain stupidities). In dynamic languages we don’t have this luxury. While you could argue over whether this is a good or a bad thing, it’s certainly true that a good static analysis tool can help you quite a bit in detecting mistakes. For Javascript, a few tools are available: there’s the good old JsLint, which is very strict, JsHint, created because not all of us are Douglas Crockford and then there’s EsLint. In this post I’ll show you how to create custom EsLint rules.

EsLint is quite a nice alternative for JsHint and is very flexible. While JsHint certainly has its benefits and comes out of the box with a lot of configurable options, EsLint allows you to configure your own custom rules.

These custom EsLint rules can be added to their library and released to the community as optional extra checks, they can be company specific to enforce a certain coding style or they can be project specific.

In this post I want to talk about creating project specific custom EsLint rules. It’s easily transferrable to a more common use plugin if you want to (by publishing to NPM). I found creating a project specific plugin has a few more hurdles, so I’ll explain that.

Analyzing code

Asbtract Syntax Trees

Before we start writing custom EsLint rules, I first want to show how code is analyzed and how we can plug in to that. In order to analyze code we must first build an abstract syntax tree. In this case we want to build an ES 6 abstract syntax tree (AST). An AST is essentially a data structure which describes the code. The next example shows some sample code and the corresponding syntax tree:

var a = 1 + 1;

The above visualization can also be presented as a pure data structure, here in the form of JSON:

Writing custom EsLint rules

Since all of this AST-generation and node-walking is not specific to any rule, it can be externalized, and that’s exactly what EsLint gives us. EsLint builds the syntax tree and walks all the nodes for us. We can then define interception points for the nodes we want to intercept. Apart from that, EsLint also gives us the infrastructure to report on problems that are found. Here’s the above example rewritten as an EsLint rule:

This can then be plugged in to EsLint and it will report the errors for any Javascript code you throw at it.

EsLint plugins

In order to write a custom EsLint rule, you need to create an EsLint plugin. An EsLint plugin must follow a set of conventions before it can be loaded by EsLint:

It must be a node package (distributed through NPM, although there’s a way around it, read on …)

Its name must start with eslint-plugin

The documentation mentions a way to write custom rules in a local directory and running them through a command-line option. This still works, but is deprecated and will soon break in newer versions of EsLint. It’s recommended to go the plugin-route as described in this post.

Creating the plugin

The generator sets you up with a nice folder structure, including tests, a proper description and some documentation. However, if you just want to write some quick rules, I find it easier to just create a folder and the structure myself. Essentially, you need two files:

Installing the plugin

As I mentioned before, if you want to share your plugin, you can distribute it via NPM. This doesn’t always make sense though as you might have project specific rules. In those cases, you can just create the folder with your plugin locally and commit it to your code repository. For it to work, you still need to install it as a node package though. You can do that with the following NPM command:

npm install -S ./my-eslint-plugin

This will install the package from the local folder my-eslint-plugin. That way, you can keep the rules locally to your project and still use them while running EsLint.

Configuring the plugin

For EsLint to recognize and use the plugin we have to notify it through the configuration. We need to do two things:

Tell it to use the plugin

Switch on the rules

To tell it to use plugin, we can add a plugins node into the configuration, specifying the name of the plugin (without the “eslint-plugin”-prefix):

"plugins": [ "my-eslint-plugin" ]

Next we need to define the rules:

"rules": { "my-eslint-plugin/var-length": "warn" }

With the plugin installed, you can now run EsLint and it will report on one letter variable names.

Example

As for general styling rules, EsLint has probably most of them already covered, and the ones it hasn’t are probably quite obscure. Custom EsLint rules come in handy on a project-level basis.

As an example, I’m currently working on an Angular 1 project. The intention is to port this over to a different framework soon. Because of that, we want to make sure we’re as independent of Angular as possible. There are certain things we can do just as easily in plain JS instead of using angular’s utility methods. For others, we can use different libraries that we can port over as well when we port the application.

Now, we don’t want to go off and change all these occurrences at once, because that would be a lot of upfront work. Ideally, we want the following:

Get notified when there’s a call to an angular-method which could be done easily in plain JS in the module we’re working on

Get notified on the CI-server (with a warning) if an angular-method is used

Once we get rid of the warnings for that angular-method, fail the build on the CI-server if that call is detected again

We then enabled the rules with a warning in our configuration. As soon as we notice no more warnings for one of these rules, we will switch them to errors. The CI-build is configured to fail when EsLint finds an error. On top of that we have the EsLint plugin for VsCode, which looks like this in the editor:

This combination ensures that we clean up angular calls while we continue development and that no new calls will be introduced.

Sidenote: the rules here are not foolproof since someone could assign angular to a temp variable and then call the methods on the temp variable. Be that as it may, we want to catch the general use case with a simple rule. We could probably write a more thorough analyzer, but it would take a lot of time. Since all of this code still needs to go through code reviews, we don’t worry too much about it.

Other possibilities

The above example is something that was very convenient for our use case, but the possibilities are endless. Here are a few things you could achieve with this:

Ensure a user message is shown when HTTP calls are initiated and that they’re properly removed once it ends.

Ensure jQuery isn’t used when we’re using a SPA-framework (or only in certain modules)

When using jQuery, ensure you’re always calling event-handlers using the .on method instead of the shorthand ,click and similar

There are plenty of possibilities for custom EsLint rules and most of it depends on your project. What other ideas do you have?

Existing plugins

Of course, there are plenty of existing EsLint plugins for existing frameworks on NPM already. If you’re using one of these frameworks, it’s worth checking out the rules and see if you could benefit from enabling some of these