本文介绍了如何编写“linebreak-style”的通用ESLint规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都知道在 Windows操作系统中使用的换行符(new line)通常是回车(CR),后跟一个换行符(LF) ie (CRLF)而Linux和Unix 使用简单的换行符(LF)



现在,在我的情况下发生了什么,我的构建服务器使用支持和Unix 格式,所以下面的规则在构建服务器上运行正常,

  linebreak-style:[error ,unix] 

但是我在Windows环境中进行开发,我需要更新每个 git pull / git push 如下,

  linebreak-style:[error,windows ] 

所以,有没有办法编写一个通用的线条风格规则来支持这两种环境,Linux / Unix和Windows?



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



任何解决方案/建议将受到高度评价。谢谢!

解决方案

eslint配置文件可以是常规的 .js 文件导出配置对象。



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



例如,当您的节点环境为prod时,要使用不同的 linebreak-style 配置: p>

  module.exports = {
root:true,
parserOptions:{
sourceType:module,
ecmaVersion:6
},
rules:{
//当生产环境中没有
时, 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错误预期换行符为CRLF但发现'LF'linebreak-style
2:30错误预期的换行符为'CRLF'但是找到'LF'linebreak-style
3:36错误预期的换行符为'CRLF'但是找到'LF' linebreak-style
4:26错误预期的换行符为'CRLF'但是找到'LF'linebreak-style
5:17错误预期的换行符为'CRLF'但是找到'LF'linebreak-style
6:50错误预期的换行符为'CRLF',但找到'LF'linebreak-style
7:62错误预期的换行符为'CRLF'但找到'LF'linebreak-style
8 :21错误预期的换行符为'CRLF',但找到'LF'linebreak-style

✖8个问题(8个错误,0个警告)

$ NODE_ENV = dev node_modules / .bin / eslint src / test.js
$#没有错误


As we all knows that the linebreaks (new line) used in windows operating system 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, what is happening in my case, my build server uses supports Linux and Unix format so, below rule is working perfectly over build server,

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

But I am doing development on Windows environment 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!

解决方案

The eslint configuration file can be a regular .js file 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

这篇关于如何编写“linebreak-style”的通用ESLint规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-16 05:05