问题
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!
回答1:
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
回答2:
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 // <----------
}
};
回答3:
.eslintc for Windows visualstudio code
{
"env": {
"node": true
},
"rules":{
"linebreak-style": 0
}
}
回答4:
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.
回答5:
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
回答6:
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
来源:https://stackoverflow.com/questions/39114446/how-can-i-write-a-eslint-rule-for-linebreak-style-changing-depending-on-windo