Javascript 如何根据 Windows 或 Unix 为“换行样式”编写 ESLint 规则?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39114446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:12:50  来源:igfitidea点击:

How can I write a ESLint rule for "linebreak-style", changing depending on Windows or Unix?

javascriptwindowsunixeslintline-endings

提问by Ravindra Thorat

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)

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

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

现在,就我而言,我的构建服务器使用支持 Linux 和 Unix 格式,因此,以下规则在构建服务器上运行良好:

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

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

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

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

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

那么,有没有办法编写一个通用的换行样式规则来支持 Linux/Unix 和 Windows 两种环境?

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

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

Any solutions/suggestions would be highly appreciated. Thanks!

任何解决方案/建议将不胜感激。谢谢!

采纳答案by vitorbal

The eslint configuration file can be a regular .jsfile(ie, not JSON, but full JS with logic) that exports the configuration object.

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

That means you could change the configuration of the linebreak-stylerule depending on your current environment (or any other JS logic you can think of).

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

For example, to use a different linebreak-styleconfiguration when your node environment is 'prod':

例如,要linebreak-style在您的节点环境为“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

回答by Stu

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 .eslintrcfile you can also set linebreak-styleto 0which shuts off the linebreakfeature:

.eslintrc文件中,您还可以设置linebreak-style0其关闭换行符特点:

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

回答by thisismydesign

In your .eslintrc.js:

在您的.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

另请参阅:如何使用 Node.js 确定当前的操作系统

回答by AndyC

.eslintc for Windows visualstudio code

.eslintc 用于 Windows 视觉工作室代码

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

回答by Raymond Wachaga

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:

如果您在 Windows 上使用 Vs Code,请转到“.eslintrc.json”文件(或“.js”,具体取决于您在设置 ESLint 时选择的选项);该文件通常位于项目的根文件夹中;并根据规则添加换行选项以使用 Windows CRLF,如下所示:

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

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

保存文件,当您返回 JavaScript 文件时,所有那些讨厌的红线都将消失。

回答by Leigh Mathieson

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

更改换行样式的 ESLint 规则所需的配置文件的位置可能会根据您是要更改本地、项目还是全局设置而变化,它首先搜索本地,覆盖树上的那些,因此在顶部更改向下传播到全球的树

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

我使用了 airbnb 风格,我的全局设置位于: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:

如果您不确定文件的位置,您可以随时搜索包含与设置相关的文本的文件列表,在 Linux 上找到所有带有换行设置的文件,导航到安装 ESLint 的文件夹并使用:

grep -r linebreak