Eslint AirBNB有4个空格用于缩进

Not*_*ous 20 eslint

我正在配置eslint并使用AirBNB样式指南.

我想把缩进(假设是2个空格)覆盖为4个空格.但无论我在.eslintrc中做什么,我都无法得到这个错误,因此我可以使用4个空格的缩进.

我的代码库中到处都有消息"预期缩进2个空格的聊天,但发现了4.(react/jsx-indent)".

我正在使用eslint 4.9.0.我该如何解决这个问题?谢谢.

Not*_*ous 46

好的,这样做相对容易,可以通过在eslint配置中添加以下内容来实现:

// Indent with 4 spaces
"indent": ["error", 4],

// Indent JSX with 4 spaces
"react/jsx-indent": ["error", 4],

// Indent props with 4 spaces
"react/jsx-indent-props": ["error", 4],
Run Code Online (Sandbox Code Playgroud)

  • 我应该在哪里添加它?eslint 配置文件在哪里? (7认同)
  • @OhadR 将其添加到 eslint 配置的 `rules` 属性中,该属性应该是项目根目录中的 `.eslintrc` 文件(与 `package.json` 文件相同的文件夹),尽管文件的扩展名有所不同。它也可以位于您的“package.json”内部。 (2认同)

Ale*_*Sil 14

上面的代码应该添加到ESlint 配置中的规则字段中。

module.exports = {
"extends": "eslint:recommended",
"rules": {
    // enable additional rules
    "indent": ["error", 4],
    "linebreak-style": ["error", "unix"],
    "quotes": ["error", "double"],
    "semi": ["error", "always"],

    // override default options for rules from base configurations
    "comma-dangle": ["error", "always"],
    "no-cond-assign": ["error", "always"],

    // disable rules from base configurations
    "no-console": "off",
}
Run Code Online (Sandbox Code Playgroud)

[来源- 请参阅使用“eslint:推荐” ]


Leo*_*ele 10

由于接受的答案不完整并且该答案的编辑队列已满,因此我添加了以下补充:

要简单地禁用2 空格 ident 规则,请将以下行添加到rulesesling 配置文件的属性中:

"indent": "off",
Run Code Online (Sandbox Code Playgroud)

要覆盖规则(可能是您想要的)以检查 4 个空格而不是 2 个空格,请添加以下行:

"indent": ["error", 4],
Run Code Online (Sandbox Code Playgroud)

它应该看起来像这样:

// eslintrc.js
module.exports = {
  "extends": [
    "eslint:recommended",
    "airbnb",
  ],
  "rules": {
    "indent": ["error", 4],
  },
};
Run Code Online (Sandbox Code Playgroud)

ESLint 配置位置

您的 eslint 配置可能位于以下任意文件中:

  1. .eslintrc.js
  2. .eslintrc.cjs
  3. .eslintrc.yaml
  4. .eslintrc.yml
  5. .eslintrc.json
  6. .eslintrc
  7. 或者它可能在您的package.json某个"eslintConfig"属性中。

有关 eslint 配置的更多信息:https://eslint.org/docs/user-guide/configuring(在“配置文件格式”标题下)