js-emacs / js2-refactor.el

A JavaScript refactoring library for emacs
GNU General Public License v3.0
373 stars 47 forks source link

calling js2r-toggle-arrow-function-and-expression is not idempotent for expressions returning json #98

Open Fuco1 opened 6 years ago

Fuco1 commented 6 years ago

I have code like this

const values = eans.map(ean => ({
    "key": ean.EanId,
    "value": ean.EanId
}));

When I toggle to function function it returns

const values = eans.map(function (ean) { return ({
    "key": ean.EanId,
    "value": ean.EanId
}); });

I would expect

const values = eans.map(function (ean) {
    return {
        "key": ean.EanId,
        "value": ean.EanId
    };
});

When I call it again it gives

const values = eans.map(ean => { return ({
    "key": ean.EanId,
    "value": ean.EanId
}); });

which is not what I started with.

NicolasPetton commented 6 years ago

That's because there's no way to know from a function expression if an arrow function should contain a return statement or not since both are valid.

Regarding the formatting issue, this is something which can be improved.

Fuco1 commented 6 years ago

Wouldn't it be possible to not include the return if not necessary then? I think most people would prefer such a style (we even have some lint checks that complain when there are unnecessary returns).

I'm not sure how feasible it is and I've only been working with javascript for a short time, so please excuse any ignorance.

Granted, this is not the most pressing issue there is.