【问题标题】:How to configure eslint indent for WebStorm?如何为 WebStorm 配置 eslint 缩进?
【发布时间】:2017-11-27 23:52:09
【问题描述】:

如何在.eslintr.json 中设置"indent" 以匹配WebStorm 中使用的默认值?

到目前为止,我尝试过的所有东西都无法匹配the official documentation

  • "indent": ["error", 2] - 给了很多Expected indentation of 2 spaces but found 4
  • "indent": ["error", 4] - 给了很多Expected indentation of 4 spaces but found 8
  • "indent": ["error", 8] - 给了很多Expected indentation of 8 spaces but found 4

我完整的 eslint 配置:

{
  "env": {
    "es6": true,
    "node": true,
    "jasmine": true
  },
  "extends": "eslint:recommended",
  "parserOptions": {
  },
  "rules": {
    "no-else-return": "error",
    "no-multi-spaces": "error",
    "no-whitespace-before-property": "error",
    "camelcase": "error",
    "new-cap": "error",
    "no-console": "error",
    "comma-dangle": "error",
    "no-var": "error",
    "indent": ["error", 4],
    "quotes": [
      "error",
      "single"
    ],
    "semi": [
      "error",
      "always"
    ]
  }
}

在输入代码时,我总是使用Ctrl+Alt+L自动格式化代码,生成的代码格式化不符合任何eslint设置。


更新

如前所述,"indent": ["error", 4] 的代码示例:

对于此代码:(通过 Ctrl+Alt+L 格式化)

const a = 123;
switch (a) {
    case 1:
        return 1;
    case 2:
        return 2;
    case 3:
        return 3;
    default:
        break;
}

结果:

3:1  error  Expected indentation of 0 spaces but found 4
4:1  error  Expected indentation of 4 spaces but found 8
5:1  error  Expected indentation of 0 spaces but found 4
6:1  error  Expected indentation of 4 spaces but found 8
7:1  error  Expected indentation of 0 spaces but found 4
8:1  error  Expected indentation of 4 spaces but found 8
9:1  error  Expected indentation of 0 spaces but found 4
10:1  error  Expected indentation of 4 spaces but found 8

示例 2

obj.format('text', {
        value: '${two}'
    }
);

结果:

2:1   error  Expected indentation of 4 spaces but found 8
3:1   error  Expected indentation of 0 spaces but found 4

示例 3

return begin()
    .then(() => {
            return callback()
                .then(data => {
                    success = true;
                    return commit();
                }, reason => {
                    return rollback();
                })
        },
        function (reason) {
            update(false, false, reason);
            return $p.reject(reason);
        });

结果:

3:1   error  Expected indentation of 8 spaces but found 12
4:1   error  Expected indentation of 12 spaces but found 16
5:1   error  Expected indentation of 16 spaces but found 20
6:1   error  Expected indentation of 16 spaces but found 20
7:1   error  Expected indentation of 12 spaces but found 16
8:1   error  Expected indentation of 16 spaces but found 20
9:1   error  Expected indentation of 12 spaces but found 16
10:1   error  Expected indentation of 4 spaces but found 8
11:1   error  Expected indentation of 4 spaces but found 8
12:1   error  Expected indentation of 8 spaces but found 12
13:1   error  Expected indentation of 8 spaces but found 12
14:1   error  Expected indentation of 4 spaces but found 8

【问题讨论】:

  • 所以你得到了 eslint 错误?具体什么时候?
  • @ush189 到处都是,就是这样,无论我尝试过什么,WebStorm 似乎对代码格式都有自己的想法。
  • 您的设置看起来不错。你能发布你的 eslint 配置吗?
  • @ush189 我已经更新了我的问题;)
  • 我有相同的 webstorm 设置,我刚刚复制了你的 eslint 配置,一切似乎都正常。因此,我认为您还需要向我们展示带有错误消息的代码屏幕截图;)

标签: javascript webstorm eslint


【解决方案1】:

Switch-Case 似乎是 eslint 关于缩进的一个特例。默认情况下,case 子句不相对于switch 缩进:

"SwitchCase" (默认: 0) 强制 case 子句的缩进级别 在 switch 语句中

查看示例:http://eslint.org/docs/rules/indent#switchcase

您需要将SwitchCase 选项设置为 1,如下所示:

"indent": [
    "error", 
    4, 
    {"SwitchCase": 1}
]

所以你完整的 eslint 配置现在看起来像这样:

{
    "env": {
        "es6": true,
        "node": true,
        "jasmine": true
    },
    "extends": "eslint:recommended",
    "parserOptions": {
    },
    "rules": {
        "no-else-return": "error",
        "no-multi-spaces": "error",
        "no-whitespace-before-property": "error",
        "camelcase": "error",
        "new-cap": "error",
        "no-console": "error",
        "comma-dangle": "error",
        "no-var": "error",
        "indent": ["error", 4, {"SwitchCase": 1}],
        "quotes": [
            "error",
            "single"
        ],
        "semi": [
            "error",
            "always"
        ]
    }
}

关于你的第二个例子,我认为这样写是很常见的:

obj.format('text', {
    value: '${two}'
});

两个括号都在同一行打开,因此您将它们关闭在同一行。如果您在该行上使用自动格式,它们将不会改变。

第三个例子看起来有点棘手。我不知道您是否可以在同一页面上获得 eslint 和自动格式。我个人更喜欢 eslint 方式,但我不知道你是否可以调整自动格式来做到这一点。

编辑:你可以这样写:

return begin()
    .then(() => callback()
        .then(data => {
            success = true;
            return commit();
        }, reason => {
            return rollback();
        }),
        function(reason) {
            update(false, false, reason);
            return $p.reject(reason);
        });

【讨论】:

  • 我在问题中添加了另一个。那是另一种特殊情况吗?我想知道其中有多少,我应该遍历所有代码...
  • 添加了带有另一个缩进问题的示例 3。请您更新您对这些额外案例的答案。
  • 关于示例 2 中的 cmets,我不明白这应该如何提供帮助。你说我的例子是有效的,自动格式不会改变它。自动格式化不会改变它,eslint 会产生问题中显示的错误。
  • 我的意思是当你像我一样写的时候,自动格式化不会改变它,eslint也不会显示任何错误。
  • 在示例 2 中 - 明白了,谢谢!现在只剩下示例 3。
【解决方案2】:

我已经通过将缩进配置添加到文件 .eslintrc.js

来修复它
module.exports = {
    "rules": {
        "indent": ["error", 4]
    }
};

【讨论】:

    【解决方案3】:

    由于WebStorm 格式,您似乎收到了错误。


    解决方案

    webstorm:

    • File -> Setting
    • 转到Editor -> Code Style -> HTML
    • Do not indent children of中,添加标签script
    • 然后再次格式化源代码。

    那么错误应该消失了。


    屏幕截图:

    【讨论】:

      猜你喜欢
      • 2021-09-01
      • 2017-06-22
      • 2017-07-31
      • 2019-01-13
      • 1970-01-01
      • 2018-04-11
      • 2019-12-28
      • 2018-03-20
      • 1970-01-01
      相关资源
      最近更新 更多