Videos
» npm install eslint-plugin-prettier
I've heard ESLint can format but I haven't found a clear answer why it seems prettier is used instead of the ESLint formatter. Whenever I try to look it up most comments neglect to mention that ESLint can also format so it's not obvious why prettier would be needed at all.
» npm install eslint-config-prettier
UPDATE 2023: ESLint is deprecating formatting rules and recommend you use a source code formatter instead.
tl;dr: Use eslint-config-prettier in eslint, and run prettier separately. You can ignore the rest.
From v8.53.0 onwards, you will see a deprecation warning if those formatting rules are enabled in your config. You should still use eslint-config-prettier to disable conflicting rules until the rules are removed in a new major release.
ESLint contains many rules and those that are formatting-related might conflict with Prettier, such as arrow-parens, space-before-function-paren, etc. Hence using them together will cause some issues. The following tools have been created to use ESLint and Prettier together.
prettier-eslint |
eslint-plugin-prettier |
eslint-config-prettier |
|
|---|---|---|---|
| What it is | A JavaScript module exporting a single function. | An ESLint plugin. | An ESLint configuration. |
| What it does | Runs the code (string) through prettier then eslint --fix. The output is also a string. |
Plugins usually contain implementations for additional rules that ESLint will check for. This plugin uses Prettier under the hood and will raise ESLint errors when your code differs from Prettier's expected output. | This config turns off formatting-related rules that might conflict with Prettier, allowing you to use Prettier with other ESLint configs like eslint-config-airbnb. |
| How to use it | Either calling the function in your code or via prettier-eslint-cli if you prefer the command line. |
Add it to your .eslintrc. |
Add it to your .eslintrc. |
| Is the final output Prettier compliant? | Depends on your ESLint config | Yes | Yes |
Do you need to run prettier command separately? |
No | No | Yes |
| Do you need to use anything else? | No | You may want to turn off conflicting rules using eslint-config-prettier. |
No |
For more information, refer to the official Prettier docs.
It's the recommended practice to let Prettier handle formatting and ESLint for non-formatting issues, prettier-eslint is not in the same direction as that practice, hence prettier-eslint is not recommended anymore. You can use eslint-plugin-prettier and eslint-config-prettier together.
- Use
eslint-config-prettierto turn-off eslint rules that are unnecessary or might conflict with Prettier. See 1st line in readme: eslint-config-prettier. - Use
eslint-plugin-prettierto run Prettier as an Eslint-rule. See 1st line in readme: eslint-plugin-prettier - Use both to take advantage of both tools. See recommended configuration: eslint-plugin-prettier.
This way you use
pluginto run Prettier as an Eslint-rule, andconfigto turn-off eslint rules that are unnecessary or might conflict with Prettier. - You can ignore
prettier-eslint
To solve conflict
install eslint configuration for prettier
npm install eslint-config-prettier
And include it in the extends option in the file .eslintrc.js
extends: [
...,
"prettier",
],
This is potentially because of conflicting rules between ESLint and Prettier plugins. Now you have two options
- Either let ESLint know that you want to use Prettier as a formatter.
https://dev.to/s2engineers/how-to-make-eslint-work-with-prettier-avoiding-conflicts-and-problems-57pi
2.You can configure ESlint and Prettier together and resolve the conflicting rules without any conflicts. https://blog.theodo.com/2019/08/empower-your-dev-environment-with-eslint-prettier-and-editorconfig-with-no-conflicts/
For some rules you mentioned, namely indent or new lines, it doesn't matter, both ESlint and prettier lead to the same result. However max-len and printWidth work significantly different. max-len will just count the number of characters and throws an error if it's exceeded, but the error needs to be manually fixed.
According to the prettier docs, here is how the printWidth option works in contrast:
Prettier’s printWidth option does not work the same way. It is not the hard upper allowed line length limit. It is a way to say to Prettier roughly how long you’d like lines to be. Prettier will make both shorter and longer lines, but generally strive to meet the specified printWidth.
Additionally, prettier automatically fixes the line length of your lines. For example, see the following code:
function myVeryCoolFunctionWithLongName(myFirstFunctionArgument, mySecondFunctionArgument, myThirdFunctionArgument) {
}
With default prettier settings, it will be changed to:
function myVeryCoolFunctionWithLongName(
myFirstFunctionArgument,
mySecondFunctionArgument,
myThirdFunctionArgument,
) {}
Whereas ESLint would just throw an error.
To summarize, while ESLint does a decent job in detecting formatting issues, prettier is much smarter in fixing your code to meet a consistent format. Read very closely again the sentence you cited:
ESLint isn't intended to perform code style fixes automatically
Further reference:
- ESLint froze stylistic rules as of May 2020
- TypeScript ESLint encourages using prettier for formatting
As prettier docs mentioned:
Linters have two categories of rules:
Formatting rules: eg: max-len, no-mixed-spaces-and-tabs, keyword-spacing, comma-style…
Prettier alleviates the need for this whole category of rules! Prettier is going to reprint the entire program from scratch in a consistent way, so it’s not possible for the programmer to make a mistake there anymore :)
Code-quality rules: eg no-unused-vars, no-extra-bind, no-implicit-globals, prefer-promise-reject-errors…
Prettier does nothing to help with those kind of rules. They are also the most important ones provided by linters as they are likely to catch real bugs with your code!
In other words, use Prettier for formatting and linters for catching bugs!
In fact, prettier will do it faster and more predictable because it is designed to do so. If you use ESLint for formatting, it would be like using a 5 pound hammer to drive a nail.
Use the right tool for the right job
I highly recommend you watch the video of Theo on this subject.
See more:
- Josh Goldberg - Setting Up ESLint and TypeScript for React
- Integrating with Linters
Instead of disabling linting for the file, you can instead disable prettier within the eslintrc.js config file:
module.exports = {
root: true,
extends: '@react-native-community',
rules: {
'prettier/prettier': 0,
},
};
To get rid of conflicting rules when using both - prettier and eslint there is a eslint-config-prettier package.
Run npm install --save-dev eslint-config-prettier to install and then in eslintrc.js (or wherever you have the eslint rules defined) add:
{
"extends": [
...,
"@react-native-community",
"prettier"
]
}
Now eslint should respect prettier rules. Here is a link to GH repo.