An ESLint plugin to prevent function declaration after return statement
npm install eslint-plugin-no-function-declare-after-returnAn ESLint plugin to prevent function declaration after return statement
!Unit test !npm !npm bundle size !npm
``bash`Using npm
npm i -S no-function-declare-after-returnUsing yarn
yarn add no-function-declare-after-return
In .eslintrc
`javascript`
{
"plugins": [
"no-function-declare-after-return"
],
"rules": {
"no-function-declare-after-return/no-function-declare-after-return": 2
}
}
Let's consider a code example:
`javascript`
function publicMethods(obj){
if(obj instanceof customClass)
return {
set: methodSetter(obj),
get: methodGetter(obj),
}
function methodSetter(obj){
.
.
.
}
function methodGetter(obj){
.
.
.
}
}
The function compiles succesfully even though the functions are used before declaration. This is due to the fact that -
> Function declarations in JavaScript are hoisted to the top of the enclosing function or global scope. (More Info)
But considering from a readability standpoint, it is quite difficult to figure out where the function is defined and also difficult for new comers to keep in mind the concept of hoisting.
This plugin will enforce that there are no function declarations are the return statement.
Note : This plugin is separate, and in no way replaces no-unreachable-code of ESLint.
- [x] Way to auto-fix the errors
- [ ] Way to configure the error messages
!GitHub followers  !Twitter Follow
---
`javascript``
if (repo.isAwesome || repo.isHelpful) {
StarRepo();
}