59

I traying setup my vim based typescript developing environment, but have an issue with indent management.

enter image description here

Probably 'eslint' says: indent: Expected indentation of 2 spaces but found 4. after prettier reformat.

My .eslintrc.js:

module.exports = { 
  parser: '@typescript-eslint/parser', // Specifies the ESLint parser
  extends: [ 
    'plugin:react/recommended', // Uses the recommended rules from @eslint-plugin-react
    'plugin:@typescript-eslint/recommended', // Uses the recommended rules from @typescript-eslint/eslint-plugin
    'prettier/@typescript-eslint',
    'plugin:prettier/recommended',
  ],
  parserOptions: { 
    ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
    sourceType: 'module', // Allows for the use of imports
    ecmaFeatures: { 
      jsx: true, // Allows for the parsing of JSX
      tsx: true, // Allows for the parsing of TSX ???
    },
  },
  rules: { 
    indent: ['error', 2],
    quotes: ['error', 'single'],
    semi: ['error', 'never'],
    'sort-keys': ['error', 'asc', { caseSensitive: true, natural: false }],
  },
}

My .prettierc:

 module.exports = { 
  semi: false,
  trailingComma: 'all',
  singleQuote: true,
  printWidth: 80, 
  tabWidth: 2,
};
Edgaras Karka
  • 7,400
  • 15
  • 61
  • 115
  • 4
    Same problem here! All the linters and auto formatting options conflict with each other! I couldn’t get code without a semicolon be accepted without errors... I had to resort to just using vs code’s native auto formatting – Kokodoko May 28 '19 at 09:51

13 Answers13

44

As per this Kai Cataldo's comment on this GitHub issue:

ESLint's indent rule and Prettier's indentation styles do not match - they're completely separate implementations and are two different approaches to solving the same problem ("how do we enforce consistent indentation in a project").

Therefore, when using prettier, you'd better disable eslint's indent rule. It's guaranteed that they will clash.

Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114
24

This should fix it https://github.com/prettier/eslint-config-prettier

It disables rules in eslint that conflict with prettier.

npm install --save-dev eslint-config-prettier

in your .eslintrc.* file put last "prettier" to extends section

"extends": [
         //"some-other-config-you-use",
         "prettier"
      ],
Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
Sam Banks
  • 369
  • 3
  • 8
16

in eslintrc add indent: [2, 2, { SwitchCase: 1}]

Parameters defined

  1. new eslint rules want the first parameter to be a number: Severity should be one of the following: 0 = off, 1 = warn, 2 = error.

  2. the amount of indent

  3. The object is stating how to indent switch and case statements following the options here.

clay
  • 5,917
  • 2
  • 23
  • 21
Cocuba
  • 363
  • 5
  • 12
10

I ran into the same issue. Thing is you can just manually override any clashing rules. In my case it was the prettier/prettier plugin for ESLint, so that can be solved adding the indent rule, under the required plugin.

"rules": {
        // "indent":["error",10]
        "prettier/prettier":[  //or whatever plugin that is causing the clash
            "error",
            {
                "tabWidth":4
            }
        ]
    }

You can override specific rules like this, to get rid of any clashes.

sayandcode
  • 1,775
  • 1
  • 11
  • 24
9

Turning off default Visual Studio Code parser and just leaving the eslint parser on save fixed it for me.

Just go to settings Ctrl/Cmd + ,, choose User (global settings) or Workspace (only for the working repo) and on top right corner click the paper with a turning arrow. That will open the declared settings on a json file. With the following settings it should work on any case:

{
  // other settings
  "editor.formatOnSave": false,
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true,
    "source.organizeImports": false
  },
  // other settings
}

Normally at the bottom of the Visual Studio Code window you have a Fix on save: X flag. That should be linked with this setting so make sure to leave it consistent.

  • 1
    this doesn't answer the question which is about vim and not VSCode – mikemols Dec 01 '20 at 13:39
  • This was my issue. Even though I had `indent: 0` in my eslint config and was using eslint-config-prettier, I was getting conflicting formatting with ternary operators in JSX. I had no idea that VS Code had its own parser on top of eslint. – Manath Feb 19 '21 at 15:44
4
  1. eslint-config-prettier will disable all ESLint formatting rules that may conflict with Prettier's rules.

    npm i --save-dev eslint-config-prettier
  2. eslint-plugin-prettier is the plugin that will add Prettier's formatting rules.
  3. Let's tell ESLint we'll use Prettier's recommended configuration:
    npm i --save-dev eslint-plugin-prettier
//eslintrc.js
{
  "eslintConfig": {
  "extends": [
    "react-app",
    "react-app/jest",
    "plugin:prettier/recommended"
  ]
}
Akshay Vijay Jain
  • 13,461
  • 8
  • 60
  • 73
3

Most annoying thing.. so fixed with: eslint-config-prettier

{
  "rules": {
    "no-tabs": ["error", {"allowIndentationTabs": true}]
  }
}
Pastuh
  • 376
  • 2
  • 12
2

It seems on my end, the only conflict is with ternary operations. Another option to fix the issue is to use the following eslint rule.

"indent": ["error", 2, { "offsetTernaryExpressions": true }],

There are a lot more options here: https://eslint.org/docs/latest/rules/indent#offsetternaryexpressions

I find eslint's indent rules more configurable and would use them over prettier.

lance
  • 21
  • 2
1

In my case what I did was I removed the indentation rule from the .eslintrc file and added useTabs: true to my .prettierrc file.

enter image description here

After reloading the VS Code it works perfectly with the tab size indentation.

Mr. Kabir
  • 695
  • 8
  • 9
0

I had this issue and by changing Prettier to Use Tabs in the settings menu (ctrl + shift + P), it fixed the issue for me.

Mike
  • 87
  • 1
  • 2
  • 10
  • 1
    Welcome to StackExchange, and thanks for posting an answer. However, it would be much better if you actually explain steps. Your one sentence here is not enough to help me fix my problem. I'm confused about what you're saying. – Ryan May 13 '21 at 15:23
  • This is a simple single settings change, there is but one step. Since I've been on StackExchange for 8+ years, I find your comment a bit condescending. – Mike May 13 '21 at 19:03
  • My apologies. I wasn't trying to be condescending. – Ryan May 13 '21 at 20:39
0

In my case I just changed CRLF (Carriage Return, Line Feed) to LF (Line Feed) on VSCode

Hermanyo H
  • 134
  • 3
0

If you are using VS-Code and configured the eslint and prettier settings as per the others answers here, then also check this option in VS-Code ;)

Change it to 'Tab' instead.

VS-Code Tab Options

Stephane
  • 11,056
  • 9
  • 41
  • 51
0

So, npm i --save-dev eslint-config-prettier fixed the Issue as pointed out by Akshay