Search code examples
javascriptsyntaxoperatorslegacy-code

JavaScript syntax advice: Invalid left hand side in prefix expression when auto formatting


I inherited a codebase aka "legacy code" and I am very careful not to break anything. I stumbled upon an error while auto-formatting.

The purpose of the function is to sort a chart. I commented the portion I do not understand:

function sortChartBy(field,dir=1) {
    if(g_sortMethod != field){
        g_sortDir = 1;
    }
    g_sortMethod = field;
    g_sortDir = g_sortDir * -1 * dir;
    
    var ranks = g_chart["ranks"];
    var sortMethod = g_sortMethod;
    var sortDir = g_sortDir;
    
    var sortFunc = undefined;
    if(field == "release_date"){
        sortFunc = function(a,b){
            var aVal = a[sortMethod];
            var bVal = b[sortMethod];
            if(aVal == "1970-01-01") aVal = "9999--99-99";
            if(bVal == "1970-01-01") bVal = "9999-99-99";
            if(aVal < bVal) return -1 * sortDir;
            if(aVal > bVal) return  1 * sortDir;
            return 0;
        };
    }else{
        sortFunc = function(a,b){
      var aVal = a[sortMethod];
      var bVal = b[sortMethod];

      // ###### QUESTION HERE ########################################
      if (aVal == -1 && bVal !=- -1) return 1; 
      // 'bVal !=- -1' What does that mean? Why '-' directly after '!='

      if (aVal != -1 && bVal == -1) return -1;
      if (aVal < bVal) return -1 * sortDir;
      if (aVal > bVal) return  1 * sortDir;
      return 0;
        };
    }
    
    ranks.sort(sortFunc);
    renderChart();
}

I use IntelliJ as an IDE. When I use auto format the '-' minus character after the '!=' moves one character to the right showing the line as

if (aVal == -1 && bVal != --1) return 1; // Invalid left hand side in prefix expression

Now Intellij shows an error stating Invalid left hand side in prefix expression and marking the '1' with the red error underline.

I don't understand why there are two minuses seperated with a space !=- -1 in the original code and why Intellij formats this into an error. The code seems to work fine in the original code. I have never seen !=- in JavaScript. To me this does not seem to be valid.

Please explain why the line if (aVal == -1 && bVal !=- -1) return 1; seems to work fine, but then gets autoformatted into an error.

What should the correct code look like?


Solution

  • Please explain why the line if (aVal == -1 && bVal !=- -1) return 1; seems to work fine, but then gets autoformatted into an error.

    The formatter is indeed breaking that code, but I suspect it was already broken. :-) But the formatter is violating a fundamental principle: It's chaning the meaning of the code (in this case, from valid-but-probably-wrong syntax to invalid syntax :-D ).

    Thanks to Mellet mentioned running it through a different formatter, my afternoon-sleepy brain understands why the original parses: It's the unary - operator being used twice.

    So the code is != - -1 which is != -(-1) which is != 1.

    But it's probably a typo and the check was probably supposed to be against -1, not 1.