JoshuaKGoldberg / JoshuaKGoldberg/create-typescript-app
🚀 Feature: Find or create a lint rule to prefer inverting a single 'if' to reduce nesting
- 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
Assessment
This issue has not been assessed yet.