简体   繁体   English

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

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

When using eslint in the gulp project i have encountered a problem with error like this在 gulp 项目中使用 eslint 时,我遇到了这样的错误问题
Expected linebreaks to be 'LF' but found 'CRLF' linebreak-style and I am using Windows environment for the running gulp and the entire error log is given below Expected linebreaks to be 'LF' but found 'CRLF' linebreak-style ,我正在使用 Windows 环境运行 gulp,下面给出了整个错误日志

 Kiran (master *) Lesson 4 $ gulp
 Using gulpfile c:\Users\Sai\Desktop\web-build-tools\4\
 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\4\js\extra.js



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

I have also including extra.js file as the error indicating possible mistake.我还包括extra.js文件作为指示可能错误的错误。

function getWindowHeight() {
    return window.innerHeight;
}

getWindowHeight();

Check if you have the linebreak-style rule configure as below either in your .eslintrc or in source code:检查您的 .eslintrc 或源代码中是否有如下配置的换linebreak-style规则:

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

Since you're working on Windows, you may want to use this rule instead:由于您在 Windows 上工作,因此您可能希望改用此规则:

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

Refer to the documentation of linebreak-style :参考linebreak-style文档

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).当与很多人一起开发时,他们都拥有不同的编辑器、VCS 应用程序和操作系统,可能会出现由上述任何一个编写的不同行尾(尤其是在同时使用 Windows 和 Mac 版本的 SourceTree 时可能会发生)。

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). Windows 操作系统中使用的换行符(新行)通常是回车符(CR)后跟换行符(LF),使其成为回车换行符(CRLF),而 Linux 和 Unix 使用简单的换行符(LF)。 The corresponding control sequences are "\n" (for LF) and "\r\n" for (CRLF).对应的控制序列是"\n" (用于LF)和"\r\n"用于(CRLF)。

This is a rule that is automatically fixable.这是一个可以自动修复的规则。 The --fix option on the command line automatically fixes problems reported by this rule.命令行上的--fix选项会自动修复此规则报告的问题。

