You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When writing arrow functions with an implicit return, sometimes it's useful to drop onto the next line to get more space to write the expression. Prettier uses this pattern a lot:
constnums=[1,2,3];constremappedArray=nums.map((el)=>`${el} plus 5 is ${el+5}`);
However when using a callback function that also happens to be an arrow function with an implicit return statement, such as when working with arrays, patterns like:
(el)=>`${el} plus 5 is ${el+5}`
get formatted to:
(el)=>`${el} plus 5 is ${el+5}`,
While valid, it is somewhat confusing because a line ending with a comma is not allowed in a return statement, but is allowed in an expression via the comma operator...but neither of those cases are happening here becuase Array.map takes 2 arguments, the callback and thisArg. So, in this case JavaScript is interpreting the above as:
... nums.map(((el)=>`${el} plus 5 is ${el+5}`),)...
Since the comma operator doesn't seem to be as well-known of a feature and the correct way to use it in an arrow function is actually () => (exp1, exp2, ...), perhaps the best way to handle this would be to not insert dangling commas in places where their meaning is ambiguous.
Happy to submit a PR with failing tests, however Vitest isn't something I'm as familiar with so I don't know how much help I could be. Still willing to give it a shot though!
Validations
Describe the bug
Possibly related: #158 / #160
When writing arrow functions with an implicit return, sometimes it's useful to drop onto the next line to get more space to write the expression. Prettier uses this pattern a lot:
However when using a callback function that also happens to be an arrow function with an implicit return statement, such as when working with arrays, patterns like:
get formatted to:
While valid, it is somewhat confusing because a line ending with a comma is not allowed in a return statement, but is allowed in an expression via the comma operator...but neither of those cases are happening here becuase
Array.map
takes 2 arguments, the callback andthisArg
. So, in this case JavaScript is interpreting the above as:Since the comma operator doesn't seem to be as well-known of a feature and the correct way to use it in an arrow function is actually
() => (exp1, exp2, ...)
, perhaps the best way to handle this would be to not insert dangling commas in places where their meaning is ambiguous.Happy to submit a PR with failing tests, however Vitest isn't something I'm as familiar with so I don't know how much help I could be. Still willing to give it a shot though!
Reproduction
https://stackblitz.com/edit/github-2cdzyb?file=src%2Findex.ts&view=editor
Contributes
The text was updated successfully, but these errors were encountered: