JSPM

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

1.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 8274
  • Score
    100M100P100Q157006F
  • License MIT

An ESLint plugin to prevent function declaration after return statement

Package Exports

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

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (eslint-plugin-no-function-declare-after-return) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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

Bhumij profile picture

GitHub followers LinkedIn Twitter Follow


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