1.1.0 • Published 4 years ago

eslint-plugin-no-function-declare-after-return v1.1.0

Weekly downloads
357
License
MIT
Repository
github
Last release
4 years ago

eslint-plugin-no-function-declare-after-return

An ESLint plugin to prevent function declaration after return statement
Unit test npm npm bundle size npm

Install

# Using npm
npm i -S no-function-declare-after-return
# Using yarn
yarn add no-function-declare-after-return

Usage

In .eslintrc

{
  "plugins": [
    "no-function-declare-after-return"
  ],
  "rules": {
    "no-function-declare-after-return/no-function-declare-after-return": 2
  }
}

Motivation

Let's consider a code example:

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.

Future features

  • Way to auto-fix the errors
  • Way to configure the error messages

Build with ♡ by

Bhumij Gupta

GitHub followers LinkedIn Twitter Follow


if (repo.isAwesome || repo.isHelpful) {
  StarRepo();
}