预期换行符为 'LF' 但发现 'CRLF' 换行符样式

Expected linebreaks to be 'LF' but found 'CRLF' linebreak-style

在 gulp 项目中使用 eslint 时遇到了类似这样的错误
Expected linebreaks to be 'LF' but found 'CRLF' linebreak-style 我正在为 运行 gulp 使用 Windows 环境,下面给出了整个错误日志

 Kiran (master *) Lesson 4 $ gulp
 Using gulpfile c:\Users\Sai\Desktop\web-build-tools\
 gulpfile.js
 Starting 'styles'...
 Finished 'styles' after 17 ms
 Starting 'lint'...
 'lint' errored after 1.14 s
 ESLintError in plugin 'gulp-eslint'
 sage: Expected linebreaks to be 'LF' but found 'CRLF'.
 ails: fileName: c:\Users\Sai\Desktop\web-build-tools\js\extra.js



$>Users\Sai\Desktop\web-build-tools\js\extra.js
error  Expected linebreaks to be 'LF' but found 'CRLF'  linebreak-style

我还包括 extra.js 文件作为指示可能错误的错误。

function getWindowHeight() {
    return window.innerHeight;
}

getWindowHeight();

检查您的 .eslintrc 或源代码中是否有如下配置的 linebreak-style 规则:

/*eslint linebreak-style: ["error", "unix"]*/

由于您正在处理 Windows,您可能希望改用此规则:

/*eslint linebreak-style: ["error", "windows"]*/

参考linebreak-styledocumentation:

When developing with a lot of people all having different editors, VCS applications and operating systems it may occur that different line endings are written by either of the mentioned (might especially happen when using the windows and mac versions of SourceTree together).

The linebreaks (new lines) used in windows operating system are usually carriage returns (CR) followed by a line feed (LF) making it a carriage return line feed (CRLF) whereas Linux and Unix use a simple line feed (LF). The corresponding control sequences are "\n" (for LF) and "\r\n" for (CRLF).

这是一条可自动修复的规则。命令行上的 --fix 选项会自动修复此规则报告的问题。

但是,如果您希望在代码中保留 CRLF 行尾(因为您正在处理 Windows),请不要使用 fix 选项。

我发现根据这个答案使用换行样式在 .eslintrc 中忽略它们很有用(我想忽略换行而不更改任何文件):

module.exports = {
  extends: 'google',
  quotes: [2, 'single'],
  globals: {
    SwaggerEditor: false
  },
  env: {
    browser: true
  },
  rules:{
    "linebreak-style": 0
  }
};

如果您正在使用 vscode 并且您使用的是 Windows 我建议您 单击底部的选项- window 的右侧并将其从 CRLF 设置为 LF。因为我们不应该仅仅为了消除 Windows

上的错误而关闭配置

如果您没有看到 LF / CLRF,请右键单击状态栏和 select 编辑器行尾。

发生在我​​身上,因为我 运行 git config core.autocrlf true 而我忘记了返回。

在那之后,当我 checkout/pull 新代码时,所有 LF(Unix 中的断行)都被 CRLF(Windows 中的断行)所取代。

我运行 linter,所有报错信息都是Expected linebreaks to be 'LF' but found 'CRLF'

为了解决这个问题,我检查了 运行 git config --list | grep autocrlfautocrlf 值,我得到:

core.autocrlf=true
core.autocrlf=false

我编辑了全局 GIT 配置 ~/.gitconfig 并将 autocrlf = true 替换为 autocrlf = false

之后,我转到我的项目并执行以下操作(假设代码在 src/ 文件夹中):

CURRENT_BRANCH=$(git branch | grep \* | cut -d ' ' -f2);
rm -rf src/*
git checkout $CURRENT_BRANCH src/

刚刚在 .gitconfig 文件 false 中创建了 autocrlf 参数并重新克隆了代码。成功了!

[core] autocrlf = false

如果您使用的是 vscode,我建议您单击 window 右下角的选项并将其从 CRLF 设置为 LF。这修复了我的错误

如果您正在使用 WebStorm 并且您在 Windows 我建议您点击 settings/editor/code style/general 选项卡和 select “windows(\r\n) 下拉菜单 menu.These 步骤也将适用于 Rider。

如果你想在 crlf (Windows Eol) 中使用它,请转到文件 -> 首选项 -> 设置。在“用户”选项卡中键入 "end of line" 并确保 Files: Eol 设置为 \r\n 并且如果您正在使用Prettier 扩展,确保 Prettier: End of Line 设置为 crlf Finally, on your eslintrc file, add this rule: 'linebreak-style': ['error', 'windows']

我在eslint中使用VSCode时出现了同样的情况。如果你使用 VSCode,

1 - 单击 VScode.

右下角名称可以是 LF 或 CRLF 的区域

2 - Select 下拉菜单中的 LF。

这对我有用。

这是处理此错误的一个非常好的方法。您可以将以下行放入 .eslintrc.js 文件。

根据操作系统,将采用适当的行结尾。

rules: {
        'linebreak-style': ['error', process.platform === 'win32' ? 'windows' : 'unix'],
 }

在 .eslintrc 文件中添加我们的规则 'linebreak-style':0 in Vue js

  rules: {
    'linebreak-style':0,
  }

尝试使用 linter 的 --fix 标志来解决问题。

eslint filePath --fix

git 配置 core.autocrlf 错误

git rm --cached -r .

git 重置 --hard

就我而言(vue.js 项目,使用 vue-cli 创建)lint 问题 Expected linebreaks to be 'LF' but found 'CRLF' linebreak-style 与 Prettier 有关。 从版本 >= 2 Prettier 开始,将所有行尾替换为“lf”:https://prettier.io/docs/en/options.html#end-of-line

由于我使用 Windows 进行开发,因此我设置了这 3 个(git、eslint、prettier)配置以避免行结束问题:

  1. Git:我设置git config --global core.autocrlf true
  2. eslint: 在 .eslintrc.js 文件中我配置了:
      module.exports = {
      rules: {
        'linebreak-style': ['error', 'windows'],
      },
    };
  1. 更漂亮:最后在prettier.config.js
module.exports = {
    endOfLine: "crlf",
};

提示:确保您已经安装了 Git,如果没有先安装的话。 您可以将其他功能设为默认,您可以选择visual studio代码作为默认编辑器。以后对你有很大帮助。

Windows 用户: 卸载 Visual Studio 代码然后再次重新安装,EditorConfig 应该可以正常工作。

注意 => 卸载 Visual Studio 代码仍然保留旧设置和扩展!完全删除 Visual Studio Windows 上的代码

已卸载Visual Studio

  1. This PC > Local disk (C) Users > CurrentUser > AppData > Local > Programs > Microsoft VS Code
    1. Unins000.exe 或从控制面板卸载它
  2. 这台电脑 > 本地盘 (C) 用户 > 当前用户 > AppData > 本地 > 漫游
    1. 代码 => 文件夹应该被删除
  3. 这台电脑 > 本地盘 (C) 用户 > 当前用户 >
    1. .vscode => 文件夹应该被删除
  4. 重新安装 vs code 它现在应该可以工作了。

我只想添加更简单的方法。在 VScode 的右下角,单击 LF 或 CRLF 在它们之间切换。 See Photo

我将 linebreak-style 设置为 0,但不起作用。

rules: {
    "linebreak-style": 0,
 },

然后我发现需要将每个文件从“CRLF”更改为“LF”,这需要很多时间。

所以这个帮助

git config core.autocrlf false 
git rm --cached -r . 
git reset --hard