Skip to content

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

Open
@Peeja

Description

@Peeja

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions