a blog for those who code

Wednesday, 28 December 2016

Linting in Nodejs using ESLint

In this post we will be discussing about linting in Node.js using ESLint. Linting is a process of running a program that will analyse code for potential errors in your application. A Lint or a Linter is a program that verifies the code quality. In other words linting helps us to automatically find the dumb mistakes we all make, so that we can fix them without thinking.

Linting is important because it will help you to prevent certain types of bugs, it saves time on finding obvious bugs and it makes your code better & cleaner. Linting will also helps you to improve the code which you haven't seen yet, for example at the time of code reviews you actually don't know the whole code but linting multiplies your power to find out obvious bugs.

In this post we will be using ESLint for linting in Node.js. ESLint is an open source project which provides a pluggable linting utility for JavaScript.

Installing ESLint 

You can install ESLint globally by running the command npm install -g eslint.

Configuring and Usage

After installing you have to setup a configuration file as eslint --init

If you have choosen "Answer questions about your style" you have to answer questions as shown below :


Which will generate the .eslintrc.js for you having the contents

module.exports = {
    "env": {
        "browser": true
    },
    "extends": "eslint:recommended",
    "rules": {
        "indent": [
            "error",
            "tab"
        ],
        "linebreak-style": [
            "error",
            "windows"
        ],
        "quotes": [
            "error",
            "single"
        ],
        "semi": [
            "error",
            "always"
        ]
    }
};

After that you can run ESLint on any file or directory using command eslint abc.js

Lets say I have a file which reads the contents from config.json and printing it to the console as shown below :

var config = require('./config.json');
console.log(config)
console.log('Value of Value1 is ' + config.value1);

As you can see I have missed a semicolon in the second line. When I run eslint example1.js it will give me 4 errors as shown below because eslint doesn't recognize require and console.


Now to make ESLint recognize require you need to add "node" environment in eslintrc.js as shown below :

"env": {
        "browser": true,
        "node": true
    },

ESLint will still not recognize console because ESLint considered it as a best practice to avoid using methods on console. More details about the same in http://eslint.org/docs/rules/no-console.

Example of ESLint Rule for Node.js 

One of the best practices of Node.js is not to use promises inside callbacks. So in this example we will write a rule of ESLint where we will throw an error whenever we see promises inside callbacks.

Using AST Explorer I can check that if promises are inside callbacks then the syntax tree will look like :

function getData() {
  fs.exists('abc.json', function(err,dsata) {
    getJSON('abc.json')
  .then(function(story) {})
  });
}

Thus from the above Syntax Tree we can say that if the property name is "then" and the parent of the MemberExpression is CallExpression then the Promises are inside the callbacks. So we will write a rule as shown below :

export default function(context) {
  return {
    MemberExpression: function(node) {
      if(node.property.name === 'then' && node.parent.type === 'CallExpression') {
        let message = 'Promises should not be inside callbacks';
        context.report(node.property, message);
      }
    }
  };
};

So start linting your Node.js applications by some means immediately if you are not already doing. It will make you better developer with less stress and more time.

Please Like and Share the CodingDefined Blog, if you find it interesting and helpful.

No comments:

Post a Comment

Contact Us

Name

Email *

Message *

Sponsers

Best Hosting Plans

CC License