1 # require a newline after each call in a method chain (newline-per-chained-call)
3 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.
5 Let's look at the following perfectly valid (but single line) code.
8 d3.select("body").selectAll("p").data([4, 8, 15, 16, 23, 42 ]).enter().append("p").text(function(d) { return "I'm number " + d + "!"; });
11 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.
28 return "I'm number " + d + "!";
32 Another argument in favor of this style is that it improves the clarity of diffs when something in the method chain is changed:
37 -d3.select("body").selectAll("p").style("color", "white");
38 +d3.select("body").selectAll("p").style("color", "blue");
47 - .style("color", "white");
48 + .style("color", "blue");
53 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.
57 This rule has an object option:
59 * `"ignoreChainWithDepth"` (default: `2`) allows chains up to a specified depth.
61 ### ignoreChainWithDepth
63 Examples of **incorrect** code for this rule with the default `{ "ignoreChainWithDepth": 2 }` option:
66 /*eslint newline-per-chained-call: ["error", { "ignoreChainWithDepth": 2 }]*/
68 _.chain({}).map(foo).filter(bar).value();
71 _.chain({}).map(foo).filter(bar);
79 obj.method().method2().method3();
82 Examples of **correct** code for this rule with the default `{ "ignoreChainWithDepth": 2 }` option:
85 /*eslint newline-per-chained-call: ["error", { "ignoreChainWithDepth": 2 }]*/
116 ## When Not To Use It
118 If you have conflicting rules or when you are fine with chained calls on one line, you can safely turn this rule off.