简体   繁体   English

如何为 WebStorm 配置 eslint 缩进?

[英]How to configure eslint indent for WebStorm?

How to set "indent" in .eslintr.json to match the default used in WebStorm?如何在.eslintr.json设置"indent"以匹配.eslintr.json中使用的默认值?

在此处输入图片说明

Everything I've tried so far, as per the official documentation can't match it:到目前为止,我尝试过的一切,根据官方文档都无法匹配:

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

My complete eslint configuration:我完整的 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"
    ]
  }
}

As I type the code, I always use Ctrl+Alt+L to auto-format the code, and the code formatting produced doesn't comply with any eslint settings.当我输入代码时,我总是使用Ctrl+Alt+L来自动格式化代码,并且生成的代码格式不符合任何 eslint 设置。


UPDATE更新

As was asked, a code example for "indent": ["error", 4] :正如所问, "indent": ["error", 4]的代码示例"indent": ["error", 4]

For this code: (formatted via Ctrl+Alt+L)对于此代码:(通过 Ctrl+Alt+L 格式化)

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

results in:结果是:

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

example 2例子2

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

results in:结果是:

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

example 3例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);
        });

results in:结果是:

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

Switch-Case seems to be a special case for eslint regarding indentation. Switch-Case 似乎是 eslint 关于缩进的一个特例。 Per default the case clauses are not indented relative to the switch :默认case下, case子句不相对于switch缩进:

"SwitchCase" (default: 0) enforces indentation level for case clauses in switch statements “SwitchCase”(默认值:0)对 switch 语句中的 case 子句强制执行缩进级别

See here for an example: http://eslint.org/docs/rules/indent#switchcase请参见此处的示例: http : //eslint.org/docs/rules/indent#switchcase

You need to set SwitchCase option to 1 like so:您需要像这样将SwitchCase选项设置为 1:

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

So your complete eslint config will now look like this:所以你完整的 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"
        ]
    }
}

Regarding your 2nd example I think it is common to write it like this:关于你的第二个例子,我认为这样写是很常见的:

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

Both parenthesis are opened on the same line, so you close them on the same line.两个括号都在同一行打开,因此您可以在同一行关闭它们。 If you use auto format on that lines, they will not change.如果您在该行上使用自动格式,它们将不会改变。

The third example looks a bit tricky.第三个例子看起来有点棘手。 I don't know if you can get eslint and auto format on the same page for that one.我不知道您是否可以在同一页面上为该页面获取 eslint 和自动格式。 I personally would prefer the eslint way, but I don't know if you can tweak the auto format to do it like that.我个人更喜欢 eslint 方式,但我不知道你是否可以调整自动格式来做到这一点。

Edit: You could write it like that:编辑:你可以这样写:

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

I have fixed it by added the indent configuration into the file .eslintrc.js我通过将缩进配置添加到文件.eslintrc.js 中来修复它

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

Seems you are getting the error due to WebStorm formatting.由于WebStorm格式,您似乎收到错误WebStorm


Solution解决方案

In webstorm :在网络webstorm

  • File -> Setting File -> Setting
  • Go to Editor -> Code Style -> HTML转到Editor -> Code Style -> HTML
  • In Do not indent children of , add tag script .Do not indent children of ,添加标记script
  • Then format the source code again.然后再次格式化源代码。

Then the error should be gone.那么错误应该消失了。


Screen shot:截屏:

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

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