Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prettier and eslint indents not working together

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, }; 
like image 630
Edgaras Karka Avatar asked May 28 '19 07:05

Edgaras Karka


People also ask

Can Prettier and ESLint work together?

Both configuration files for Prettier and ESLint can be adjusted to your needs. If you need to add rules, you can do it with both files. If you need to disable a rule coming from Airbnb's style guide, you can do it in the ESLint configuration.

Does Prettier conflict with ESLint?

Prettier runs as a plugin of ESLint and thanks to the special configuration it won't conflict with it.

What is the difference between ESLint and Prettier?

ESlint is not only a code formatter, it also helps developers to find coding errors. For Example, ESLint will give you a warning if you use a variable without declaring it. Prettier doesn't have such an ability. Also, ESLint will let you know what's wrong with your code formatting and give you options to fix the issue.


2 Answers

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.

like image 96
Paul Razvan Berg Avatar answered Sep 23 '22 02:09

Paul Razvan Berg


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.

like image 29
Cocuba Avatar answered Sep 23 '22 02:09

Cocuba