Jump to content

Topic on Manual talk:Coding conventions/JavaScript

Arrow function callbacks

6
ESanders (WMF) (talkcontribs)
MusikAnimal (talkcontribs)

Is it possible to enforce this only when doing so would not also break other eslint rules? I often find myself having to craft a one-liner that satisfies both prefer-arrow-callback and max-len, for example.

ESanders (WMF) (talkcontribs)

prefer-arrow-callback shouldn't affect how many lines you can use in a callback. Can you give an example?

MusikAnimal (talkcontribs)

Sure. Consider:

// eslint-disable-next-line arrow-body-style
provide: ( stateField ) => {
	// eslint-disable-next-line arrow-body-style
	return showPanel.from( stateField, ( on ) => {
		return on ? () => this.panel : null;
	} );
}

And if prefer-arrow-callback had its way:

// eslint-disable-next-line max-len
provide: ( stateField ) => showPanel.from( stateField, ( on ) => on ? () => this.panel : null )

Clearly the first example is more legible. Worse, the second is considered acceptable given max-len is only a warning. Or is there a third variant I'm missing here?

I think prefer-arrow-callback is great practice, but ideally it would only apply if what would follow => does not then cause it to break max-len. Hopefully that makes sense.

Jdforrester (WMF) (talkcontribs)

Rather than max-len disables, why not just write it out with an indent like normal, i.e.:

provide: ( stateField ) => showPanel.from(
    stateField, ( on ) => on ? () => this.panel : null
)
ESanders (WMF) (talkcontribs)

As James shows, long expressions can usually be split elsewhere.

Is it possible to enforce this only when doing so would not also break other eslint rules?

To answer your original question, I don't think it is. Rules run independently.

Reply to "Arrow function callbacks"