JamieMason / eslint-plugin-prefer-arrow-functions

Auto-fix plain Functions into Arrow Functions, in all cases where conversion would result in the same behaviour
https://www.npmjs.com/package/eslint-plugin-prefer-arrow-functions
MIT License
43 stars 11 forks source link

More intelligent `returnStyle` option #21

Closed u3u closed 10 months ago

u3u commented 1 year ago

Description

I hope that arrow functions can be forced to convert to implicit style when there is only one line.

const arr = [0, 1, 2]

arr.filter((x) => {
  return x % 2 === 0
})

// ↓ ↓ ↓ ↓ ↓ ↓
arr.filter((x) => x % 2 === 0)

But if there are multiple lines, it can be forced to convert to the explicit style.

export const delay = (timeout) =>
  new Promise((resolve) => {
    setTimeout(() => {
      resolve()
    }, timeout)
  })

// ↓ ↓ ↓ ↓ ↓ ↓
export const delay = (timeout) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve()
    }, timeout)
  })
}

This also makes it easier to extend React functional components.

export const Button = () => (
  <button>
    <span></span>
  </button>
)

// ↓ ↓ ↓ ↓ ↓ ↓
export const Button = () => {
  return (
    <button>
      <span></span>
    </button>
  )
}

ref: https://github.com/eslint/eslint/issues/9062

Suggested Solution

Add a new smart enumeration to the returnStyle option for intelligent judgment.

u3u commented 10 months ago

I implemented it myself. https://github.com/u3u/eslint-plugin-arrow-return-style

/cc @tjbenton

JamieMason commented 10 months ago

Great work @u3u, thank you.