Skip to content

Latest commit

 

History

History
127 lines (91 loc) · 2.47 KB

newline-per-chained-call.md

File metadata and controls

127 lines (91 loc) · 2.47 KB
规则名 规则类型
newline-per-chained-call
layout

Chained method calls on a single line without line breaks are harder to read, so some developers place a newline character after each method call in the chain to make it more readable and easy to maintain.

Let's look at the following perfectly valid (but single line) code.

d3.select("body").selectAll("p").data([4, 8, 15, 16, 23, 42 ]).enter().append("p").text(function(d) { return "I'm number " + d + "!"; });

However, with appropriate new lines, it becomes easy to read and understand. Look at the same code written below with line breaks after each call.

d3
    .select("body")
    .selectAll("p")
    .data([
        4,
        8,
        15,
        16,
        23,
        42
    ])
    .enter()
    .append("p")
    .text(function (d) {
        return "I'm number " + d + "!";
    });

Another argument in favor of this style is that it improves the clarity of diffs when something in the method chain is changed:

Less clear:

-d3.select("body").selectAll("p").style("color", "white");
+d3.select("body").selectAll("p").style("color", "blue");

More clear:

d3
    .select("body")
    .selectAll("p")
-    .style("color", "white");
+    .style("color", "blue");

规则详解

This rule requires a newline after each call in a method chain or deep member access. Computed property accesses such as instance[something] are excluded.

配置项

This rule has an object option:

  • "ignoreChainWithDepth" (default: 2) allows chains up to a specified depth.

ignoreChainWithDepth

选项 { "ignoreChainWithDepth": 2 } 默认值的 错误 代码示例:

/*eslint newline-per-chained-call: ["error", { "ignoreChainWithDepth": 2 }]*/

_.chain({}).map(foo).filter(bar).value();

// Or
_.chain({}).map(foo).filter(bar);

// Or
_
  .chain({}).map(foo)
  .filter(bar);

// Or
obj.method().method2().method3();

选项 { "ignoreChainWithDepth": 2 } 默认值的 正确 代码示例:

::: correct

/*eslint newline-per-chained-call: ["error", { "ignoreChainWithDepth": 2 }]*/

_
  .chain({})
  .map(foo)
  .filter(bar)
  .value();

// Or
_
  .chain({})
  .map(foo)
  .filter(bar);

// Or
_.chain({})
  .map(foo)
  .filter(bar);

// Or
obj
  .prop
  .method().prop;

// Or
obj
  .prop.method()
  .method2()
  .method3().prop;

禁用建议

If you have conflicting rules or when you are fine with chained calls on one line, you can safely turn this rule off.