10
votes

I have a project that has both prettier and eslint installed. The problem is that when I save a file eslint automatically changes the format of the file and it seems some rules conflict with prettier. What is the solution?

This is prettier formatting: enter image description here

When saved, the file changes to: enter image description here

Also this is eslintrc file

{
  "parser": "@typescript-eslint/parser",
  "extends": [
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:prettier/recommended",
    "prettier/@typescript-eslint"
  ],
  "rules": {
    "@typescript-eslint/explicit-function-return-type": "off",
    "@typescript-eslint/no-explicit-any": "off",
    "@typescript-eslint/ban-ts-ignore": "off",
    "@typescript-eslint/interface-name-prefix": "off",
    "@typescript-eslint/no-unused-vars": ["warn", { "argsIgnorePattern": "^_" }]
  },
  "overrides": [
    {
      "files": ["**/*.tsx"],
      "rules": {
        "react/prop-types": "off"
      }
    }
  ],
  "settings": {
    "react": {
      "version": "detect"
    }
  },
  "ignorePatterns": ["coverage/", "node_modules/", "src/serviceWorker.ts"]
}
3

3 Answers

8
votes

Not fully configuring ESlint and Prettier can cause a myriad of issues. To avoid all of them follow the steps mentioned in How to use Prettier with ESLint and TypeScript in VSCode , also remove any extra setting in config files for ESlint, Prettier and setting.json for VScod that might override other rules.

Important: Based on the answer here: Uninstall prettier-eslint extension.

0
votes

One workaround could be to concatenate strings with the addition operator (+) instead of using template strings:

try {
  const response = await api.get(
    API_BOARDS +
      boardSearchParam.boardId +
      `?assignees=${boardSearchParam.assigneeIds.toString()}/`
  );
} catch (err) {
  // ...
}

Depending on your ESLint rules, you might get an error saying that you should use template strings instead of the addition operator. In that case, you can create a variable and use the addition assignment operator (+=):

try {
  let url = API_BOARDS + boardSearchParam.boardId;
  url += `?assignees=${boardSearchParam.assigneeIds.toString()}/`

  const response = await api.get(url);
} catch (err) {
  // ...
}

It's a matter of taste, but I think this is even a bit nicer to read.

0
votes

.prettierrc

{
  "printWidth": 160
}