Issue
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!
Solution
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
Answered By - vitorbal Answer Checked By - David Marino (WPSolving Volunteer)