Pages

Saturday, August 26, 2017

NodeJS – Linting

Process of checking source code for programmatic as well as stylistic errors is called linting. These tools provide us the code analysis. They don’t run the code but inspect it by looking for typos or anti-patterns
Bug detection and maintainability are some of the uses we obtain by using the linting tools.

Eslint – Eslint is an Open Source Project whose goal is to provide a pluggable linting utility for Java Script. ESLint is a tool that analyses your code and points out any issues it finds. It can find bugs, potential problem areas, poor coding styles and stylistic issues. Best of all it’s highly configurable. Eslint is chosen to be a linting tool for Nodejs applications in Cloud automation.

Some of the noticeable points about Eslint are
·         ECMAScript is a Standard for scripting languages. Languages like JavaScript are based on the ECMAScript standard.
·         ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, with the goal of making code more consistent and avoiding bugs. 
·         Easily Pluggable and Extensible.
·         Written in JavaScript on Node.js
·         Supports custom reporters
·         Supports ES6 ( new ECMAScript Standards )
·         All rules are plugins, more can be added at runtime.
·         Different parsers can be used (Esprima, Espree or Babel are currently compatible).
·         Integrates with editors, build systems, command line tools, and more!
Installation
Eslint can be installed either locally or globally.
Install Eslint locally as,
     Install eslinnt : npm install eslint --save-dev
     setup a configuration file : ./node_modules/.bin/eslint --init
     Run ESLint on Source code as :  ./node_modules/.bin/eslint yourfile.js

Install Eslint globally as
     Install eslint : npm install -g eslint
     Setup a configuration file :  eslint –init
     Run ESLint on any file or directory :  eslint yourfile.js

Note - eslint --init is intended for setting up and configuring ESlint on a per-project basis and will perform a local installation of ESlint and its plugins in the directory in which it is run. If you prefer using a global installation of ESlint, any plugins used in your configuration must also be installed globally.

Eslint getting Started –
To get started with the Eslint tool, first create a configuration file using

/node_modules/.bin/eslint --init 

This will present with options
  1. You will be asked a few questions about your coding style, and the generated config file will be based on your answers
  2. community-developed style guides available as shared ESlint configs which can be installed and used
  3. It will result in the most strict configuration (enabling the most rules) 
Follow the below steps to get started with Eslint 
Once the configuration file is created , run the test on sample code below

function test() {
  var myVar = 'Hello, World';
  console.log(myvar);
}

We will be given with the Eslint response as below
We can obtain configuration file that are sharable at this moment. We have chosen to use “google” configuration file and made tweaks to the rules according to the project.

No comments :

Post a Comment