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

Re: [jslint] Re: "Too many var statements"

Expand Messages
  • Stefan Weiss
    ... That s not what this option does (and there is no block scope in JavaScript). ... The onevar option doesn t check for this at all. The following code
    Message 1 of 6 , Aug 27, 2009
    • 0 Attachment
      On 27/08/09 20:09, aseem.kishore@... wrote:
      > What I'm not seeing is why the check for incorrectly-block-scoped
      > vars needs to be tied to the check for one var per function.

      That's not what this option does (and there is no block scope in
      JavaScript).

      > When I uncheck the onevar option and enter this:
      >
      > function hello() {
      > var x = 1;
      > var y = 2;
      >
      > if (y > x) {
      > var z = 3;
      > x += z;
      > }
      >
      > return x + y;
      > }
      >
      > It passes, even though "var z" inside the if {} block is clearly misleading.

      The "onevar" option doesn't check for this at all. The following code
      would also pass, with or without the option enabled:

      function hello() {
      if (false) {
      var z = 3;
      }
      return z;
      }

      Technically, there's nothing wrong with the "var z". What may be
      confusing is that z will be declared regardless of the value in the
      if-condition, but the assignment will only be evaluated if the condition
      is true.

      I find the onevar option to be useful in some cases. For example, the
      YUI Compressor tool will issue a warning if it finds more than one var
      statement in a scope. JSLint's onevar option lets me avoid these warnings.


      regards,
      Stefan
    Your message has been successfully submitted and would be delivered to recipients shortly.