53

As we all know, the linebreaks (new line) used in Windows are usually carriage returns (CR) followed by a line feed (LF) i.e. (CRLF) whereas, Linux and Unix use a simple line feed (LF)

Now, in my case, my build server uses supports Linux and Unix format so, below rule is working perfectly on build server:

linebreak-style: ["error", "unix"]

But I am doing development on Windows and I need to update rule on each git pull/git push as below,

linebreak-style: ["error", "windows"]

So, is there any way to write a generic linebreak-style rule to support both environments, Linux/Unix and Windows?

Note: I am using ECMAScript6[js], WebStorm[ide] for development

Any solutions/suggestions would be highly appreciated. Thanks!

mikemaccana
  • 81,787
  • 73
  • 317
  • 396
Ravindra Thorat
  • 1,120
  • 1
  • 11
  • 22

8 Answers8

61

I spent time trying to find how to shut off the linkbreak-style and lost it due to reverting some of my code I thought others my like to have this as well.

In the .eslintrc file you can also set linebreak-style to 0 which shuts off the linebreak feature:

module.exports = {
  extends: 'google',
  quotes: [2, 'single'],
  globals: {
    SwaggerEditor: false
  },
  env: {
    browser: true
  },
  rules:{
    "linebreak-style": 0   // <----------
  }
};
vsync
  • 87,559
  • 45
  • 247
  • 317
Stu
  • 917
  • 1
  • 7
  • 14
34

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
mikemaccana
  • 81,787
  • 73
  • 317
  • 396
vitorbal
  • 2,470
  • 21
  • 23
  • 18
    Building on this answer I switched to detecting the normal line ending for the platform instead of having to specify the environment, so: `"linebreak-style": ["error", (require("os").EOL === "\r\n" ? "windows" : "unix")]` – Brian J. Miller May 18 '17 at 20:45
  • Uh, you can't set the variables that way on the dev environment if the dev environment is Windows, so the example of setting NODE_ENV=dev that way is invalid. Can you correct it? – Poikilos Nov 28 '20 at 05:47
10

In your .eslintrc.js:

"rules": {
  "linebreak-style": ["error", (process.platform === "win32" ? "windows" : "unix")], // https://stackoverflow.com/q/39114446/2771889
}

See also: How do I determine the current operating system with Node.js

thisismydesign
  • 8,745
  • 4
  • 63
  • 58
6

.eslintc for Windows visualstudio code

{
  "env": {
    "node": true
  },
  "rules":{
    "linebreak-style": 0
  }
}
AndyC
  • 77
  • 1
  • 2
  • That doesn't answer the question, it just turns the valuable feature off. Now you're going to end up with a code base in disarray. – user959690 Feb 18 '21 at 00:03
3

A better option

.editorconfig end_of_line

Add the end_of_line rule in .editorconfig file:

[*]
end_of_line= lf

EditorConfig is an extension for most code editors nowadays that changes the contents of the file you just saved. This rule enforces that all line endings are always unix consistent (\n) each time a developer saves a file (note: MacOs no longer uses \r => cr). For enforcing windows line endings (\r\n) use crlf.

This option is preferable since it avoids all line ending changes to be recorded in the project's repository (version control).

Not so optimal options...

As an EditorConfig note mentions:

if you want to use native line endings between different OSes, it is better not to set this option and leave that task to the VCS! In the future we might add a value like native for this scenario (cf #226).

This would be a replacement for the accepted answer's solution:

"linebreak-style": process.env.NODE_ENV === 'prod' ? "unix" : "windows"

Which is still better than completely disabling the rule ("linebreak-style": 0): developers/contributors inconsistently may use their preferred line endings in the same file...

CPHPython
  • 6,842
  • 2
  • 41
  • 58
2

If you're using Vs Code on Windows, go to your ".eslintrc.json" file (or '.js' depending on which option you chose when setting up your ESLint); this file will usually be found in the root folder of your project; and under rules add the linebreak option to use Windows CRLF as follows:

"rules": {
    "linebreak-style": ["error", "windows"]
}

Save the file and when you go back to your JavaScript file, all those pesky red lines will disappear.

Raymond Wachaga
  • 1,888
  • 1
  • 19
  • 23
1

There are a few answers playing around with.eslintrc and one even suggest switching off the rule altogether.

However, what I prefer is to change VCS settings to checkout LF instead of CRLF. Assuming even in windows env, modern IDE should handle LF just fine. The drawback is some windows software like notepad aren't going to like it. But this way you will be warned if you have incorrect linebreak style. And it is less likely for people to commit incorrect linebreak style even if their VCS is misconfigured. Simply run git config --global core.autocrlf false or follow instructions on How do I force git to use LF instead of CR+LF under windows?

Anyway, turning off the rule is discouraged in general as this will impact other team members, unless you have all of team's consent.

seanplwong
  • 585
  • 2
  • 11
0

The location of the config file required to alter ESLint rules for linebreak-style may change depending on whether you want to alter local, project or global settings, it searches for local first which overrides those further up the tree, so alter at the top of the tree to propagate down for global

I used airbnb style and my global settings were located here: node_modules/eslint-config-airbnb-base/rules/style.js:

If you are unsure on the location of the file you can always search for a list of files that contain text relating to the settings, on Linux to find all files with linebreak settings navigate to the folder where ESLint was installed and use:

grep -r linebreak
Leigh Mathieson
  • 793
  • 1
  • 7
  • 15