You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用ESLint强制链式方法首个调用从新行开始?

解决方案

要实现你想要的代码格式,需要结合两个ESLint规则配合完成:

  1. 控制await后的换行:用operator-linebreak规则强制await运算符后换行
  2. 控制链式调用的换行:保留newline-per-chained-call的现有配置,确保链式调用的每个方法单独成行

具体配置如下:

module.exports = {
  // 其他配置项...
  rules: {
    // 强制await运算符后换行
    'operator-linebreak': ['error', 'after', {
      overrides: {
        'await': 'after'
      }
    }],
    // 确保深度≥2的链式调用强制换行(你的现有配置)
    'newline-per-chained-call': ['error', { ignoreChainWithDepth: 1 }]
  }
}

规则说明:

  • operator-linebreak的after选项指定运算符(此处为await)后换行,通过overrides针对await单独配置,确保await与后续的database不在同一行。
  • newline-per-chained-call的ignoreChainWithDepth: 1表示仅允许深度≤1的链式调用(如database.collection())不强制换行,深度≥2的链式调用(如database.collection().findOne())会被强制要求每个方法单独成行,正好满足.collection和.findOne分别换行的需求。

内容的提问来源于stack exchange,提问作者Lincoln

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 14:45:32