ESLint

My first step is to integrate eslint. There is a gulp-eslint package, so I added that to my package.json and required it at the top of my Gulpfile.js – just like all the other plugins. My rule for running eslint is fairly simple:

Run this rule using the right-click->Run in the Task Runner Explorer of Visual Studio and you will see the following:

There are a bunch of errors in init.js that we can just fix – things like double-quotes for strings and a new line at the end. However handling the non-obvious ones will take some work. Let’s cover them now:

1. Require is not defined

Require is a global and is defined within our HTML page, so I need to tell ESLint that this is ok. While I am at it, I also need to tell ESLint that jQuery is ok, but only in the $ form – not in the long form. I can do this with this task configuration by including an options object and specifying a globals option:

2. Document is not defined

The browser defines document. You may also see references to ‘window’ is not defined, which is the same problem. I need to tell ESLint my application runs in a browser. I do this by specifying an environment declaration in the configuration like this:

4. Invalid import declaration

You’ve probably guessed by now that this is because of ECMAScript 6 support. I need to enable this and the configuration is a little more extensive and certainly not documented well. I need to add an environment to our config to tell ESLint that I write es6 code and then add eslint configuration inside of package.json to tell it which features I use that are non-standard:

The extra eslintConfig section tells ESLint that I am using ECMAScript 6 modules. The configuration of the environment is done in the Gulpfile.js task definition in a similar way to the browser definition earlier:

When I run eslint now I get more errors but at least I am syntax checking the entire ES6 class file now. Note also that because I am running in ES6 mode, the use strict I added earlier is no longer required.

That’s 66 errors to go! In doing all the obvious ones, note the error on line 76:

76:22 error Expected '===' and instead saw '==' eqeqeq

That’s actually a major style problem that ESLint found. I’m doing this all the time as a habit, so getting ESLint to break me of this habit is a good thing.

5. Unexpected console statement

There are a bunch of errors around the console statement. I left them in because I can programmatically remove them when I am minifying my code for production. I really want to make ESlint ignore them. That’s a two step process. Firstly I need to tell ESLint that console is a global – I do that the same way I did with require earlier. Once I did that and re-ran eslint, I noticed that I had a mis-spelling of console, so I corrected that (thank you eslint!) The other error eslint is throwing is Unexpected console statement. It is best to disable this rule since I use a lot of console statements. To do this, I have to alter the eslintConfig inside of package.json:

The rule name is printed on the eslint output next to the error message. If you’ve done everything correctly, you can rejoice now because you have a clean lint run. However, make sure you always do the following:

Run eslint before you check in your code

Run eslint whenever you complete a class or major piece of functionality

Care about the errors enough to deal with them

I’ve uploaded this code to my GitHub Repository for you to review. Note that it is the complete code, so check out the other articles in the series that cover everything.