首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为WebStorm配置eslint缩进?

如何为WebStorm配置eslint缩进?
EN

Stack Overflow用户
提问于 2017-06-24 14:58:32
回答 3查看 8.3K关注 0票数 7

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

到目前为止,我尝试过的所有东西(根据正式文件 )都无法与之相匹配:

  • "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

我的完整设计:

代码语言:javascript
复制
{
  "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自动格式化代码,生成的代码格式不符合任何弹性设置。

更新

正如所问的,"indent": ["error", 4]的代码示例

此代码的(通过Ctrl+Alt+L格式化)

代码语言:javascript
复制
const a = 123;
switch (a) {
    case 1:
        return 1;
    case 2:
        return 2;
    case 3:
        return 3;
    default:
        break;
}

的结果是:

代码语言:javascript
复制
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

代码语言:javascript
复制
obj.format('text', {
        value: '${two}'
    }
);

的结果是:

代码语言:javascript
复制
2:1   error  Expected indentation of 4 spaces but found 8
3:1   error  Expected indentation of 0 spaces but found 4

示例3

代码语言:javascript
复制
return begin()
    .then(() => {
            return callback()
                .then(data => {
                    success = true;
                    return commit();
                }, reason => {
                    return rollback();
                })
        },
        function (reason) {
            update(false, false, reason);
            return $p.reject(reason);
        });

在以下方面的成果:

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-06-26 14:15:55

开关箱似乎是关于压痕的一个特例。默认情况下,case子句不会相对于switch缩进。

"SwitchCase“(缺省值: 0)对开关语句中的case子句强制缩进级别

参见这里的示例:http://eslint.org/docs/rules/indent#switchcase

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

代码语言:javascript
复制
"indent": [
    "error", 
    4, 
    {"SwitchCase": 1}
]

因此,完整的eslint配置现在将如下所示:

代码语言:javascript
复制
{
    "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"
        ]
    }
}

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

代码语言:javascript
复制
obj.format('text', {
    value: '${two}'
});

这两个括号都是在同一行上打开的,因此您可以在同一行上关闭它们。如果在该行上使用自动格式,则不会更改。

第三个例子看起来有点棘手。我不知道你是否能得到相同的页面上的复写和自动格式。我个人更喜欢这种方式,但我不知道你是否可以像这样调整自动格式。

编辑:你可以这样写:

代码语言:javascript
复制
return begin()
    .then(() => callback()
        .then(data => {
            success = true;
            return commit();
        }, reason => {
            return rollback();
        }),
        function(reason) {
            update(false, false, reason);
            return $p.reject(reason);
        });
票数 9
EN

Stack Overflow用户

发布于 2018-06-17 09:40:10

我已经通过将缩进配置添加到文件.eslintrc.js来修复它。

代码语言:javascript
复制
module.exports = {
    "rules": {
        "indent": ["error", 4]
    }
};
票数 2
EN

Stack Overflow用户

发布于 2020-11-20 15:12:09

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

解决方案

webstorm

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

那么错误就应该消失了。

屏幕截图:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44737710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档