create account

ESLint and the beauty of JavaScript Linting by luschn

View this thread on: hive.blogpeakd.comecency.com
· @luschn · (edited)
$161.55
ESLint and the beauty of JavaScript Linting
Alright, it is time for some developer stuff again! 🤓

Ever heard of "**Linting**"? It´s basically checking out your code for potential errors. As always in the JavaScript world, there are a lot of different tools to choose from and they can do the Linting **on the fly while you type your code**:

The most famouse ones probably are "**JSLint**", followed by "**JSHint**" (a fork of JSLint, less opinionated and with more options) and then "**ESLint**". Forget about JSLint and JSHint, ESLint is all you need, because it is highly modifiable and it´s the only Linter that supports plugins. Which is great if you want to use **React** (like all smart people), because there is [eslint-plugin-react](https://www.npmjs.com/package/eslint-plugin-react).

![032f30a0-f2e1-11e5-8676-4676c2ca102a.png](https://steemitimages.com/DQmYrpGZqdTS1JotjpoYx39BEbZt1WivdWcymkcRtjmNNVC/032f30a0-f2e1-11e5-8676-4676c2ca102a.png)

## Setup
If you just want to use ESLint in all your projects (and i highly recommend that!), just install all the necessary tools globally with npm:

* `npm install -g eslint`
* `npm install -g eslint-plugin-react`
* `npm install -g babel-eslint`

You can also just combine them, of course:
* `npm install -g eslint eslint-plugin-react babel-eslint`

This will install ESLint, the ESLint React Plugin and a special parser that understands experimental/new JavaScript features. If you don´t know what **Babel** is: It turns new JavaScript code into older one for older browsers not supporting all the fancy features.

## Example .eslintrc

We´re all set up with the tools we need, there is only one thing left to do: Place a **.eslintrc** file in the root folder of all your projects. It tells ESLint which parser and plugins to use, and which rules to apply to your code. Here is an example how that file could look like:

```JSON
{
    //choose babel parser instead of default one
    "parser": "babel-eslint",

    //activate react plugin
    "plugins": [
        "react"
    ],

    //set default/recommended rules as basis
    "extends": ["eslint:recommended", "plugin:react/recommended"],

    //additional parser options: https://eslint.org/docs/user-guide/configuring
    "parserOptions": {
        "ecmaVersion": 8,
        "sourceType": "module",
        "ecmaFeatures": {
            "jsx": true,
            "experimentalObjectRestSpread": true
        }
    },

    //this sets some predefined global vars depending on the environment
    "env": {
        "browser": true,
        "node": true,
        "es6": true
    },

    //set global vars
    "globals": {
        "Phaser": false,
        "Steemit": true //true means that you are allowed to override it
    },

    //http://eslint.org/docs/rules/
    //0 - off, 1 - warn, 2 - error
    "rules": {
        "quotes": [2, "single"],
        "indent": [2, "tab", {"SwitchCase": 1}],
        "max-statements": [1, 20],
        "max-depth": [1, 3],
        "complexity": [1, 10],
        "no-unused-vars": [1, {"varsIgnorePattern": "styles|React"}],
        "no-console": 2,
        "semi": 2,
        "no-empty": 1,
        "react/prop-types": 1,
        "react/no-array-index-key": 1,
        "jsx-quotes": [2, "prefer-double"],
        "no-multiple-empty-lines": [2, {"max": 1, "maxEOF": 1}],
        "no-var": 2
    }
}
```

Check out the docs for additional information about a specific rule or feel free to ask me about the existing ones in the file!
You are probably using some editor/IDE like **Atom** or **Webstorm** already/hopefully. In Webstorm you can just activate ESLint in the settings, in Atom there is a plugin for it. They all take the .eslintrc file from the root folder.

Are you using ESLint (or something else) already? What´s your configuration and why?
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 6 others
properties (23)
authorluschn
permlinkeslint-and-the-beauty-of-javascript-linting
categorydevelopment
json_metadata{"tags":["development","javascript","dev"],"image":["https://steemitimages.com/DQmYrpGZqdTS1JotjpoYx39BEbZt1WivdWcymkcRtjmNNVC/032f30a0-f2e1-11e5-8676-4676c2ca102a.png"],"links":["https://www.npmjs.com/package/eslint-plugin-react"],"app":"steemit/0.1","format":"markdown"}
created2018-05-01 21:51:51
last_update2018-05-03 13:50:57
depth0
children3
last_payout2018-05-08 21:51:51
cashout_time1969-12-31 23:59:59
total_payout_value121.825 HBD
curator_payout_value39.724 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,753
author_reputation19,362,064,963,957
root_title"ESLint and the beauty of JavaScript Linting"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id53,317,388
net_rshares28,249,278,084,923
author_curate_reward""
vote details (70)
@grammarnazi ·
$0.02
Minor Correction
You have a minor misspelling in the following sentence: <blockquote>), just install all the neccessary tools globally with npm:.</blockquote> It should be <i>necessary</i> instead of <i>neccessary</i>.
👍  
properties (23)
authorgrammarnazi
permlinkre-luschn-eslint-and-the-beauty-of-javascript-linting-20180501t215156586z
categorydevelopment
json_metadata{"app":"steemit"}
created2018-05-01 21:51:54
last_update2018-05-01 21:51:54
depth1
children1
last_payout2018-05-08 21:51:54
cashout_time1969-12-31 23:59:59
total_payout_value0.020 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length201
author_reputation-144,064,903,190
root_title"ESLint and the beauty of JavaScript Linting"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id53,317,398
net_rshares4,329,561,226
author_curate_reward""
vote details (1)
@luschn ·
oops, corrected :)
properties (22)
authorluschn
permlinkre-grammarnazi-re-luschn-eslint-and-the-beauty-of-javascript-linting-20180501t215334106z
categorydevelopment
json_metadata{"tags":["development"],"app":"steemit/0.1"}
created2018-05-01 21:53:33
last_update2018-05-01 21:53:33
depth2
children0
last_payout2018-05-08 21:53:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length18
author_reputation19,362,064,963,957
root_title"ESLint and the beauty of JavaScript Linting"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id53,317,645
net_rshares0
@steem881 ·
Good writing! I think you have very good potential here. Check out [my post](https://steemit.com/introduceyourself/@itsadel/reintroducing-myself-plans-getting-ready-for-a-summer-of-photography) as well
properties (22)
authorsteem881
permlinkre-eslint-and-the-beauty-of-javascript-linting-20180503t151805
categorydevelopment
json_metadata""
created2018-05-03 15:18:06
last_update2018-05-03 15:18:06
depth1
children0
last_payout2018-05-10 15:18:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length201
author_reputation-796,876,938,732
root_title"ESLint and the beauty of JavaScript Linting"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id53,660,452
net_rshares0