esformatter icon indicating copy to clipboard operation
esformatter copied to clipboard

Add config for anonymous functions

Open lucasmotta opened this issue 9 years ago • 2 comments

I like having a space on the top and bottom of my functions (both declaration and expression), but not when I use anonymous functions, like in timeouts or array methods, like this:

array.forEach(function(a) {

  console.log("do something");

});

The output at the moment using FunctionDeclaration: 2 and FunctionExpression: 2:

array.forEach(

      function(a) {

        el.style.display = 'none';

      });

Is there anyway to set a config only for those situation (anonymous functions)?

lucasmotta avatar Jun 02 '15 17:06 lucasmotta

probably easier to do it as plugin than add even more settings to esformatter.. probably something that just checks if the function is an argument or not before adding/removing the line breaks. should be easy to write it.

basically a nodeAfter hook and check if node.type === 'FunctionExpression' && node.parent.type === 'CallExpression' and call br.limitBefore(node.startToken, 0) to remove the line breaks.

indentation is only applied as the last step (and only if function starts on an empty line), so if you remove the line break in between ( and function it should be indented properly.

millermedeiros avatar Jun 02 '15 18:06 millermedeiros

Good shout @millermedeiros! I did a small test locally and it worked perfectly! I will write some tests, find a proper name for the plugin and publish to npm/github.

Cheers! :)

lucasmotta avatar Jun 03 '15 09:06 lucasmotta