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.

Allow variable declarations with guard-for-in

See original GitHub issue

What rule do you want to change? guard-for-in

Does this change cause the rule to produce more or fewer warnings? Fewer.

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:

for (key in foo) {
    const val = checkSomething(key)
    if (val > 0) {
        doSomething(key, val);
    }
}

What does the rule currently do for this code? Warns.

What will the rule do after it’s changed? Not warn.


The point is that the condition(s) that you check to filter properties may involve values that are also needed inside the loop body. It would be good to allow this without having to disable or work around the warning. I.e. allow let and const statements before the if.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
not-an-aardvarkcommented, Oct 19, 2016

Thanks for the suggestion.

I’m not sure about this change; I think the point of the guard-for-in rule is to enforce the following pattern:

for (key in foo) {
  // Only do things if `key` is an own property of `foo`, rather than an inherited property.
  if (Object.prototype.hasOwnProperty.call(foo, key)) {
    // do logic, including const and let declarations, in this `if` statement
  }
}
0reactions
kaicataldocommented, Aug 8, 2017

Feel free to make a new issue with a proposal!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rules - ESLint - Pluggable JavaScript Linter
Disallow variable or `function` declarations in nested blocks ... guard-for-in ... Enforce a maximum number of statements allowed in function blocks.
Read more >
How to fix Definition for rule typescript-eslint no-use-before ...
I am new to eslint and I cannot figure out how to solve this issue. The beginning of my imports is always underlined...
Read more >
Function throwing eslint.org guard-for-in error - Stack Overflow
Using this for loop to build a querystring with no issues. for (const key in parameters) { const value = parameters[key]; qs += ......
Read more >
List of available rules - ESLint - Pluggable JavaScript linter
disallow variable or function declarations in nested blocks ... enforce a maximum cyclomatic complexity allowed in a program ... guard-for-in.
Read more >
Integration with ESLint JavaScript Linter - Codacy | Blog
Enable them on your projects and share your feedback with us! ... Variables. These rules have to do with variable declarations.
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