Expected Linebreaks to Be 'Lf' But Found 'Crlf' Linebreak-Style

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

Check if you have the linebreak-style rule configure as below either in your .eslintrc or in source code:

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

Since you're working on Windows, you may want to use this rule instead:

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

Refer to the documentation of 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).

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).

This is a rule that is automatically fixable. The --fix option on the command line automatically fixes problems reported by this rule.

But if you wish to retain CRLF line-endings in your code (as you're working on Windows) do not use the fix option.

Expected linebreaks to be 'LF' but found CRLF

Note - Make sure you don't have any uncommitted changes else it will be deleted when you run the following cmd!

Run this in your terminal or cmd prompt

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

This usually happens when the project is created on UNIX based system & then used on a windows system because both have different line endings.

We need to disable auto CRLF on git & uncommit & recommit changes.

Reference

(linebreak-style) Expected linebreaks to be 'LF' but found 'CRLF'. (eslint)

The main solution was that Windows by default using CRLF, like my WebStorm.

1)first step is to change on WebStorm default encoding like there:

https://www.jetbrains.com/phpstorm/help/configuring-line-separators.html

2)And change it on Status Bar

https://www.jetbrains.com/phpstorm/help/status-bar.html

Save it! and

OKAY No lint warnings.

How can I write a ESLint rule for "linebreak-style", changing depending on Windows or Unix?

The eslint configuration file can be a regular .js file (ie, not JSON, but full JS with logic) that exports the configuration object.

That means you could change the configuration of the linebreak-style rule depending on your current environment (or any other JS logic you can think of).

For example, to use a different linebreak-style configuration when your node environment is 'prod':

module.exports = {
"root": true,
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 6
},
"rules": {
// windows linebreaks when not in production environment
"linebreak-style": ["error", process.env.NODE_ENV === 'prod' ? "unix" : "windows"]
}
};

Example usage:

$ NODE_ENV=prod node_modules/.bin/eslint src/test.js

src/test.js
1:25 error Expected linebreaks to be 'CRLF' but found 'LF' linebreak-style
2:30 error Expected linebreaks to be 'CRLF' but found 'LF' linebreak-style
3:36 error Expected linebreaks to be 'CRLF' but found 'LF' linebreak-style
4:26 error Expected linebreaks to be 'CRLF' but found 'LF' linebreak-style
5:17 error Expected linebreaks to be 'CRLF' but found 'LF' linebreak-style
6:50 error Expected linebreaks to be 'CRLF' but found 'LF' linebreak-style
7:62 error Expected linebreaks to be 'CRLF' but found 'LF' linebreak-style
8:21 error Expected linebreaks to be 'CRLF' but found 'LF' linebreak-style

✖ 8 problems (8 errors, 0 warnings)

$ NODE_ENV=dev node_modules/.bin/eslint src/test.js
$ # no errors

Expected linebreaks to be 'LF' but found 'CRLF' but in only one file

Was that file created on a different machine?

Personally I would just run eslint with —fix flag and not worry about it.



Related Topics



Leave a reply



Submit