question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Follow identifiers to their declaration in `no-constant-condition`

See original GitHub issue
  • 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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:6
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
btmillscommented, 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.

1reaction
nzakascommented, Nov 10, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Should we extend no-constant-condition to catch comparisons ...
The point of the no-constant-condition rule is to watch for cases ... Follow identifiers to their declaration in no-constant-condition # ...
Read more >
Integration with ESLint JavaScript Linter - Codacy | Blog
A popular linter for the JavaScript community, ESL Lint is now supported by Codacy static analysis tool making it very easy to write...
Read more >
Identifier, Variable, and Constants
Identifiers are used for the naming of variable, function, class, structure or a ... Take a look at this: int age; This is...
Read more >
Declarators and variable declarations - Microsoft Learn
When a declarator consists of an unmodified identifier, the item being declared has a base type. If an asterisk ( * ) appears...
Read more >
Chapter 4. Basic Declarations and Expressions - O'Reilly
Some names like int, while, for, and float have a special meaning to C and are considered reserved words. They cannot be used...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found