Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can For-In loop result an undefined or null?

This is my code:

const a = function(obj) {
  for (let key in obj) {
    if (!obj.hasOwnProperty(key)) {
      continue;
    }
    console.info(key.split('_'));
  }
};
a({a_b: 123});

I thought there is no problem at all but SonarQube gives me a critical error:

TypeError can be thrown as "key" might be null or undefined here.

The word key in key.split('_') is highlighted. Indicating variable key can be undefined/null here.

I tried to pass in something like {[undefined]: 123}, and the variable key becomes a string "undefined" instead of real undefined.

Hence. I am wondering will the key be undefined/null in any possible situation? Or is it just a False Positive?

Here is a screenshot:

Picture

like image 867
SCLeo Avatar asked Nov 03 '16 01:11

SCLeo


1 Answers

That's a known bug in the SonarQube JavaScript analyser which was fixed a few months ago. You should upgrade to the latest version of the JavaScript plugin.

like image 107
Pierre-Yves Avatar answered Sep 29 '22 22:09

Pierre-Yves