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.

guard-for-in is unable to detect if statement guarding loop

See original GitHub issue

Tell us about your environment

  • ESLint Version: 5.16.0
  • Node Version: 10.16.0
  • npm Version: 6.9.0 What parser (default, Babel-ESLint, etc.) are you using?

Please show your full configuration:

module.exports = {
  env: {
    browser: true,
    es6: true,
  extends: [
  globals: {
    Atomics: 'readonly',
    SharedArrayBuffer: 'readonly',
  parserOptions: {
    ecmaVersion: 2018,
    sourceType: 'module',
  rules: {
    'no-continue': 0,
    'no-plusplus': 0,
    'no-unused-expressions': [
        'allowShortCircuit': true, // 允许 短路求值
        'allowTernary': true // 允许 三目运算符

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

class FormValidator extends Schema {
  constructor(formEl, rule, cb) {
    const r = { ...defaultRule, ...rule };

    cb && (this.cb = cb);

    const form = document.querySelector(formEl);
    formItems = form.querySelectorAll('[data-prop]');
    // 绑定事件
    for (const field in r) {
      if (!, field)) continue;
      let rules = r[field];
      rules = rules instanceof Array ? rules : [rules];
      const triggerRules = rules.filter(item => {
        return item.trigger !== void 0;
      if (triggerRules.length === 0) continue;
      for (let i = 0, len = triggerRules.length; i < len; i++) {
        const tr = triggerRules[i];
        const curEl = [...formItems].find(item => {
          return item.dataset.prop === field;
        curEl.addEventListener(tr.trigger, ev => {
          const { value } =;
          const model = {
            [field]: value,
          let s = new Schema({
            [field]: tr,
          s.validate(model, (err, fields) => {
            this.finalCallback(err, fields);
          s = null;

What did you expect to happen?

No error

What actually happened? Please include the actual, raw output from ESLint. for…in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array

Are you willing to submit a pull request to fix this bug?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

Yangfan2016commented, Jul 1, 2019

thx, It works @kaicataldo

kaicataldocommented, Jul 1, 2019

Here it is. If you believe there is a bug in the rule, feel free to make an issue on that repo. That being said, it looks like the intention here is to not allow that syntax at all, so I believe it’s working correctly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

guard-for-in doesn't recognize that Object.entries() is returning ...
Looping over objects with a for in loop will include properties that are inherited through the prototype chain. This behavior can lead to ......
Read more >
Function throwing guard-for-in error - Stack Overflow
hasOwnProperty(key) this check if the property/key exists in the object ... now says: loops iterate over the entire prototype chain, ...
Read more >
guard-for-in - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
"" loops should filter properties before acting on them - Jira
The statement allows you to loop through the names of all of the properties of an object. The list of properties includes...
Read more >
The body of a for in should be wrapped in an if statement to ...
In ESLint the rule that generates this warning is named guard-for-in . You can disable it by setting it to 0 , or...
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 Post

No results found

github_iconTop Related Hashnode Post

No results found