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

Option to only allow named function *expressions*, not declarations #23

Open Peeja opened 7 months ago

Peeja commented 7 months ago

Description

Many libraries require named functions for various things, but almost all of them end up taking those functions as expressions, rather than requiring them to be declarations. For instance, React's forwardRef works best with a named function rather than an arrow function, because React will use the name for things like debugging information. But that function is an expression passed to forwardRef(); it isn't usually (and never has to be) a function declaration which binds the function to a variable in scope. That's the thing our team actually wants to avoid. We want to always use const to declare variables, never function; and we never want to use anonymous non-arrow function expressions; but we sometimes need to use named function expressions to work well with certain libraries.

Suggested Solution

An option such that this code is correct:

# Arrow function
const doSomething = () => {
  console.log('Doing something!');
};

# Named function expression
useThisFunction(function doSomething() {
  console.log('Doing something!');
});

while this code is incorrect:

# Named function declaration
function doSomething() {
  console.log('Doing something!');
}

# Anonymous (non-arrow) function expression
useThisFunction(doSomething() {
  console.log('Doing something!');
});

Perhaps allowNamedFunctions: "only-expressions"?

Help Needed

I may be able to work on this if it would be accepted.