Delete `␍`eslint(prettier/prettier) error solution

Delete '␍' eslint(prettier/prettier) error solution


problem background

WindowsAfter pulling the code on the notebook, when executing it , pre-committhe following error occurs:

Delete `␍`eslint(prettier/prettier)

wrong picture

Here are a few solutions I've personally tried:

solution

1. Crtl+S to save the file

Press Crtl+SSave the current error report file, eslintthe error disappears, but Gitthere is an additional file change record in the temporary storage area, and Working treeno difference is found in the comparison.
insert image description here

Disadvantages: You can't save all files one by one, which is troublesome and commitredundant.

二、yarn run lint --fix

It is easier than the above, and eslintthe error disappears, but there are more nfile change records in the temporary storage area, and Working treeno difference is found in the comparison.

Cons: Needs commitall files, redundant.

Reference: "error Delete ⏎ prettier/prettier" in .vue files ``

3. Configure the .prettierrc file

.prettierrcJust write in the file in the root directory of the project . In fact, it is prettierthe format that does not allow the detection of the end of each line of the file.

"endOfLine": "auto"

Disadvantages: It is not compatible with cross-platform development, and it is not perfect in terms of front-end engineering.

参考资料:Why do I keep getting Delete ‘cr’ [prettier/prettier]?

4. One-key switching through IDE

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-UQxZMB7g-1673344596978)(https://segmentfault.com/img/bVcPuZP “image”)]

This is the method provided by Nuggets in the comment area. Generally, the status bar below the IDE will provide a tool to "switch the line ending sequence". As shown in the figure above, you can switch to the correct line ending sequence with one click.

Disadvantage: It can only repair the current file, but cannot solve the problem of error reporting for the entire project.

Five, the best solution

Root cause of the problem:

The culprit is gita configuration property of:core.autocrlf

For historical reasons, the text files windowsbelow and below have inconsistent line breaks.linux

  • WindowsWhen changing the line, both carriage return CR(carriage-return character)and line feed are used LF(linefeed character)
  • And Macthe Linuxsystem, just use the newline characterLF
  • Older versions of Macthe system used the carriage return characterCR
Windows Linux/Mac Old Mac(pre-OSX)
CRLF LF CR
‘\n\r’ ‘\n’ ‘\r’

Therefore, there will be incompatibility problems when text files are created and used under different systems.

LinuxThe code submitted in the environment is the default in my project warehouse , and the file ends with LFthe default (engineering needs, unified standards).

When I use windowscomputer git clonecode, if mine autocrlf(installed windowsbelow git, this option defaults to true) true, then each line of the file will be automatically converted to CRLFend with , if no modification is made to the file, it will prompt you when pre-commitexecuting eslintdelete CR.

Now you can understand why ctrl+sand yarn run lint --fixthe program can fix eslintthe error, because it will be converted Gitautomatically .CRLFLF

Best Practices:

Now VScode, Notepad++the editor can automatically recognize whether the line break of the file is LF or CRLF.

If you are using windows, the file encoding is UTF-8and contains Chinese, it is best to autocrlfset it globally false.

git config --global core.autocrlf false

Note: gitAfter global configuration, you need to pull the code again.

Summarize

I searched a lot of information, stackoverflowand githubthere are corresponding discussions and solutions to this problem on website and online, but none of them can touch the soul. Write this article for future reading. If it is helpful to friends who are in trouble, the author will never tire of it!

Guess you like

Origin blog.csdn.net/zqd_java/article/details/128634137