Loading ...
Sorry, an error occurred while loading the content.

Mysterious and confusing error message on encountering destructuring assignment

Expand Messages
  • silver_7402
    If a script is entered into JSLint.com[1] containing a destructuring assignment[2], JSLint gives a strange error message[3] that has little or nothing to do
    Message 1 of 3 , Sep 9, 2009
    • 0 Attachment
      If a script is entered into JSLint.com[1] containing a destructuring assignment[2], JSLint gives a strange error message[3] that has little or nothing to do with the actual cause of the error, then dies -- i.e. doesn't produce any more results. The message appears to indicate a problem in the script at that position, but is I think actually an error message from JSLint itself.

      I guess I'd like to see one of two things: either JSLint supporting this assignment syntax (nice, but will take a while, probably); or JSLint at any rate not crashing on it.

      Test script:
      /*jslint white: true, onevar: true, browser: true, undef: true, eqeqeq: true, indent: 2 */
      /*globals window */
      /*members alert, ceil, random */

      function fib(n) {
      var p1 = 1, p2 = 1, i;

      for (i = 0; i < n; i++) {
      // Destructuring assignment (see e.g. https://developer.mozilla.org/en/New_in_JavaScript_1.7#Destructuring_assignment)
      [p2, p1] = [p1, p2 + p1];
      }
      return p2;
      }

      function echo(s) {
      if (window && typeof window.alert === "function") {
      window.alert(s);
      }
      else if (WScript && typeof WScript.Echo === "function") {
      WScript.Echo(s);
      } else { /* Oh well! */ }
      }

      var n = Math.ceil(Math.random() * 100);
      echo("fib(" + n + ") = " + fib(n));



      [1] Tested with 2009-08-31 edition.
      [2] See e.g. https://developer.mozilla.org/en/New_in_JavaScript_1.7#Destructuring_assignment
      [3] Specifically, "left.left is undefined", presented as though a standard problem message corresponding to a flaw in the script.
    • douglascrockford
      ... Thanks. JSLint now reports Bad assignment.
      Message 2 of 3 , Sep 10, 2009
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "silver_7402" <silver_7402@...> wrote:
        >
        > If a script is entered into JSLint.com[1] containing a destructuring assignment[2], JSLint gives a strange error message[3] that has little or nothing to do with the actual cause of the error, then dies -- i.e. doesn't produce any more results. The message appears to indicate a problem in the script at that position, but is I think actually an error message from JSLint itself.
        >
        > I guess I'd like to see one of two things: either JSLint supporting this assignment syntax (nice, but will take a while, probably); or JSLint at any rate not crashing on it.
        > [p2, p1] = [p1, p2 + p1];


        Thanks. JSLint now reports "Bad assignment."
      • silver_7402
        ... Ah, much better. Now I can actually check the rest of the script.... Many thanks.
        Message 3 of 3 , Sep 11, 2009
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@...> wrote:
          > Thanks. JSLint now reports "Bad assignment."
          >
          Ah, much better. Now I can actually check the rest of the script.... Many thanks.
        Your message has been successfully submitted and would be delivered to recipients shortly.