Using Emacs 25.1 and js-mode for javascript editing.
Is it possible to change the indention rules in js-mode to customize the indention of arrow functions?
Example of anonymous function indention:
const x = list.map(function (x) {
return someCoolFunction(x)
.maybeEvenAChain()
.or(two)
})
How I would like arrow functions indented:
const x = list.map(x =>
someCoolFunction(x)
.maybeEvenAChain()
.or(two)
)
This is what emacs is doing right now:
const x = list.map(x =>
someCoolFunction(x)
.maybeEvenAChain()
.or(two)
)
UPDATE:
I have looked through the indention code of js.el, and there seem to be two reasons arrow functions are not indented like anonymous functions.
If an opening paren, square or curly brace has any text to the right all the text inside will be indented to the column of the first word after the opening punctuation. Also, the closing punctuation will be indented to the same column of the opening punctuation.
This is the desired behavior for object literals and the argument list of both function definitions and calls.
This functionality is defined injs--proper-indentation
If an exception is made for the arrow operator
=>
to resolve the first issue, a second issue arises. The arrow operator is considered a continued expression byjs--continued-expression-p
. The body of the arrow function will indented to(* 2 js-indent-level)
. This aligns the body with any actual continued expressions inside the body, which looks pretty messy.
I am still pursuing a comprehensive way to resolve the issue.