这个问题是因为不同系统换行符不同导致的。
统一换行符,"\n" unix(for LF)
"\r\n" for windows(CRLF)
所以需要根据eslint的配置来设置,当然也可以改eslint配置。
'linebreak-style': ["error", "windows"], //默认unix
如果需要根据配置来,比如你不能把unix改成Windows,因为你改了window,别人unix,就会出问题,所以有时候,是不能改的。
那么你需要通过代码编辑器来设置。
vscode编辑器中,打开报错的文件,下面会出现蓝色的工具栏一样的东西,可以设置CRLF/LF格式。
结束。
但是这样设置有点麻烦,借助vscode的一个插件,可以做到CTRL+S保存一下文件,自动对文件进行LF或CRLF设置。
首先给vs code安装插件-EditorConfig for Visual Studio Code。
然后,在项目目录下增加一个相应的配置文件.editorconfig。
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
保存后自动保存为LF尾行序列,除此之外还有其他设置,具体见:http://editorconfig.org/
下面是VS code 编辑器 eslint设置,设置好,对那些不遵循规则的文件重新CTRL+S保存一下,绝大部分的不规则的地方,都自动帮你改好了,一些特别的需要手动修改。
设置:
文件-首选项-设置-右上角的'打开设置(json)'按钮,输入json:
{
// 保存自动修复
// "eslint.autoFixOnSave": true,
// "eslint.validate": [
// "javascript",
// "javascriptreact",
// { "language": "vue", "autoFix": true }
// ],
//autoFixedOnSave 设置已废弃,采用如下新的设置
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.format.enable": true,
//autoFix默认开启,只需输入字符串数组即可
"eslint.validate": ["javascript", "vue", "html"],
"editor.fontSize": 14,
"editor.tabSize": 2,
"editor.renderWhitespace": "all"
// "window.zoomLevel": 1,
// "editor.quickSuggestions": { "strings": true },
// // "prettier.eslintIntegration": true,
// "javascript.format.insertSpaceBeforeFunctionParenthesis": true,
// "vetur.format.defaultFormatter.html": "js-beautify-html",
// "vetur.format.defaultFormatter.js": "vscode-typescript",
// "terminal.integrated.shell.osx": "/bin/zsh",
// "wxmlConfig.format": {
// "brace_style": "collapse",
// "end_with_newline": false,
// "indent_char": "",
// "indent_handlebars": false,
// "indent_inner_html": false,
// "indent_scripts": "keep",
// "indent_size": 2,
// "indent_with_tabs": true,
// "max_preserve_newlines": 1,
// "wrap_attributes_count": 2,
// "unformatted": "['text']",
// "disable_automatic_closing_labels": false,
// "preserve_newlines": true,
// "wrap_attributes": "force-expand-multiline"
// },
// "files.associations": {
// "*.wpy": "vue",
// "*.wxml": "wxml",
// "*.less": "less",
// "*.cjson": "jsonc",
// "*.wxss": "css",
// "*.wxs": "javascript"
// },
// "terminal.integrated.shell.windows": "C:\\Users\\yuyingrong963\\AppData\\Local\\Programs\\Git\\bin\\bash.exe",
// "cssrem.rootFontSize": 50,
// "workbench.tree.indent": 4,
// "emmet.includeLanguages": {
// "wxml": "html"
// },
// "minapp-vscode.disableAutoConfig": true,
// "[javascript]": {
// "editor.defaultFormatter": "vscode.typescript-language-features"
// },
// "files.autoSave": "off",
// "[html]": {
// "editor.defaultFormatter": "esbenp.prettier-vscode"
// }
}
如果有某些代码不做eslint校验,可以做如下设置
/* eslint-disable */
if(window.AGENT){
}
/* eslint-enable */
或者:
let agent = window.AGENT // eslint-disable-line
2020-08-18 更新:
最近换电脑,以前的代码重新拉下来后,全部是LF的,而且现在文件几百个,一个个改太吓人了,后来才发现,git有设置,因为我的操作系统是windows的,拉代码的时候git自动把库里面的代码转换成CRLF结尾的,git可以设置不转换,这样拉下来的代码就还是LF的。
# 拒绝提交包含混合换行符的文件
git config --global core.safecrlf true
# 提交时转换为LF,检出时不转换
git config --global core.autocrlf input
执行后,再拉代码,就发现里面的文件全是LF的,不用一个一个转换了。
相关资料:
// 提交时验证
# 拒绝提交包含混合换行符的文件
git config --global core.safecrlf true
# 允许提交包含混合换行符的文件
git config --global core.safecrlf false
# 提交包含混合换行符的文件时给出警告
git config --global core.safecrlf warn
// 下拉提交时操作
# 提交时转换为LF,检出时转换为CRLF
git config --global core.autocrlf true
# 提交时转换为LF,检出时不转换
git config --global core.autocrlf input
# 提交检出均不转换
git config --global core.autocrlf false