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

feat: Treat Class/New Expressions as truthy in no-constant-condition #15326

Merged

Conversation

captbaritone
Copy link
Contributor

In working on #15296 I discovered a few constant expressions that that were not covered in no-constant-condition, so I figured I'd propose adding them here.

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[X] Changes an existing rule (template)

What changes did you make? (Give an overview)

In working on #15296 I discovered a few constant expressions that that were not covered in no-constant-condition, so I figured I'd propose adding them here. This change add the ability to detect ClassExpression and NewExpressions as always truthy. I especially like that it catches the unintuitive fact that boxed primitives are always truthy.

These will now report errors:

if(new Foo()) {
  // ...
}

if(new Number(x)) {
  // ...
}

if(class {}) {
  // ...
}

Is there anything you'd like reviewers to focus on?

There's some overlap here with no-new-wrappers but I think it's sensible since this detects an actual bug that results from primitive boxing.

@eslint-github-bot eslint-github-bot bot added the triage An ESLint team member will look at this issue soon label Nov 17, 2021
@eslint-github-bot
Copy link

Hi @captbaritone!, thanks for the Pull Request

The first commit message isn't properly formatted. We ask that you update the message to match this format, as we use it to generate changelogs and automate releases.

  • The length of the commit message must be less than or equal to 72

Read more about contributing to ESLint here

@eslint-github-bot
Copy link

Hi @captbaritone!, thanks for the Pull Request

The first commit message isn't properly formatted. We ask that you update the message to match this format, as we use it to generate changelogs and automate releases.

  • The length of the commit message must be less than or equal to 72

Read more about contributing to ESLint here

@captbaritone captbaritone changed the title feat: Treat Class & New Expressions as always truthy in no-constant-condition feat: Treat Class/New Expressions as truthy in no-constant-condition Nov 17, 2021
@nzakas nzakas added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Nov 18, 2021
Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me. Can you also update the docs?

@captbaritone
Copy link
Contributor Author

I've added an example of each error to the docs. The class expression seems obscure enough that it might not be worth inclusion. Happy to remove it if others feel the same way.

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks.

lib/rules/no-constant-condition.js Outdated Show resolved Hide resolved
@captbaritone
Copy link
Contributor Author

I believe all comments have been addressed.

Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@mdjermanovic mdjermanovic added the accepted There is consensus among the team that this change meets the criteria for inclusion label Dec 2, 2021
@mdjermanovic mdjermanovic merged commit d041f34 into eslint:main Dec 2, 2021
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Jun 1, 2022
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Jun 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants