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

JSLint does not allow commas inside of arrays

Expand Messages
  • pauanyu
    var foo = [0,, 1,, 2,, 3]; There are times where you wish to leave particular indices of an array as undefined. The above fails with the following errors:
    Message 1 of 7 , Sep 5, 2009
    View Source
    • 0 Attachment
      var foo = [0,, 1,, 2,, 3];

      There are times where you wish to leave particular indices of an array as undefined. The above fails with the following errors:


      Problem at line 1 character 14: Missing space after ','.

      Problem at line 1 character 13: Extra comma.

      Problem at line 1 character 14: Expected an identifier and instead saw ','.

      Problem at line 1 character 14: Stopping, unable to continue. (100% scanned).
    • douglascrockford
      ... This is a hazard. It is indistinguishable from the accidental inclusion of an extra comma, which is a common typo. It also encourages the development of
      Message 2 of 7 , Sep 5, 2009
      View Source
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "pauanyu" <pcxunlimited@...> wrote:
        >
        > var foo = [0,, 1,, 2,, 3];
        >
        > There are times where you wish to leave particular indices of an
        > array as undefined.

        This is a hazard. It is indistinguishable from the accidental inclusion of an extra comma, which is a common typo. It also encourages the development of tricky positional array structures when an object would serve better.

        So I recommend that you explicitly include values between those commas.
      • pauanyu
        ... Fair enough. What bothers me far more is that it stops the test! So if there s any errors after the bad commas, you won t get to see them. I m okay with it
        Message 3 of 7 , Sep 5, 2009
        View Source
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@...> wrote:
          >
          > This is a hazard. It is indistinguishable from the accidental inclusion of an extra comma, which is a common typo. It also encourages the development of tricky positional array structures when an object would serve better.
          >
          > So I recommend that you explicitly include values between those commas.
          >

          Fair enough. What bothers me far more is that it stops the test! So if there's any errors after the bad commas, you won't get to see them. I'm okay with it throwing an error, but does it really need to completely halt any further processing?
        • Paul Novitski
          ... I imagine this was just a typo but just in case it wasn t I ll point out that it s not the indices here that are undefined, it s the values. The indices
          Message 4 of 7 , Sep 5, 2009
          View Source
          • 0 Attachment
            At 9/5/2009 05:42 AM, pauanyu wrote:
            >var foo = [0,, 1,, 2,, 3];
            >
            >There are times where you wish to leave particular indices of an
            >array as undefined.


            I imagine this was just a typo but just in case it wasn't I'll point
            out that it's not the indices here that are undefined, it's the
            values. The indices for the array above are consecutive integers 0-6.

            Regards,

            Paul
            __________________________

            Paul Novitski
            Juniper Webcraft Ltd.
            http://juniperwebcraft.com
          • douglascrockford
            ... JSLint will now continue after an extra comma in an array literal. But I still recommend that you fix your code.
            Message 5 of 7 , Sep 6, 2009
            View Source
            • 0 Attachment
              --- In jslint_com@yahoogroups.com, "pauanyu" <pcxunlimited@...> wrote:
              >
              > --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@> wrote:
              > >
              > > This is a hazard. It is indistinguishable from the accidental inclusion of an extra comma, which is a common typo. It also encourages the development of tricky positional array structures when an object would serve better.
              > >
              > > So I recommend that you explicitly include values between those commas.
              > >
              >
              > Fair enough. What bothers me far more is that it stops the test! So if there's any errors after the bad commas, you won't get to see them. I'm okay with it throwing an error, but does it really need to completely halt any further processing?


              JSLint will now continue after an extra comma in an array literal.
              But I still recommend that you fix your code.
            • pauanyu
              ... Thank you. Actually, I would never write such garrish code (normally). Allow me to explain my use case (and how I found out about this): I have a function
              Message 6 of 7 , Sep 7, 2009
              View Source
              • 0 Attachment
                --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@...> wrote:
                >
                > JSLint will now continue after an extra comma in an array literal.
                > But I still recommend that you fix your code.
                >

                Thank you.

                Actually, I would never write such garrish code (normally). Allow me to explain my use case (and how I found out about this):

                I have a function that converts a string into an array. For instance, the string "[0, 1, 2, 3, 4]" gets converted into the array [0, 1, 2, 3, 4].

                It needs to be able to handle a variety of situations; strings, booleans, numbers, etc. So I wrote a bunch of unit tests to verify that it is working properly. They have already helped me catch a couple regressions.

                The goal of the function was to convert the string as accurately as possible, so if you pass in the string "[,,,]" you should get back the array [,,,]. I had a unit test that verified that commas in the string would properly get converted to commas in the array.

                When running these unit tests in JSLint, I stumbled upon the error, hence this post.
              • pauanyu
                ... You are quite right. I meant to say There are times where you wish to leave the value of particular indices of an array as undefined.
                Message 7 of 7 , Sep 7, 2009
                View Source
                • 0 Attachment
                  --- In jslint_com@yahoogroups.com, Paul Novitski <paul@...> wrote:
                  >
                  > I imagine this was just a typo but just in case it wasn't I'll point
                  > out that it's not the indices here that are undefined, it's the
                  > values. The indices for the array above are consecutive integers 0-6.
                  >
                  > Regards,
                  >
                  > Paul
                  > __________________________
                  >
                  > Paul Novitski
                  > Juniper Webcraft Ltd.
                  > http://juniperwebcraft.com
                  >

                  You are quite right. I meant to say "There are times where you wish to leave the value of particular indices of an array as undefined."
                Your message has been successfully submitted and would be delivered to recipients shortly.