dprint-plugin-typescript icon indicating copy to clipboard operation
dprint-plugin-typescript copied to clipboard

Multi-line arrow-function expression body should not add line break after arrow in certain circumstances

Open WearyMonkey opened this issue 4 years ago • 3 comments

Describe the bug

dprint-plugin-typescript version: 0.40.1

Input Code

colors.map(color => darken({
    color,
    opacity: 1
}));

Expected Output

colors.map(color => darken({
    color,
    opacity: 1
}));

Actual Output

colors.map(color =>
  darken({
    color,
    opacity: 1,
  })
);

Playground link

Adding a line break before the arrow makes a lot of code unnecessarily verbose with small expressions.

Thank you!

WearyMonkey avatar Feb 20 '21 02:02 WearyMonkey

Another similar example from @declanvong:

https://dprint.dev/playground/#code/GYexAoDsFMHcAIDyAjAVuA3gKHr+BDALnlBABoc9ljl8AnCgXwEpmBuIA/language/typescript

foo(new Obj({
    a: foo,
    b: bar,
}));

Outputs:

foo(
    new Obj({
        a: foo,
        b: bar,
    }),
);

(I know these examples are different, but perhaps the implementation could use the same logic)

dsherret avatar Sep 30 '21 13:09 dsherret

I agree with @WearyMonkey, arrow functions that return single expression should be able to be compacted as much as they can, the current behavior discourages the usage of higher order functions in cases like currying etc.:

const add = x => y => {
  return x + y
}

becomes:

const add = x =>
    y => {
        return x + y;
    }

which is less readable as the "y" is now in a different line than the "x" so harder to reason about.

stagas avatar Mar 23 '22 05:03 stagas

my example:

const render = () => html`
    <div b="1 rd-4 gray-300"> 
        <button m-4 p-2 rd onclick="${addKid}">+</button>
    </div>
`

(html shouldn't go on next line)

Did not find any config to change this behaviour

tennox avatar Jun 11 '23 14:06 tennox