5

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.

  1. 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 in js--proper-indentation

  2. 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 by js--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.

lookyhooky
  • 949
  • 7
  • 18
  • Here is a related issue, and it looks as though it has not be resolved, http://github.com/mooz/js2-mode/issues/314 – lookyhooky Sep 02 '16 at 13:37

1 Answers1

1

I have written a solution based on the indentation code of js.el and have called it js-align, simular to cc-align. If you are interested, the code is available here github.com/johnhooks/js-align. It isn't perfect yet, though I plan to continue to hack away at it. If you have any suggestions, feel free to let me know.

lookyhooky
  • 949
  • 7
  • 18
  • Excellent job. Just as a small note, you don't *have* to use curly braces and `return`. You can just put parens around the body – George Mauer Oct 01 '18 at 15:18