JoshuaKGoldberg / JoshuaKGoldberg/create-typescript-app

🚀 Feature: Find or create a lint rule to prefer inverting a single 'if' to reduce nesting

Open
#1,267 4 comments 1 reaction 0 assignees View on GitHub
status: accepting prs type: feature
Dominant language
TypeScript
Stars
1.4k
Forks
89
Avg merge
1m
Merged PRs (30d)
3

Description

### Bug Report Checklist

- [X] I have tried restarting my IDE and the issue persists.
- [X] I have pulled the latest `main` branch of the repository.
- [X] I have [searched for related issues](https://github.com/JoshuaKGoldberg/create-typescript-app/issues?q=is%3Aissue) and found none that matched my issue.

### Overview

I often see functions like:

```ts
function doSomething() {
if (someCondition) {
const someOtherValue = getSomething();
doSomethingWithSomeOtherValue();
}
}
```

If a function consists entirely of an `if` and that `if` contains a declaration, I'd generally consider it cleaner to instead invert the if to reduce nesting:

```ts
function doSomething() {
if (!someCondition) {
return;
}

const someOtherValue = getSomething();
doSomethingWithSomeOtherValue();
}
```

Let's find or create an ESLint plugin to do this.

### Additional Info

https://stackoverflow.com/questions/268132/invert-if-statement-to-reduce-nesting goes into the reasoning for this nicely.

This was proposed to ESLint in https://github.com/eslint/eslint/issues/10120 but never ratified.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.