Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align const indent in ESLint?

I'm using ESLint to make my JavaScript code style consistent. My favorite indentation level is 4 and I want my declarations style to be this:

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.

If I set the rule to:

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

The correct align will be 4 spaces:

const cc = 3,
    ddd = 4;

And if I set the rule to 2:

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

It's going to expect 8 spaces:

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?

like image 538
Robo Robok Avatar asked Apr 28 '17 21:04

Robo Robok


People also ask

What is the difference between alignment and indentation?

is that alignment is an arrangement of items in a line while indentation is the act of indenting or state of being indented.

How do you fix error mixed spaces and tabs no mixed spaces and tabs?

Go to view option then go to indentation and you will find indent using space . Your problem should be fixed.

How do I disable ESLint on next line?

If you want to disable an ESLint rule in a file or on a specific line, you can add a comment. On a single line: const message = 'foo'; console. log(message); // eslint-disable-line no-console // eslint-disable-next-line no-console console.

How do you bypass ESLint?

To temporarily turn off ESLint, you should add a block comment /* eslint-disable */ before the lines that you're interested in: /* eslint-disable */ console.


1 Answers

According https://eslint.org/docs/rules/indent you can use: 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

like image 143
Igor Golopolosov Avatar answered Oct 19 '22 05:10

Igor Golopolosov