The problem background
The following error occurs when executing pre-commit on Windows laptop:
The Delete ` ␍ ` eslint (prettier/prettier)Copy the code
Here are a few solutions that individuals have tried:
The solution
Crtl+S save files
Press Crtl+S to save the current error file, esLint error disappeared, but Git temporary area has a file change record, compared to the Working tree found no difference.
Disadvantages: you can’t save all the files one by one, trouble, but also commit, redundant.
Yarn run Lint –fix
Eslint errors disappear, but n more file changes are recorded in the temporary storage area, and there is no difference compared to Working Tree.
Disadvantages: Need to commit all files, redundant.
Reference: “error Delete ⏎ prettier/prettier” in. Vue files’ ‘
3, Configure the. Prettierrc file
Prettierrc in the. Prettierrc file in the root directory of the project. She doesn’t let Prettier check the format at the end of each line of a file.
"endOfLine": "auto"
Copy the code
Disadvantages: not compatible with cross-platform development, not perfect in terms of front-end engineering.
Why do I keep getting Delete ‘cr’ [prettier/prettier]?
Four, through the IDE one key switch
This is the method provided by the gold diger in the comments section. Generally, the status bar at the bottom of the IDE provides a tool to “switch the end of the line sequence”, as shown in the picture above, one click to switch to the correct end of the line sequence.
Disadvantages: Can only fix the current file, does not solve the entire project error.
Five, the best plan
Root cause:
The main culprit is a git configuration property called core. Autocrat lf
The newline characters of text files in Windows and Linux are inconsistent due to historical reasons.
* Windows uses both carriage return character (CR) and linefeed character (LF) * on Mac and Linux, Old Mac systems used the carriage return character CR insteadCopy the code
Windows | Linux/Mac | Old Mac(pre-OSX |
---|---|---|
CRLF | LF | CR |
‘\n\r’ | ‘\n’ | ‘\r’ |
As a result, text files are incompatible when created and used on different systems.
The default in my project repository is the code submitted under Linux environment, and the file ends in LF by default (engineering needs, unified standard).
The second step is to change the file’s trigger (for example, the second step is to change the trigger’s trigger (for example, the second step is to change the trigger’s trigger) (for example, the second step is to change the trigger’s trigger (for example, the second step is to change the trigger’s trigger) (for example, the second step is to change the trigger’s trigger (for example, the second step is to change the trigger’s trigger). Pre-commit EsLint will prompt you to delete CR.
You can now understand why the CTRL + S and YARN Run Lint –fix schemes can fix ESLint errors because Git automatically converts CRLF to LF.
Best practices:
VScode and Notepad++ editors can now automatically identify whether the file newline is LF or CRLF. If you are using Windows and the file encoding is UTF-8 and contains Chinese, change the triggering LF setting to false globally.
git config --global core.autocrlf false
Copy the code
Note: After git global configuration, you need to pull the code again.
conclusion
There are discussions and solutions on stackOverflow and Github, but none of them touch the soul. Make next this article, so that in the future to read, if to meet pit friend help, the author never tired!