eslint-plugin-prefer-function-declarations
TypeScript icon, indicating that this package has built-in type declarations

2.1.0 • Public • Published

eslint-plugin-prefer-function-declarations

ESLint plugin to prefer function declarations instead of arrow functions or function expressions. Arrow functions are still allowed in specific use cases, for example, callbacks.

This plugin will automatically fix your code using ESLint's --fix option.

Installation

Install the npm package

# If eslint is installed globally
npm install -g eslint-plugin-prefer-function-declarations

# If eslint is installed locally
npm install -D eslint-plugin-prefer-function-declarations

Add the plugin to the plugins section and the rule to the rules section in your .eslintrc

"plugins": [
  "prefer-function-declarations"
],
"rules": {
  "prefer-function-declarations/prefer-function-declarations": "error"
}

Autofixing

To autofix your code, simply run ESLint with the --fix option.

eslint --fix

Code and Autofixes

Arrow function

const myFunction = () => 5;

Autofixes to

function myFunction() {
  return 5;
}

Function Expressions

const myFunction = function hello() {
  return 5;
};

Autofixes to

function myFunction() {
  return 5;
}

Arrow functions inside Arrow Functions

const outerFunction = () => {
  const getMyNumber = () => {
    const variable = 5;
    return variable;
  };
  const getOtherNumber = () => 5;

  return getMyNumber() + getOtherNumber();
};

Autofixes to

function outerFunction() {
  function getMyNumber() {
    const nu = 5;
    return nu;
  }

  function getOtherNumber() {
    return 5;
  }

  return getMyNumber() + getOtherNumber();
}

Package Sidebar

Install

npm i eslint-plugin-prefer-function-declarations

Weekly Downloads

138

Version

2.1.0

License

MIT

Unpacked Size

26.5 kB

Total Files

15

Last publish

Collaborators

  • bmvantunes