AndrewRadev/splitjoin.vim

SplitJoin incorrectly joins one-line functions that return objects

LandonSchropp opened this issue · 2 comments

Hello again!

SplitJoin seems to have no issues with joining JavaScript one-line functions under normal circumstances. For example, the following function is joined without any issues:

const example = () => {
  return "hello";
};
const example = () => "hello";

However, if the function returns an object, it should be joined with parentheses surrounding it.

This function:

[ 1, 2, 3 ].map(value => {
  return { square: value * value };
});

Joins to this (which isn't valid);

[ 1, 2, 3 ].map(value => { square: value * value });

It should join to this:

[ 1, 2, 3 ].map(value => ({ square: value * value }));

Weirdly, while this example won't produce a correct value, it is valid JavaScript syntax. However, if you add another item to the object, isn't not. This example when joined produces a syntax error:

[ 1, 2, 3 ].map(value => {
  return { value, square: value * value };
});
[ 1, 2, 3 ].map(value => { value, square: value * value });

However, if it's surrounded by parentheses, it's perfectly fine.

[ 1, 2, 3 ].map(value => ({ value, square: value * value }));

I don't know if there's an easy way to solve this without parsing the line, so this issue may not be actionable.

I'd noticed this issue myself, but hadn't gotten around to fixing it -- I just pushed a fix, could you check it out?

Sorry to take so long to get back to you. This works great! Thanks so much for tackling this. I can't imagine how complex this extension must get for so many languages. I really appreciate it!