Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

eslint error "guard-for-in" not clear how to work with for-in

I'm working with cucumber js and I want to fill out some fields in an application, so i'm using a for-in to get the data from the rowHash but i'm getting the error message "The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype guard-for-in" i'm not sure how I should code my for-in with the if inside the for.

this is my code:

this.fillRequiredfields = function(dataTable){
  var rows = dataTable.rowsHash();
  for (var row in rows) {
    var val = rows[row];

    if (row === 'firstname') {
      element(by.name('firstName')).sendKeys(val).isPresent();
    }
    if (row === 'lastname') {
      element(by.name('lastName')).sendKeys(val).isPresent();
    }
    if (row === 'emailaddress') {
      element(by.name('emailAddress')).sendKeys(val).isPresent();
    }
    if (row === 'displayname') {
      element(by.name('displayName')).sendKeys(val).isPresent();
    }
    if (row === 'password') {
      element(by.name('newPassword')).sendKeys(val).isPresent();
    }
  }
};

So when I try to do the commit in git i'm getting the "guard-for-in" from eslint. If somebody can explain me how I should do the if in the for-in that could be good.

Hope you can help me.

like image 856
burq24 Avatar asked Jan 11 '18 20:01

burq24


People also ask

How do you set rules in ESLint?

ESLint comes with a large number of built-in rules and you can add more rules through plugins. You can modify which rules your project uses either using configuration comments or configuration files. To change a rule setting, you must set the rule ID equal to one of these values: "off" or 0 - turn the rule off.

How many ESLint rules are there?

Keep in mind that we have over 200 rules, and that is daunting both for end users and the ESLint team (who has to maintain them).

Where can I find ESLint rules?

The current eslint:recommended rules can be found at github.com/eslint/eslint/blob/master/conf/eslint-recommended.js.

How do you ignore ESLint?

Ignore multiple files or folders To turn off ESLint in the whole file, you can add /* eslint-disable */ in the first line of that file. Alternatively, you can create a file . eslintignore in the root catalog.


2 Answers

I already solve this, in my case the solution was:

instead of

var val = rows[row];

I add the if with the hasOwnProperty(), like this:

if (rows.hasOwnProperty(row))

So the code is like this:

for (var row in rows) {
  if (rows.hasOwnProperty(row)){

    if (row === 'firstname') {
      element(by.name('firstName')).sendKeys(rows[row]).isPresent();
    }
    if (row === 'lastname') {
      element(by.name('lastName')).sendKeys(rows[row]).isPresent();
    }
    if (row === 'emailaddress') {
      element(by.name('emailAddress')).sendKeys(rows[row]).isPresent();
    }
    if (row === 'displayname') {
      element(by.name('displayName')).sendKeys(rows[row]).isPresent();
    }
    if (row === 'password') {
      element(by.name('newPassword')).sendKeys(rows[row]).isPresent();
    }
  }
}

Hope can help to somebody else.

like image 80
burq24 Avatar answered Sep 20 '22 22:09

burq24


In some rare cases

if (rows.hasOwnProperty(row))

might trigger undesired effect (for example if this is a function hasOwnProperty()). So this is why suggested method is

Object.prototype.hasOwnProperty.call(rows, row)
like image 36
Vans Avatar answered Sep 18 '22 22:09

Vans