Skip to content

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

Open
@captbaritone

Description

  • 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.

Activity

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

Metadata

Assignees

No one assigned

    Labels

    acceptedThere is consensus among the team that this change meets the criteria for inclusionenhancementThis change enhances an existing feature of ESLintruleRelates to ESLint's core rules

    Type

    No type

    Projects

    • Status

      Waiting for RFC

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions