2.1.0 • Published 1 year ago

eslint-plugin-prefer-function-declarations v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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();
}