簡體   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