简体   繁体   English

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

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

I have some methods like:我有一些方法,例如:

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

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

First type is ok, may stay that way.第一种类型还可以,可能会保持这种状态。 But I want to know if is possible for the second type to break a line for all methods, like:但我想知道第二种类型是否有可能为所有方法换行,例如:

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

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

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

Eslint have the rule newline-per-chained-call but it won't force the behavior I want. Eslint 有规则newline-per-chained-call但它不会强制我想要的行为。

{ "ignoreChainWithDepth": 2 } will let me do this: { "ignoreChainWithDepth": 2 }让我这样做:

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

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

And well, it's not ideal.好吧,这并不理想。

Is there a way to manage it?有没有办法管理它?

I've tried newline-per-chained-call with the { "ignoreChainWithDepth": 2 } option, and looking for plugins.我已经尝试使用{ "ignoreChainWithDepth": 2 }选项newline-per-chained-call ,并寻找插件。 Did not find any plugin for it.没有找到任何插件。

You can use the array-element-newline rule in ESLint to enforce line breaks for long chains of methods.您可以在 ESLint 中使用 array-element-newline 规则来为长链方法强制换行。 In.eslintrc Ineslintrc

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

This rule is typically used to enforce line breaks in arrays, but it can also be used to enforce line breaks in chains of method calls.此规则通常用于在 arrays 中强制换行,但它也可用于在方法调用链中强制换行。

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

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