But if you wish to retain CRLF line-endings in your code (as you're working on Windows) do not use the fix option.但是,如果您希望在代码中保留CRLF行尾(因为您在 Windows 上工作),请不要使用fix选项。

I found it useful (where I wanted to ignore line feeds and not change any files) to ignore them in the .eslintrc using linebreak-style as per this answer: https://stackoverflow.com/a/43008668/1129108我发现它很有用(我想忽略换行符而不更改任何文件)在 .eslintrc 中使用换行样式忽略它们,按照这个答案: https ://stackoverflow.com/a/43008668/1129108

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

If you are using vscode and you are on Windows i would recommend you to click the option at the bottom-right of the window and set it to LF from CRLF .如果您使用的是 vscode 并且您在Windows上,我建议您单击窗口右下角的选项并将其设置为 LF 从 CRLF Because we should not turn off the configuration just for sake of removing errors on Windows因为我们不应该仅仅为了消除Windows上的错误而关闭配置

If you don't see LF / CLRF, then right click the status bar and select Editor End of Line.如果您没有看到 LF / CLRF,则右键单击状态栏并选择 Editor End of Line。

菜单

Here is a really good way to manage this error.这是管理此错误的一种非常好的方法。 You can put the below line in .eslintrc.js file.您可以将以下行放入 .eslintrc.js 文件中。

Based on the operating system, it will take appropriate line endings.根据操作系统,它将采用适当的行尾。

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

If you want it in crlf (Windows Eol), go to File -> Preferences -> Settings.如果您想在crlf (Windows Eol) 中使用它,请转到文件 -> 首选项 -> 设置。 Type "end of line" in the User tab and make sure Files: Eol is set to \r\n and if you're using the Prettier extension, make sure Prettier: End of Line is set to crlf .在“用户”选项卡中键入“行尾”并确保将Files: Eol设置为\r\n ,如果您使用的是 Prettier 扩展,请确保Prettier: End of Line设置为crlf 在此处输入图像描述 Finally, on your eslintrc file, add this rule: 'linebreak-style': ['error', 'windows']最后,在您的 eslintrc 文件中,添加以下规则: 'linebreak-style': ['error', 'windows'] 在此处输入图像描述

add to our rule in the .eslintrc file 'linebreak-style':0 in Vue js在 Vue js 中的 .eslintrc 文件 'linebreak-style':0 中添加我们的规则

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

在此处输入图像描述

Just made autocrlf param in .gitconfig file false and recloned the code.刚刚将autocrlf文件中的 autocrlf 参数设置为false并重新克隆了代码。 It worked!有效!

[core] autocrlf = false

The same situation occurred when I was using VSCode with eslint.当我将 VSCode 与 eslint 一起使用时,也发生了同样的情况。 If you use VSCode,如果你使用 VSCode,

1 - Click area that name can be both LF or CRLF where at the bottom right of the VScode. 1 - 单击名称可以是 LF 或 CRLF 的区域,位于 VScode 的右下角。

2 - Select LF from the drop-down menu. 2 - 从下拉菜单中选择 LF。

That's worked for me.这对我有用。

在此处输入图像描述

Happen with me because I ran git config core.autocrlf true and I forgot to rever back.发生在我身上,因为我运行了git config core.autocrlf true并且我忘记了返回。

After that, when I checkout/pull new code, all LF (break line in Unix) was replaced by CRLF (Break line in Windows).之后,当我签出/提取新代码时,所有 LF(Unix 中的换行符)都被 CRLF(Windows 中的换行符)替换。

I ran linter, and all error messages are Expected linebreaks to be 'LF' but found 'CRLF'我跑了 linter,所有错误消息都是Expected linebreaks to be 'LF' but found 'CRLF'

To fix the issue, I checked autocrlf value by running git config --list | grep autocrlf为了解决这个问题,我通过运行git config --list | grep autocrlf检查了autocrlf值。 git config --list | grep autocrlf and I got: git config --list | grep autocrlf我得到了:

core.autocrlf=true
core.autocrlf=false

I edited the global GIT config ~/.gitconfig and replaced autocrlf = true by autocrlf = false .我编辑了全局 GIT 配置~/.gitconfig并将autocrlf = true替换为autocrlf = false

After that, I went to my project and do the following (assuming the code in src/ folder):之后,我进入我的项目并执行以下操作(假设src/文件夹中的代码):

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

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

git config core.autocrlf false git config core.autocrlf 假

git rm --cached -r . git rm --cached -r 。

git reset --hard git reset --hard

If you are using WebStorm and you are on Windows i would recommend you to click settings/editor/code style/general tab and select "windows(\r\n) from the dropdown menu.These steps will also apply for Rider.如果您使用的是WebStorm并且在Windows上,我建议您单击设置/编辑器/代码样式/常规选项卡,然后从下拉菜单中选择“windows(\r\n)。这些步骤也适用于 Rider。

在此处输入图像描述

In my case (vue.js project, created using vue-cli) the lint problem Expected linebreaks to be 'LF' but found 'CRLF' linebreak-style was related to Prettier.就我而言(vue.js 项目,使用 vue-cli 创建),lint 问题Expected linebreaks to be 'LF' but found 'CRLF' linebreak-style与 Prettier 有关。 From version >= 2 Prettier replace all line endings with "lf": https://prettier.io/docs/en/options.html#end-of-line从版本 >= 2 Prettier 用“lf”替换所有行尾: https ://prettier.io/docs/en/options.html#end-of-line

Since I'm using Windows to develop I set these 3 (git, eslint, prettier) configuration to avoid line endings problems:由于我使用 Windows 进行开发,因此我设置了这 3 个(git、eslint、prettier)配置以避免行尾问题:

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

Tips: Make sure you have installed Git as the picture if not do that first.提示:如果没有先安装,请确保您已经安装了 Git You can take other features as default, You can choose visual studio code as the default editor .您可以将其他功能作为默认设置,您可以选择 Visual Studio 代码作为默认编辑器 helps you a lot later.以后对你有很大帮助。

在此处输入图像描述

Windows Users: Uninstall Visual Studio Code then reinstalled it again and EditorConfig should work just fine. Windows 用户:卸载 Visual Studio Code 然后重新安装它,EditorConfig 应该可以正常工作。

NOTE => Uninstalling Visual Studio Code still leaves the old settings and extensions!注意 => 卸载 Visual Studio Code 仍然保留旧设置和扩展! remove Visual Studio Code on Windows completely完全删除 Windows 上的 Visual Studio Code

Uninstalled Visual Studio卸载的 Visual Studio

  1. This PC > Local disck (C) Users > CurrentUser > AppData > Local > Programs > Microsoft VS Code这台电脑 > 本地磁盘 (C) 用户 > CurrentUser > AppData > 本地 > 程序 > Microsoft VS Code
    1. Unins000.exe or Uninstall it from conrol panel Unins000.exe 或从控制面板卸载它
  2. This PC > Local disck (C) Users > CurrentUser > AppData > Local > Roaming此 PC > 本地磁盘 (C) 用户 > CurrentUser > AppData > Local > Roaming
    1. Code => Folder should be delete it代码=> 文件夹应该被删除
  3. This PC > Local disck (C) Users > CurrentUser >这台电脑 > 本地磁盘 (C) 用户 > 当前用户 >
    1. .vscode => Folder should be delete it .vscode => 文件夹应该被删除
  4. reinstall vs code it should work now.重新安装 vs 代码它现在应该可以工作了。

Try using the linter's --fix flag to resolve the issue.尝试使用 linter 的 --fix 标志来解决问题。

eslint filePath --fix eslint 文件路径 --fix

I just want to add the easier way.我只想添加更简单的方法。 On the bottom right of VScode click the LF or CRLF to toggle between them.在 VScode 的右下角,单击 LF 或 CRLF 在它们之间切换。 See Photo见照片

I set linebreak-style to 0, but not working.我将换行样式设置为 0,但不起作用。

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

so then I found out need to change every files from "CRLF" to "LF", and that take lots time.所以后来我发现需要将每个文件从“CRLF”更改为“LF”,这需要很多时间。

so this help所以这个帮助

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

The "endOfLine": "auto" setting in .eslintrc.js and .prettierrc files worked for me. .eslintrc.js.prettierrc文件中的"endOfLine": "auto"设置对我有用。

File .eslintrc.js should have:文件.eslintrc.js应该有:

rules: {
    /*  ...the other code is omitted for the brevity */
    'arrow-body-style': [1, 'as-needed'],
    'import/extensions': 'off',
    'prettier/prettier': [
        'error',
        {
            semi: false,
            singleQuote: true,
            useTabs: true,
            endOfLine: 'auto', /* this setting should be included */
        },
    ],

File .prettierrc should have:文件.prettierrc应该有:

{
    "semi": false,
    "trailingComma": "all",
    "singleQuote": true,
    "useTabs": true,
    "tabWidth": 2,
    "endOfLine": "auto" /* this setting should be included */
}

对我来说,我通过在 .prettierrc 文件中设置 "endOfLine": "lf" 解决了这个问题。

In case you want to change to LF and you are using VS Code you can do it per file like so:如果你想更改为LF并且你正在使用 VS 代码,你可以像这样针对每个文件执行此操作:

显示如何使用 VS CODE 从 CRLF 更改为 LF 的图形

In my case, the LF rule was set in a file read by the maven-checkstyle-plugin in pom.xml .在我的例子中, LF rule是在pom.xml中的maven-checkstyle-plugin读取的文件中设置的。 The ${skipCheckstyle} environment variable was not set.未设置${skipCheckstyle}环境变量。 This way I skipped the whole check.这样我就跳过了整个检查。

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
    <skip>${skipCheckstyle}</skip>
    <configLocation>${top.dir}/src/main/config/checkstyle/checker.xml</configLocation>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 预期换行符为 &#39;LF&#39; 但找到 CRLF - Expected linebreaks to be 'LF' but found CRLF JS(预期换行符为“LF”但发现“CRLF”.eslintlinebreak-style)问题 - JS (Expected linebreaks to be 'LF' but found 'CRLF'.eslintlinebreak-style) issue 预期换行符为“LF”,但发现“CRLF”但仅在一个文件中 - Expected linebreaks to be 'LF' but found 'CRLF' but in only one file 在ESlint中强制执行“换行样式”规则有多重要? 我可以关掉它吗? - How important is to enforce the “linebreak-style” rule in ESlint? Can I just turn it off? 我如何为“linebreak-style”编写 ESLint 规则,根据 Windows 或 Unix 进行更改? - How can I write a ESLint rule for "linebreak-style", changing depending on Windows or Unix? 字符串中的CR,LF和CRLF字符 - CR, LF and CRLF characters in a String 如何用换行符替换值并在 b-modal 中显示(带换行符) - How to replace value with linebreak and show it in b-modal (with linebreaks) 为什么jquery.serialize将LF更改为CRLF? - Why does jquery.serialize change LF to CRLF? LF或CRLF的Visual Studio代码安装eslint错误 - visual studio code install eslint error for LF or CRLF 为什么 LF 和 CRLF 与 /^\\s*$/gm 正则表达式的行为不同? - Why does LF and CRLF behave differently with /^\s*$/gm regex?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM