MetaMask / MetaMask/eslint-config

Enforce use of `function` for declared functions

Open
#324 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

team-wallet-framework
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:

  1. It's not immediately obvious that the thing being set is a function. function pops out more visually.
  2. 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 using function, 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 function functions are JSDoc'd, whereas we don't do that for const functions. We could certainly add another rule, but it would be nice if we didn't have to.
  • function functions 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 the describe to hide them, thereby making describe the first thing that readers see when reading the file.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.