简体   繁体   English

如何在ESLint中对齐const缩进?

[英]How to align const indent in ESLint?

I'm using ESLint to make my JavaScript code style consistent. 我正在使用ESLint使我的JavaScript代码样式保持一致。 My favorite indentation level is 4 and I want my declarations style to be this: 我最喜欢的缩进级别是4,我希望我的声明样式是这样的:

function () {
    let a = 1,
        bbb = 2;

    const cc = 3,
          ddd = 4;
}

There is a problem though, since indent rule per each structure takes a number, which is multiplication of base indentation. 但是存在一个问题,因为每个结构的缩进规则需要一个数字,这是基本缩进的乘法。 If I set my basic indentation to 4, I don't seem to be able to align consts. 如果我将基本缩进设置为4,我似乎无法对齐consts。

If I set the rule to: 如果我将规则设置为:

"indent": ["error", 4, {"VariableDeclarator": {"const": 1}}],

The correct align will be 4 spaces: 正确的对齐将是4个空格:

const cc = 3,
    ddd = 4;

And if I set the rule to 2: 如果我将规则设置为2:

"indent": ["error", 4, {"VariableDeclarator": {"const": 2}}],

It's going to expect 8 spaces: 它预计会有8个空格:

const cc = 3,
        ddd = 4;

It doesn't accept floating numbers. 它不接受浮动数字。 How can I align var, let and const the way I want using ESLint? 如何使用ESLint以我想要的方式对齐var,let和const?

According https://eslint.org/docs/rules/indent you can use: indent: ["error", 2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } } 根据https://eslint.org/docs/rules/indent您可以使用: indent: ["error", 2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }

Also you can avoid this problem using next rules: https://eslint.org/docs/rules/one-var https://eslint.org/docs/rules/one-var-declaration-per-line 您也可以使用下一条规则来避免此问题: https//eslint.org/docs/rules/one-var https://eslint.org/docs/rules/one-var-declaration-per-line

Maybe it's worth to try: 也许值得尝试:

function () {
    let a = 1,
        bbb = 2;

    const 
        cc = 3,
        ddd = 4;
}

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

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