Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

comma-dangle: should not insert comma in arrow function return expressions #300

Open
4 of 5 tasks
noelforte opened this issue Mar 19, 2024 · 0 comments
Open
4 of 5 tasks

Comments

@noelforte
Copy link

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:

const nums = [1, 2, 3];

const remappedArray = 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!

Reproduction

https://stackblitz.com/edit/github-2cdzyb?file=src%2Findex.ts&view=editor

Contributes

  • I am willing to submit a PR to fix this issue
  • I am willing to submit a PR with failing tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant