繁体   English   中英

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

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

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

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

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

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

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

那么,有什么方法可以编写通用的换行符样式规则来支持 Linux/Unix 和 Windows 这两种环境?

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

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

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

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

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

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.js

"rules": {
  "linebreak-style": ["error", (process.platform === "win32" ? "windows" : "unix")], // https://stackoverflow.com/q/39114446/2771889
}

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

.eslintc 用于 Windows visualstudio 代码

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

更好的选择

.editorconfig end_of_line

.editorconfig文件中添加end_of_line规则:

[*]
end_of_line= lf

EditorConfig是当今大多数代码编辑器的扩展,它可以更改您刚刚保存的文件的内容 此规则强制每次开发人员保存文件时所有行结尾始终是unix 一致的( \n )(注意: MacOs 不再使用\r => cr )。 对于强制Windows行尾( \r\n )使用crlf

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

不是那么理想的选择...

正如EditorConfig 注释所述:

如果您想在不同操作系统之间使用本机行尾,最好不要设置此选项并将该任务留给 VCS! 将来我们可能会为此场景添加一个像 native 这样的值(参见#226 )。

这将替代接受的 answer的解决方案:

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

这仍然比完全禁用规则更好( "linebreak-style": 0 ):开发人员/贡献者不一致地可能在同一个文件中使用他们喜欢的行结尾......

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

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

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

有一些与.eslintrc的答案,其中一个甚至建议完全关闭该规则。

但是,我更喜欢将 VCS 设置更改为结帐LF而不是CRLF 假设即使在 windows 环境中,现代 IDE 也应该可以很好地处理LF 缺点是像记事本这样的一些 Windows 软件不会喜欢它。 但是这样,如果您的换行样式不正确,您将收到警告。 即使他们的 VCS 配置错误,人们也不太可能提交不正确的换行样式。 只需运行git config --global core.autocrlf false或按照如何强制 git 在 Windows 下使用 LF 而不是 CR+LF 的说明进行操作?

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

就我而言, VS 代码的默认设置为 line break CRLF ,因为它是新安装的。 我不得不去改变它: Ctrl + Shift + P打开命令面板并搜索:

更改行尾

在那里你可以将它切换到LF

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

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

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

grep -r linebreak

.eslintrc.js.prettierrc文件中的"endOfLine": "auto"设置对我有用。

文件.eslintrc.js应该有:

rules: {
    /*  ...the other code is omitted for the brevity */
    'arrow-body-style': [1, 'as-needed'],
    'import/extensions': 'off',
    'prettier/prettier': [
        'error',
        {
            semi: false,
            singleQuote: true,
            useTabs: true,
            endOfLine: 'auto', /* this setting should be included */
        },
    ],

文件.prettierrc应该有:

{
    "semi": false,
    "trailingComma": "all",
    "singleQuote": true,
    "useTabs": true,
    "tabWidth": 2,
    "endOfLine": "auto" /* this setting should be included */
}

如果你想为所有linux/unix、mac和windows设置它,只需将eslintrc.json中的linebreak-style替换为"linebreak-style": ["error","unix | windows"]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM