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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With