TypeError can be thrown as "key" might be null or undefined here in SonarQube
Asked Answered
M

1

6

In JQuery Library jquery-1.4.1-vsdoc.js, Following is the code block :

jQuery.fn.extend({
data: function( key, value ) {

    if ( typeof key === "undefined" && this.length ) {
        return jQuery.data( this[0] );

    } else if ( typeof key === "object" ) {
        return this.each(function() {
            jQuery.data( this, key );
        });
    }

    var parts = key.split(".");
    parts[1] = parts[1] ? "." + parts[1] : "";

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.

Please suggest how to resolve this issue. SonarQube Build Number is Version 6.3 (build 19869)

Mandrel answered 26/7, 2017 at 11:21 Comment(0)
P
0

I have this issue previously, Here the issue is you are only checking for undefined.
You ave to check both undefined & null.
Like this

if( key !== undefined && key !== null){ 
  var parts = key.split(".");
  parts[1] = parts[1] ? "." + parts[1] : "";    
}

I think it will be helpful

Parasang answered 1/9, 2022 at 9:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.