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

JSLint forin option in scripts

Expand Messages
  • pauanyu
    There are times where you absolutely must go against something JSLint is warning you about. Perhaps you need eval (rare, but it happens), or perhaps you have a
    Message 1 of 5 , Aug 31, 2009
    • 0 Attachment
      There are times where you absolutely must go against something JSLint is warning you about. Perhaps you need eval (rare, but it happens), or perhaps you have a function that loops over an object's properties, including those in the prototype chain.


      "use strict";
      var prop, foo;

      /*jslint forin:true */
      for (prop in foo) {
      foo[prop] = "foo";
      }
      /*jslint forin:false */


      We might have multiple for-in loops, and we only want to disable warnings on this particular loop. So, we enable the "forin" option, so JSLint doesn't whine about it, and afterwards we set it back to false. It should work, right?

      However, it fails with the following error:

      Problem at line 5 character 1: The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype.
    • douglascrockford
      ... I didn t intend for /*jslint*/ to be used this way. Mostly it just seems to work. In this case, there was an interaction with the token lookahead and block
      Message 2 of 5 , Aug 31, 2009
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "pauanyu" <pcxunlimited@...> wrote:
        >
        > There are times where you absolutely must go against something JSLint is warning you about. Perhaps you need eval (rare, but it happens), or perhaps you have a function that loops over an object's properties, including those in the prototype chain.
        >
        >
        > "use strict";
        > var prop, foo;
        >
        > /*jslint forin:true */
        > for (prop in foo) {
        > foo[prop] = "foo";
        > }
        > /*jslint forin:false */
        >
        >
        > We might have multiple for-in loops, and we only want to disable warnings on this particular loop. So, we enable the "forin" option, so JSLint doesn't whine about it, and afterwards we set it back to false. It should work, right?
        >
        > However, it fails with the following error:
        >
        > Problem at line 5 character 1: The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype.


        I didn't intend for /*jslint*/ to be used this way. Mostly it just seems to work. In this case, there was an interaction with the token lookahead and block processing.

        Let me know if you find any more cases of this.
      • Arthur Blake
        don t you have the options reversed? That is, shouldn t your code be: use strict ; var prop, foo; /*jslint forin:false */ for (prop in foo) { foo[prop] =
        Message 3 of 5 , Aug 31, 2009
        • 0 Attachment
          don't you have the options reversed? That is, shouldn't your code be:

          "use strict";
          var prop, foo;

          /*jslint forin:false */
          for (prop in foo) {
          foo[prop] = "foo";
          }
          /*jslint forin:true */

          ?

          On Mon, Aug 31, 2009 at 11:58 AM, pauanyu<pcxunlimited@...> wrote:
          >
          >
          > There are times where you absolutely must go against something JSLint is
          > warning you about. Perhaps you need eval (rare, but it happens), or perhaps
          > you have a function that loops over an object's properties, including those
          > in the prototype chain.
          >
          > "use strict";
          > var prop, foo;
          >
          > /*jslint forin:true */
          > for (prop in foo) {
          > foo[prop] = "foo";
          > }
          > /*jslint forin:false */
          >
          > We might have multiple for-in loops, and we only want to disable warnings on
          > this particular loop. So, we enable the "forin" option, so JSLint doesn't
          > whine about it, and afterwards we set it back to false. It should work,
          > right?
          >
          > However, it fails with the following error:
          >
          > Problem at line 5 character 1: The body of a for in should be wrapped in an
          > if statement to filter unwanted properties from the prototype.
          >
          >
        • pauanyu
          ... Nope. I very carefully read the documentation. It says, true if unfiltered for in statements should be allowed.
          Message 4 of 5 , Aug 31, 2009
          • 0 Attachment
            --- In jslint_com@yahoogroups.com, Arthur Blake <arthur.blake@...> wrote:
            >
            > don't you have the options reversed? That is, shouldn't your code be:
            >
            > "use strict";
            > var prop, foo;
            >
            > /*jslint forin:false */
            > for (prop in foo) {
            > foo[prop] = "foo";
            > }
            > /*jslint forin:true */
            >
            > ?
            >

            Nope. I very carefully read the documentation. It says, "true if unfiltered for in statements should be allowed."
          • woomla
            ... I always use /*jslint*/ this way. I think it is a great way to parse multiple files or one file with several scripts from different people in it. Mostly I
            Message 5 of 5 , Sep 1, 2009
            • 0 Attachment
              --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@...> wrote:
              >

              > >
              > > "use strict";
              > > var prop, foo;
              > >
              > > /*jslint forin:true */
              > > for (prop in foo) {
              > > foo[prop] = "foo";
              > > }
              > > /*jslint forin:false */
              > >

              >
              >
              > I didn't intend for /*jslint*/ to be used this way. Mostly it just seems to work. In this case, there was an interaction with the token lookahead and block processing.
              >
              > Let me know if you find any more cases of this.
              >

              I always use /*jslint*/ this way. I think it is a great way to parse multiple files or one file with several scripts from different people in it. Mostly I use it to set indent.

              My work around is to put the /*jslint forin:false */ line after the next statement (or maybe even further away). That takes care of the lookahead issue. It would be great if there will be a fix for that.

              A small issue with that is that when changing a property and resetting it, you basically don't know the previous state. By trial and error you can figure it out but a nice solution would be to possibility to restore a state. (I'll post a new message for this).
            Your message has been successfully submitted and would be delivered to recipients shortly.