Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I define tab size to 4 in ESLint?

I'm experimenting with ESLint in my workspace and I got little confused by the indent rule.

Here is my rule for indent setting:

"rules": {
  "indent": [
    "error", "tab"
  ]
}

The documentation only says that there are only two options: a positive number for number of spaces and "tab".

My question is how can I define tab size of 4 rather than just "tab"? Is it possible to use the indent rule for this?

I'm using vscode.

like image 260
Louie Albarico Avatar asked Feb 10 '20 03:02

Louie Albarico


People also ask

How do you fix mixed spaces and tabs?

Go to view option then go to indentation and you will find indent using space . Your problem should be fixed. If it is not fixed then go to convert indention to spaces .

What are rules in ESLint?

ESLint comes with a large number of built-in rules and you can add more rules through plugins. You can modify which rules your project uses either using configuration comments or configuration files. To change a rule setting, you must set the rule ID equal to one of these values: "off" or 0 - turn the rule off.

How do I turn off ESLint rule?

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.


2 Answers

What you're looking for isn't possible as a linter setting. ESLint lints the source files (or plain text), and all the source files have at the location of a tab is a tab character. There aren't separate tab characters for different tab lengths. Rather, each application which is displaying a tab character decides for itself how to display the tab character. VSCode may have one setting for it, but when you open the file in a different editor, or in a browser, the other editor or browser may choose to display it differently (like as a tab taking up 2 spaces, or a tab taking up 8 spaces, or something like that).

Even in a browser, there can be multiple different settings for the length of a tab, in the same tab, despite starting from the same source file. See here for an example of a related discussion.

VSCode does have a setting for the visual size of tabs, though: go to File -> Preferences -> Tab Size and set it to 4:

enter image description here

like image 116
CertainPerformance Avatar answered Sep 25 '22 16:09

CertainPerformance


You need config Vscode to define tab size to 4. ESlint is used to show an error, it isn't used to convert tab to spaces. You can use this rule on ESlint for show error when the intent difference from 4 spaces (which is default style):

{
    "indent": ["error", 4]
}
like image 35
Thai Dang Avatar answered Sep 23 '22 16:09

Thai Dang