preface
When I recently studied the front end, I used esLint and Prettier in my project. As a result, the two tools were in conflict with each other and I only took study notes and wrote them down after consulting some materials.
Cause of conflict
The eslint and prettier plugins of vscode were used in the project and the automatic formatting and fixing of code when saving was enabled:
// .vscode/setting.json
{
// When saving, prettier automatically formats
"editor.formatOnSave": true.// Automatically enable ESlint when saving --fix Automatically fixes
"editor.codeActionsOnSave": {
"source.fixAll": true}}// .eslintrc
{
"env": {
"browser": true."es2021": true."node": true
},
"extends": ["eslint:recommended"."standard"]."parserOptions": {
"ecmaVersion": 12."sourceType": "module"
},
"plugins": ["import"."node"."promise"]."rules": {}}// .prettierrc
{
// Use double quotes
"singleQuote": false.// Add a semicolon to the end of each line
"semi": true
}
Copy the code
The following error message is displayed:
The reason I’m showing the error here is because I inherited it in ESLinteslint-config-standard
This set of rules recommends the use of single quotes and no semicolons at the end of lines, which is different from mineprettier
Configurations conflict. Ideally, because it’s onfixAll:true
, theneslint --fix
It should start automatically when saving, which should have been fixed, but the screen flashes back to the same state. After looking through the data, I found that becauseeslint
The rules andprettier
Conflict, save enabled firsteslint --fix
Fixed it, and it’s up and running againprettier
Formatting, so the screen flashes back to this state.
The solution
The nature of the conflict is that ESLint is responsible for both code quality inspection and part of formatting beautification, where some of the formatting rules are incompatible with Prettier. Could esLint only check code quality and Prettier only prettier? Fortunately, the community has a very mature solution: eslint-config-prettier + eslint-plugin-prettier.
eslint-config-prettier
The function is to turn offeslint
And in theprettier
Conflicting rules.eslint-plugin-prettier
The function of is to endoweslint
withprettier
The ability to format code.
Install dependencies and modify.eslintrc files
// Install dependencies
yarn add eslint-config-prettier eslint-plugin-prettier -D
// .eslintrc
{
// Rest of the configuration
- "extends": ["eslint:recommended"."standard"]
+ "extends": ["eslint:recommended"."standard"."plugin:prettier/recommended"]
// Rest of the configuration
}
Copy the code
The key lies in the increase of the plugin: prettier/it the rules. Let’s see what it does, right
// node_modules/eslint-plugin-prettier/eslint-plugin-prettier.js
module.exports = {
/ / the plugin: prettier/it is loading the
configs: {
recommended: {
extends: ['prettier'].plugins: ['prettier'].rules: {
'prettier/prettier': 'error'.'arrow-body-style': 'off'.'prefer-arrow-callback': 'off',}}},/ / the other
}
Copy the code
This grammar plugin: prettier/it is loaded the node_modules/eslint – plugin – prettier/eslint – plugin – prettier. Js file export of configs Recommended configuration items. Here’s a breakdown of what they did.
extends: ['prettier']
Through:eslint-config-prettier
Shut downeslint
andprettier
Conflicting rules.plugins: ['prettier']
Loading:eslint-plugin-prettier
To giveeslint
用prettier
The ability to format documents'prettier/prettier': 'error'
: let the code file do not conformprettier
Formatting rules are marked error, combinedvscode-eslint
The plugin will see these errors marked in red when runningeslint --fix
Command, these errors are automatically fixed.arrow-body-style
和prefer-arrow-callback
: These two rules are ineslint
和prettier
Closed because there was an unresolvable conflict in the.
Prettier and ESLint could now work together without conflict and automatically fix and format code when they saved, while Prettier and ESLint could also work together without conflict.
Matters needing attention
Prettierrc, eslint and prettier conflict again because vscode is not the cache for prettierrc, restart vscode
conclusion
Eslint checks code quality as well as formatting, causing formatting to conflict with Prettier. Because there is expertise, coding should be done by Prettier. Eslint-plugin-prettier + eslint-config-prettier solves conflicts perfectly and makes the two work together.
This article simply describes the causes and solutions of the conflict between EsLint and Prettier. When you have time, write the ultimate guide to ESLint + Prettier + Husky + Lint-staged + Commit-Lint to ensure project code quality.