2.1.0 • Published 3 years ago
eslint-plugin-prefer-function-declarations v2.1.0
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-declarationsAdd 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 --fixCode 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();
}