eslint: newline-per-chained-call not working as expected

What rule do you want to change? newline-per-chained-call

Does this change cause the rule to produce more or fewer warnings? More.

How will the change be implemented? (New option, new default behavior, etc.)? A new option.

Please provide some example code that this change will affect:

NOTE: D3 example copied straight from the docs.

// ignoreChainWithDepth: 1
let d3

// Currently allowed.
d3.select("body")
.selectAll("p")
.data([4, 8, 15, 16, 23, 42 ])

// What I'd expect to see.
d3
.select("body")
.selectAll("p")
.data([4, 8, 15, 16, 23, 42 ])

let someReallyLongArrayName = []

// Currently allowed.
someReallyLongArrayName.map(({
  foo,
  bar,
}) => ({
  foo,
  bar,
}))
.filter(Boolean)

// What I'd expect to see.
someReallyLongArrayName 
.map(({
  foo,
  bar,
}) => ({
  foo,
  bar,
}))
.filter(Boolean)

What does the rule currently do for this code? With the ignoreChainWithDepth value, there’s no way to say “if the chain depth is no longer ignored, all values need newlines”.

At the point that this rule is triggered, instead of allowing only the first chained prop to stay at the top, all props would be on a new line.

What will the rule do after it’s changed? It will look to see if the chain depth has been passed and if so, enforce also dropping the first value for consistency. It’s strange to have one chain not being dropped, but the rest having to be dropped.

Are you willing to submit a pull request to implement this change? Yes.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 20 (19 by maintainers)

Commits related to this issue

Most upvoted comments

I was thinking about a non-breaking change like this:

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

// valid
obj.a().b().c().d();

// invalid
obj.a().b().c().d().e();

// invalid. this would be valid without allowLeading
obj.a().b().c().d()
  .e();

// invalid. this would be valid without allowLeading
obj.a().b().c()
  .d()
  .e();

// valid. the two invalid examples above would be autofixed to this
obj.a().b()
  .c()
  .d()
  .e();

// also valid
obj.a()
  .b()
  .c()
  .d()
  .e();

// also valid
obj
  .a()
  .b()
  .c()
  .d()
  .e();

allowLeading is a new option. There could be a better name, though.

If it isn’t set, the behavior is the same as the actual behavior.

"allowLeading": 0 restores behavior from v2 (only the last example would be valid).