我如何为 "linebreak-style" 编写 ESLint 规则,根据 Windows 或 Unix 进行更改?

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

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

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

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

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

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

那么,有什么方法可以编写通用的 linebreak-style 规则来支持两种环境,Linux/Unix 和 Windows?

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

任何 solutions/suggestions 将不胜感激。谢谢!

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

我花了很多时间试图找到如何关闭链接中断样式,但由于恢复了我的一些代码而丢失了它我认为其他人也喜欢这个。

.eslintrc 文件中,您还可以将 linebreak-style 设置为 0,从而关闭 linebreak 功能:

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

.eslintrc 用于 Windows visual studio 代码

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

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

我使用了 airbnb 样式,我的全局设置位于此处:node_modules/eslint-config-airbnb-base/rules/style.js:

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

grep -r linebreak

在你的.eslintrc.js中:

"rules": {
  "linebreak-style": ["error", (process.platform === "win32" ? "windows" : "unix")], // 
}

另请参阅:How do I determine the current operating system with Node.js

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

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

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

更好的选择

.editorconfig end_of_line

添加 end_of_line rule in .editorconfig 文件:

[*]
end_of_line= lf

EditorConfig is an extension for most code editors nowadays that changes the contents of the file you just saved. This rule enforces that all line endings are always unix consistent (\n) each time a developer saves a file (note: MacOs no longer uses \r => cr)。 要强制执行 windows 行结尾 (\r\n),请使用 crlf.

此选项更可取,因为它避免了将所有行结束更改记录在项目的存储库(版本控制)中。

不是最佳选择...

正如 EditorConfig note 提到的:

if you want to use native line endings between different OSes, it is better not to set this option and leave that task to the VCS! In the future we might add a value like native for this scenario (cf #226).

这将替代 的解决方案:

"linebreak-style": process.env.NODE_ENV === 'prod' ? "unix" : "windows"

这仍然比完全禁用规则 ("linebreak-style": 0) 更好:developers/contributors 不一致可能会在同一文件中使用他们首选的行结尾...

有一些答案在玩弄.eslintrc,甚至有人建议完全关闭规则。

不过,我更喜欢将 VCS 设置更改为签出 LF 而不是 CRLF。假设即使在 windows 环境中,现代 IDE 也应该可以很好地处理 LF。缺点是某些 windows 软件(如记事本)不会喜欢它。但是如果你的换行样式不正确,你会被警告。即使他们的 VCS 配置错误,人们也不太可能犯下错误的换行风格。只需 运行 git config --global core.autocrlf false 或按照 How do I force git to use LF instead of CR+LF under windows?

上的说明进行操作

无论如何,通常不鼓励关闭规则,因为这会影响其他团队成员,除非您获得团队的所有同意。

在我的例子中,VS code 有换行符的默认设置 CRLF,因为 它是新安装的。我不得不通过以下方式更改它:Ctrl + Shift + P 打开命令面板并搜索:

Change for line endings

在那里你可以切换到LF.