KEEP YOUR CODE CLEAN WITH ESLINT


LEARN THE BASICS OF THE MOST POPULAR JAVASCRIPT LINTER, WHICH CAN HELP TO MAKE YOUR CODE ADHERE TO A CERTAIN SET OF SYNTAX CONVENTIONS, CHECK IF THE CODE CONTAINS POSSIBLE SOURCES OF PROBLEMS AND IF THE CODE MATCHES A SET OF STANDARDS YOU OR YOUR TEAM DEFINE

ESLint is a JavaScript linter.

WHAT IS LINTER?

Good question! A linter is a tool that identifies issues in your code.

Running a linter against your code can tell you many things:

  • if the code adheres to a certain set of syntax conventions
  • if the code contains possible sources of problems
  • if the code matches a set of standards you or your team define

It will raise warnings that you, or your tools, can analyze and give you actionable data to improve your code.

ESLINT

ESLint is a linter for the JavaScript programming language, written in Node.js.

It is hugely useful because JavaScript, being an interpreted language, does not have a compilation step and many errors are only possibly found at runtime.

ESLint will help you catch those errors. Which errors in particular you ask?

  • avoid infinite loops in the for loop conditions
  • make sure all getter methods return something
  • disallow console.log (and similar) statements
  • check for duplicate cases in a switch
  • check for unreachable code
  • check for JSDoc validity

and much more! The full list is available at https://eslint.org/docs/rules/

The growing popularity of Prettier as a code formatter made the styling part of ESLint kind of obsolete, but ESLint is still very useful to catch errors and code smells in your code.

ESLint is very flexible and configurable, and you can choose which rules you want to check for, or which kind of style you want to enforce. Many of the available rules are disabled and you can turn them on in your .eslintrc configuration file, which can be global or specific to your project.

INSTALL ESLINT GLOBALLY

Using npm.

npm install -g eslint

# create a `.eslintrc` configuration file
eslint --init

# run ESLint against any file with
eslint yourfile.js

INSTALL ESLINT LOCALLY

npm install eslint --save-dev

# create a `.eslintrc` configuration file
./node_modules/.bin/eslint --init

# run ESLint against any file with
./node_modules/.bin/eslint yourfile.js

USE ESLINT IN YOUR FAVOURITE EDITOR

The most common use of ESLint is within your editor of course.

COMMON ESLINT CONFIGURATIONS

ESLint can be configured in tons of different ways.

AIRBNB STYLE GUIDE

A common setup is to use the Airbnb JavaScript coding style to lint your code.

Run

yarn add --dev eslint-config-airbnb

or

npm install --save-dev eslint-config-airbnb

to install the Airbnb configuration package, and add in your .eslintrc file in the root of your project:

{
  "extends": "airbnb",
}

REACT

Linting React code is easy with the React plugin:

yarn add --dev eslint-plugin-react

or

npm install --save-dev eslint-plugin-react

In your .eslintrc file add

{
  "extends": "airbnb",
  "plugins": [
      "react"
  ],
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    }
  }
}

USE A SPECIFIC VERSION OF ECMASCRIPT

ECMAScript changes version every year now.

The default is currently set to 5, which means pre-2015.

Turn on ES6 (or higher) by setting this property in .eslintrc:

{
  "parserOptions": {
    "ecmaVersion": 6,
  }
}

FORCE STRICT MODE

{
  "parserOptions": {
    "ecmaFeatures": {
      "impliedStrict": true
    }
  }
}

MORE ADVANCED RULES

A detailed guide on rules can be found on the official site at https://eslint.org/docs/user-guide/configuring

DISABLING RULES ON SPECIFIC LINES

Sometimes a rule might give a false positive, or you might be explicitly willing to take a route that ESLint flags.

In this case, you can disable ESLint entirely on a few lines:

/* eslint-disable */
alert('test');
/* eslint-enable */

or on a single line:

alert('test'); // eslint-disable-line

or just disable one or more specific rules for multiple lines:

/* eslint-disable no-alert, no-console */
alert('test');
console.log('test');
/* eslint-enable no-alert, no-console */

or for a single line:

alert('test'); // eslint-disable-line no-alert, quotes, semi
Anúncios

Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. Sair /  Alterar )

Foto do Google+

Você está comentando utilizando sua conta Google+. Sair /  Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair /  Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair /  Alterar )

Conectando a %s

%d blogueiros gostam disto: