Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intellij plugin: AirBnB ESLint w/ React

Using Intellij Idea 15.0.2 on Ubuntu 15.10 and trying to configure ESLint to work.

Followed the instructions on Jetbrains' site, but no dice.

Here's a screencap of my settings at languages&frameworks > javascript > code quality tools > ESLint. And here's a screencap of my nodejs/npm settings within IntelliJ.

And my .eslintrc file, in the root project directory:

{
  "extends": "airbnb",
  "rules": {
    "comma-dangle": 0
  }
}

Here's a snip from /index.js that produces no errors or warnings in IntelliJ:

var superman = {
    default: { clark: "kent" },
    private: true
};

Here's the output when I run eslint index.js from the terminal:

   4:1   error    Unexpected var, use let or const instead                      no-var
   5:5   error    Expected indentation of 2 space characters but found 4        indent
   5:23  error    Strings must use singlequote                                  quotes
   6:5   error    Expected indentation of 2 space characters but found 4        indent

Note: I believe ESLint is running, since before I changed my .eslintrc to the AirBNB version, I was using an .eslintrc from Github that threw a number of ESLint errors in IntelliJ (that is, errors in the .eslintrc file itself, not my code).

Once I fixed those errors, though, the plugin quieted down and didn't yell at me when I tested it by producing mistakes.

like image 847
Brandon Avatar asked Jan 09 '16 23:01

Brandon


People also ask

How do I add ESLint to Intellij?

To configure ESLint automatically in the current project, open the Settings/Preferences dialog ( Ctrl+Alt+S ), go to Languages & Frameworks | JavaScript | Code Quality Tools | ESLint, and select the Automatic ESLint configuration option.

Is AirBnB an ESLint?

AirBnB released a free downloadable package (eslint-config-airbnb) that contains an . eslintrc file with AirBnB's recommended ESLint rules. This can easily be plugged into your project and AirBnB style rules are applied.


1 Answers

JetBrains (Idea, Webstorm) settings

File > Settings > Plugins > Browse repositories... > Search: eslint > Install > Restart WebStorm

File > Settings > Languages & Frameworks > JavaScript > Code Quality Tools > ESLint

enter image description here

After that it should work like this:

enter image description here

ESLint config

ESLint doesn't come with a config. You have to create your own or use a preset:

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

Then in your .eslintrc

{
  "extends": "airbnb"
}

You can also selectively disable/modify some rules from preset (0 - disable rule, 1 - warning, 2 - error):

{
  'extends': 'airbnb',
  'rules': {
    'indent': [2, 'tab', { 'SwitchCase': 1, 'VariableDeclarator': 1 }],
    'react/prop-types': 0,
    'react/jsx-indent-props': [2, 'tab'],
  }
}

Read: Turning off eslint rule for a specific line.

If you don't want to use airbnb config (most popular javascript style guide) you can create your own. Here is the instruction for react: How to config ESLint for React on Atom Editor.

To create your own preset you have to create npm package named eslint-config-myname and then use 'extends': 'myname', http://eslint.org/docs/developer-guide/shareable-configs

You can use command line to check if eslint works:

./node_modules/.bin/eslint .

You may though exclude some files from eslinting (node_modules are excluded by default) in .eslintignore:

bundle.js

There is also a --fix switch for eslint.

.editorconfig

Good companion for ESLint is editorconfig. Here is an example which works in JetBrains products:

root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true


# Matches multiple files with brace expansion notation
# Set default charset
[*.{js,jsx,html,sass}]
charset = utf-8
indent_style = tab
indent_size = 4
trim_trailing_whitespace = true

# don't use {} for single extension. This won't work: [*.{css}]
[*.css]
indent_style = space
indent_size = 2

I also have a github repository which already has these files set https://github.com/rofrol/react-starter-kit/

Based on this https://www.themarketingtechnologist.co/how-to-get-airbnbs-javascript-code-style-working-in-webstorm/

More here https://www.jetbrains.com/webstorm/help/using-javascript-code-quality-tools.html

like image 157
rofrol Avatar answered Sep 26 '22 09:09

rofrol