Skip to content

Commit

Permalink
feat(prettier): switch arrowParens to "always"
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcdodds committed Jun 14, 2024
1 parent ec805aa commit 5f1b6ee
Show file tree
Hide file tree
Showing 5 changed files with 4,645 additions and 4,597 deletions.
48 changes: 48 additions & 0 deletions docs/decisions/006-arrow-parens.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Arrow Parens

Date: 2024-06-13

Status: accepted

## Context

Prettier has a configuration option called `arrowParens` which decides whether
to add parentheses around the arguments of arrow functions. The available
options are:

- "always" - Add parentheses around the arguments of arrow functions.
- "avoid" - Only add parentheses around the arguments of arrow functions if it
improves readability.

The "always" option adds parentheses around the arguments of arrow functions,
even if there's only one argument. This can result in unnecessary parentheses in
the code.

The "avoid" option removes parentheses around the arguments if there is only one
argument (and that one argument is not being destructured or defaulted). This
means that if the argument is a single identifier, it will be printed without
parentheses. However, if the argument is a more complex expression, parentheses
will be added due to syntax requirements.

Just reading those descriptions demonstrates that the rules around when it's ok
to avoid parentheses are more complicated than the simple rule of: "always have
parentheses".

Additionally, consider this: if you have a single argument in an arrow function,
you will not have parentheses around it. If you then decide to destructure it,
add an additional argument, add a type, or add a default value, you will have to
add parentheses.

We want to avoid the extra work required to refactor code as much as possible.
Additionally, simpler rules are often better. The simple rule of "always have
parentheses" around the arguments of arrow functions is much simpler.

## Decision

Update the prettier config from "avoid" to "always."

## Consequences

People will need to reformat their code when they update `@epic-web/config`. In
accordance to our [semver policy](./003-semver.md), we will not be treating this
as a major version bump.
2 changes: 1 addition & 1 deletion eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
const ERROR = 'error'
const WARN = 'warn'

const has = pkg => {
const has = (pkg) => {
try {
import.meta.resolve(pkg, import.meta.url)
return true
Expand Down
2 changes: 1 addition & 1 deletion fixture/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { useState } from 'react'

export function Counter() {
const [count, setCount] = useState(0)
const increment = () => setCount(c => c + 1)
const increment = () => setCount((c) => c + 1)
return <button onClick={increment}>{count}</button>
}
Loading

1 comment on commit 5f1b6ee

@kentcdodds
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shout out to @jacobparis who inspired this improvement

Please sign in to comment.