Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule proposal: Collapsible if-statements #819

Closed
sindresorhus opened this issue Sep 1, 2020 · 9 comments · Fixed by #936
Closed

Rule proposal: Collapsible if-statements #819

sindresorhus opened this issue Sep 1, 2020 · 9 comments · Fixed by #936

Comments

@sindresorhus
Copy link
Owner

The rule should identify nested if-statements that can be combined into one.

Would be useful if the rule could be auto-fixable.

Instead of:

if (foo) {
    if (bar) {
        // ...
    }
}

Prefer:

if (foo && bar) {
    // ...
}

From: typescript-eslint/typescript-eslint#279

@sindresorhus
Copy link
Owner Author

Accepted. PR welcome.

@fisker
Copy link
Collaborator

fisker commented Sep 27, 2020

Rule name? collapse-if?

@fregante
Copy link
Collaborator

fregante commented Oct 5, 2020

This can only work if it’s not followed by else

Can’t be flattened:

if (x) {
  if (y) {}
} else {}

Can be flattened

if (x) {
 
} else if (x) {
  if (y) {}
}

@fisker fisker self-assigned this Dec 14, 2020
@fisker
Copy link
Collaborator

fisker commented Dec 14, 2020

I think this comment make sense, let's name it as no-lonely-if too, hope it will merge to core someday.

@fregante
Copy link
Collaborator

Hasn’t eslint stopped adding rules to core?

@fisker
Copy link
Collaborator

fisker commented Dec 14, 2020

No, but @sindresorhus made this proposal here. I guess he want have this rule in unicorn first?

@fisker
Copy link
Collaborator

fisker commented Dec 14, 2020

Anyway, the rule is ready #936

@fregante
Copy link
Collaborator

According to their rule suggestion issue template:

New rules must be related to ECMAScript features added within the last 12 months

But I suppose this rule could still be accepted as a modification of the existing one.

@sindresorhus
Copy link
Owner Author

sindresorhus commented Dec 17, 2020

No, but @sindresorhus made this proposal here. I guess he want have this rule in unicorn first?

Yeah, I have given up bothering proposing stuff over at ESLint. The process there is way too slow and they reject almost everything anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants