MetaMask / MetaMask/eslint-config
Enforce use of `function` for declared functions
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 12
- Forks
- 21
- PR merge metrics
- No merged PRs in 30d
Description
There are two ways to declare functions:
// 1
function foo(x: number): string {
// ...
}
// 2
const foo = (x: number): string => {
// ...
}
I propose that we add a rule which enforces that declared functions use the first style rather the second. There are a couple of issues with using an arrow function that I think function solves:
- It's not immediately obvious that the thing being set is a function.
functionpops out more visually. - Adding type information for arrow functions can be confusing syntactically. (I've seen less senior developers struggle with doing this.)
=>is a bit overloaded, as it's used to indicate the return value for a function type in TypeScript. When usingfunction, however, you don't have to deal with the ambiguity of=>nor do you have extra characters in the way.:always separates the arguments from the return value, and the return value always goes before the{.
There are also some benefits for using function:
- Currently we enforce that
functionfunctions are JSDoc'd, whereas we don't do that forconstfunctions. We could certainly add another rule, but it would be nice if we didn't have to. functionfunctions are hoisted; when defined at the top level, this means they are automatically available anywhere in the file no matter where they're defined. This comes in handy when writing tests. If you want to define test helpers for tests, you can put them below thedescribeto hide them, thereby makingdescribethe first thing that readers see when reading the file.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the repository's ESLint configuration and existing function-related rules; the issue does not name specific files or tests. Determine how a rule would distinguish declared functions from arrow-function assignments, then verify that the configuration enforces the proposed style without leaving the behavior ambiguous.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- eslint, typescript
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100