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

Follow identifiers to their declaration in no-constant-condition #13776

Open
captbaritone opened this issue Oct 20, 2020 · 11 comments
Open

Follow identifiers to their declaration in no-constant-condition #13776

captbaritone opened this issue Oct 20, 2020 · 11 comments
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules

Comments

@captbaritone
Copy link
Contributor

  • What rule do you want to change? no-constant-condition
  • Does this change cause the rule to produce more or fewer warnings?: More
    How will the change be implemented? (New option, new default behavior, etc.)?: New default behavior

Please provide some example code that this change will affect:

var foo = true;
if(foo) {}
  • What does the rule currently do for this code?: Nothing
  • What will the rule do after it's changed?: Warn/error
  • Are you willing to submit a pull request to implement this change?: Possibly

This proposal has been split out of #13752

Currently no-constant-condition triggers on if(true){} but not on const foo = true; if(foo){}. In this case we could use the ScopeManager to attempt to follow foo to its declaration/assignment. If the variable is in scope and only assigned once, then we could check if the assigned value is constant.

In addition to assignments, we could also check other types of declarations. For example a function declaration could trigger an error:

function foo() {}

if(foo){} // <= foo is always truthy here

Additionally, @mdjermanovic pointed out that there may be other rules which could be employing a similar technique.

I've done a simple version of this for a rule I wrote (as mentioned in #13752) which could be used as a starting place.

Shout out to @bradzacher who first suggested this approach while we were iterating on my no-useless-null-checks rule.

@captbaritone captbaritone added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules triage An ESLint team member will look at this issue soon labels Oct 20, 2020
@nzakas nzakas added evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Oct 31, 2020
@nzakas
Copy link
Member

nzakas commented Oct 31, 2020

An interesting idea for sure. As long as we don’t go too far down this path (evaluating expressions rather than just checking for single truthy values), I’m in favor.

@nzakas nzakas added the tsc agenda This issue will be discussed by ESLint's TSC at the next meeting label Nov 17, 2020
@nzakas
Copy link
Member

nzakas commented Nov 17, 2020

TSC Summary: This proposal seeks to have no-constant-condition follow the value of identifiers and consider that value as part of it's evaluation. This would allow us to capture more errors when identifiers are assigned a value once and then used in a condition. Optionally, it might make sense to build this into eslint-scope to allow other rules to use the same functionality.

TSC Question: Shall we accept this proposal?

@btmills btmills added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion tsc agenda This issue will be discussed by ESLint's TSC at the next meeting labels Nov 20, 2020
@btmills
Copy link
Member

btmills commented Nov 20, 2020

Marking this as accepted after discussion in today's TSC meeting. Since there are some complex parts here, we'd like to discuss the implementation details in a new RFC before starting work on a PR. It should help us decide where we draw the line on the extended checks and whether the implementation belongs in this rule or eslint-scope.

@nzakas nzakas added this to Planned in Public Roadmap via automation Nov 24, 2020
@nzakas nzakas moved this from Planned to Needs RFC in Public Roadmap Nov 24, 2020
@nzakas nzakas added this to Needs Triage in Triage via automation Feb 18, 2021
@nzakas nzakas moved this from Needs Triage to RFC Opened in Triage Feb 18, 2021
@nzakas nzakas moved this from RFC Opened to Waiting for RFC in Triage Feb 18, 2021
@github-actions
Copy link

Oops! It looks like we lost track of this issue. @eslint/eslint-tsc what do we want to do here? This issue will auto-close in 7 days without an update.

@github-actions github-actions bot added the Stale label Oct 15, 2021
@nzakas
Copy link
Member

nzakas commented Oct 16, 2021

Just waiting for an RFC here

@github-actions github-actions bot removed the Stale label Oct 16, 2021
@captbaritone
Copy link
Contributor Author

I can work on an RFC for this once I finish making a PR for #13752.

@Gautam-Arora24
Copy link
Contributor

I would like to work on this issue.

@nzakas
Copy link
Member

nzakas commented Nov 10, 2021

@Gautam-Arora24 awesome! The next step here is to create an RFC.

@Gautam-Arora24
Copy link
Contributor

Sure!

@captbaritone
Copy link
Contributor Author

@Gautam-Arora24 If you're still working on this, it would be worth including the proposed new no-constant-binary-expression rule (#15296) in the proposal as well since it would similarly benefit from following identifiers to their definition. I made some changes to the proposed rule for our local fork of it and, anecdotally, it detects on the order of twice as many issues.

@sam3k
Copy link
Contributor

sam3k commented Mar 8, 2023

@Gautam-Arora24 do you still have interest in contributing a RFC?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
Public Roadmap
  
Needs RFC
Status: Waiting for RFC
Triage
Waiting for RFC
Development

No branches or pull requests

5 participants