简体   繁体   English

Intellij 插件:带 React 的 AirBnB ESLint

[英]Intellij plugin: AirBnB ESLint w/ React

Using Intellij Idea 15.0.2 on Ubuntu 15.10 and trying to configure ESLint to work.在 Ubuntu 15.10 上使用 Intellij Idea 15.0.2 并尝试配置 ESLint 以使其工作。

Followed the instructions on Jetbrains' site, but no dice.按照 Jetbrains 网站上的说明进行操作,但没有骰子。

Here's a screencap of my settings at languages&frameworks > javascript > code quality tools > ESLint.这是我在 languages&frameworks > javascript > code quality tools > ESLint 中的设置的屏幕截图。 And here's a screencap of my nodejs/npm settings within IntelliJ.是我在 IntelliJ 中的 nodejs/npm 设置的屏幕截图。

And my .eslintrc file, in the root project directory:还有我的.eslintrc文件,在根项目目录中:

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

Here's a snip from /index.js that produces no errors or warnings in IntelliJ:这是来自/index.js的一个片段,它在 IntelliJ 中不会产生任何错误或警告:

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

Here's the output when I run eslint index.js from the terminal:这是我从终端运行eslint index.js时的输出:

   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).注意:我相信 ESLint 正在运行,因为在我将.eslintrc更改为 AirBNB 版本之前,我使用的是来自 Github 的.eslintrc ,它在 IntelliJ 中引发了一些 ESLint 错误(即.eslintrc文件本身的错误,而不是我的代码)。

Once I fixed those errors, though, the plugin quieted down and didn't yell at me when I tested it by producing mistakes.但是,一旦我修复了这些错误,当我通过产生错误来测试它时,插件就会安静下来并且不会对我大喊大叫。

JetBrains (Idea, Webstorm) settings JetBrains(创意、Webstorm)设置

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

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

在此处输入图像描述

After that it should work like this:之后它应该像这样工作:

在此处输入图像描述

ESLint config ESLint 配置

ESLint doesn't come with a config. ESLint 没有配置。 You have to create your own or use a preset:您必须创建自己的或使用预设:

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

Then in your.eslintrc然后在你的.eslintrc

{
  "extends": "airbnb"
}

You can also selectively disable/modify some rules from preset (0 - disable rule, 1 - warning, 2 - error):您还可以有选择地禁用/修改预设的一些规则(0 - 禁用规则,1 - 警告,2 - 错误):

{
  '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 .阅读: 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.如果您不想使用 airbnb 配置(最流行的 javascript 样式指南),您可以创建自己的配置。 Here is the instruction for react: How to config ESLint for React on Atom Editor .这是反应的说明: 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要创建自己的预设,您必须创建名为 eslint-config-myname 的 npm 包,然后使用'extends': 'myname', http://eslint.org/docs/developer-guide/shareable-configs

You can use command line to check if eslint works:您可以使用命令行来检查 eslint 是否有效:

./node_modules/.bin/eslint .

You may though exclude some files from eslinting (node_modules are excluded by default) in.eslintignore:你可以从 eslinting 中排除一些文件(默认情况下排除 node_modules)in.eslintignore:

bundle.js

There is also a --fix switch for eslint.还有一个用于 eslint 的--fix开关。

.editorconfig .editorconfig

Good companion for ESLint is editorconfig. ESLint 的好搭档是 editorconfig。 Here is an example which works in JetBrains products:这是一个在 JetBrains 产品中有效的示例:

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/我还有一个 github 存储库,其中已经设置了这些文件https://github.com/rofrol/react-starter-kit/

Based on this https://www.themarketingtechnologist.co/how-to-get-airbnbs-javascript-code-style-working-in-webstorm/基于此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更多信息在这里https://www.jetbrains.com/webstorm/help/using-javascript-code-quality-tools.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM