我如何写一个“linebreak-style”的ESLint规则,根据Windows或Unix改变?

众所周知,Windows中使用的换行符(换行符)通常是回车符(CR),后跟换行符(LF),即(CRLF),而Linux和Unix使用简单的换行符(LF)

现在,在我的情况下,我的构build服务器使用支持Linux和Unix格式,所以下面的规则在构build服务器上完美地工作:

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

但是我在Windows上进行开发,我需要更新每个git pull / git push的规则,如下所示,

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

那么,有什么办法可以编写一个通用的换行符规则来支持Linux / Unix和Windows?

注意 :我正在使用ECMAScript6 [js],WebStorm [ide]进行开发

任何解决scheme/build议将不胜感激。 谢谢!

eslint配置文件可以是一个常规的.js文件 (即,不是JSON,而是带有逻辑的完整JS),用于导出配置对象。

这意味着您可以根据您当前的环境(或您可以想到的任何其他JS逻辑)更改linebreak-style规则的配置。

例如,要在节点环境为“prod”时使用不同的linebreak-style配置:

 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"] } }; 

用法示例:

 $ 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 

我花了一些时间试图找出如何关闭linkbreak风格,并由于恢复了一些我认为别人也喜欢的代码而丢失了它。 在.eslintrc.js文件中,还可以将换行符样式设置为0,从而关闭换行符功能:

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

.eslintc for Windows visualstudio代码

 { "env": { "node": true }, "rules":{ "linebreak-style": 0 } }