繁体   English   中英

只有超过两个时,是否有办法为所有链接方法换行?

[英]Is there a way to break lines for all chained methods only if more than two?

我有一些方法,例如:

// 1. two methods chained
first.method().anotherMethod();

// 2. three or more
second.method().anotherMethod().yetAnotherOne();
third.method().anotherMethod().yetAnotherOne().stillGoing();

第一种类型还可以,可能会保持这种状态。 但我想知道第二种类型是否有可能为所有方法换行,例如:

// 1. two methods chained
first.method().anotherMethod();

// 2. three or more
second.method()
  .anotherMethod()
  .yetAnotherOne();

third.method()
  .anotherMethod()
  .yetAnotherOne()
  .stillGoing();

Eslint 有规则newline-per-chained-call但它不会强制我想要的行为。

{ "ignoreChainWithDepth": 2 }让我这样做:

second.method().anotherMethod()
  .yetAnotherOne();

third.method().anotherMethod()
  .yetAnotherOne()
  .stillGoing();

好吧,这并不理想。

有没有办法管理它?

我已经尝试使用{ "ignoreChainWithDepth": 2 }选项newline-per-chained-call ,并寻找插件。 没有找到任何插件。

您可以在 ESLint 中使用 array-element-newline 规则来为长链方法强制换行。 Ineslintrc

{
  "rules": {
    "array-element-newline": ["error", { "multiline": true, "minItems": 3 }]
  }
}

此规则通常用于在 arrays 中强制换行,但它也可用于在方法调用链中强制换行。

暂无
暂无

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

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