Skip to content

New Rule: no-else #19871

Closed as not planned
Closed as not planned
@raphael-arce

Description

@raphael-arce

Rule details

An ESLint plugin that forbids else blocks and encourages early returns instead

Related ECMAScript feature

None

What type of rule is this?

Suggests an alternate way of doing something

Example code

/**
 * Bad example
 * /
function getSign(x) {
    if (x > 0) {
      return '+';
    } else if (x < 0) {
      return '-';
    } else {
      return '';
    }
}

/**
 * Good example
 * /
function getSign(x) {
  if (x > 0) {
    return '+';
  }

  if (x < 0) {
    return '-';
  }

  return '';
}

Why should this rule be in the core instead of a plugin?

This makes the code flow more linear and easier to follow / understand.

Participation

  • I am willing to submit a pull request to implement this rule.

Additional comments

Here would be an example implementation of the rule: https://github.com/raphael-arce/eslint-plugin-no-else/blob/main/src/no-else.js
(First time writing an EsLint plugin/rule, not sure the implementation is correct)

Metadata

Metadata

Assignees

Labels

featureThis change adds a new feature to ESLintruleRelates to ESLint's core rules

Type

No type

Projects

Status

Complete

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy