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

Move all var declarations to the top of the function and unable to continue.

Expand Messages
  • Luke Page
    Is it purposeful or a bug that when the onevar option is off it is complaining about vars needing to be moved to the top of the function if they appear in a
    Message 1 of 4 , Jan 10, 2011
    • 0 Attachment
      Is it purposeful or a bug that when the onevar option is off it is
      complaining about vars needing to be moved to the top of the function if
      they appear in a for loop declaration, but not if they don't. e.g.


      var func = function() {
      while(true) {
      alert("test 1");
      }
      var i = 0; // no error
      i++;
      };

      var func = function() {
      while(true) {
      alert("test 2");
      }
      for(var i = 0; i < 9; i++) { //line 13 - error
      alert("test3");
      }
      };

      *Error:*

      Problem at line 13 character 7: Move all 'var' declarations to the top of
      the function.

      for(var i = 0; i < 9; i++) {

      Problem at line 13 character 7: Stopping, unable to continue. (81% scanned).

      If it is now a problem to use var in a for declaration, could the error be
      more specific?

      Also JSLint now seems to be stopping on a lot of errors rather than
      continuing so more than one error can be fixed at once.Is it possible to
      change JSLint to the way it used to work, so that if(a) b; (e.g. missing
      curly brackets) is not a stopping error?


      [Non-text portions of this message have been removed]
    • Douglas Crockford
      JavaScript does not have block scope. It only has function scope. So it is wise to always declare at the top. Insert the braces, then it won t stop.
      Message 2 of 4 , Jan 10, 2011
      • 0 Attachment
        JavaScript does not have block scope. It only has function scope. So it
        is wise to always declare at the top.

        Insert the braces, then it won't stop.

        On 1/10/2011 1:27 AM, Luke Page wrote:
        > Is it purposeful or a bug that when the onevar option is off it is
        > complaining about vars needing to be moved to the top of the function if
        > they appear in a for loop declaration, but not if they don't. e.g.
        >
        >
        > var func = function() {
        > while(true) {
        > alert("test 1");
        > }
        > var i = 0; // no error
        > i++;
        > };
        >
        > var func = function() {
        > while(true) {
        > alert("test 2");
        > }
        > for(var i = 0; i< 9; i++) { //line 13 - error
        > alert("test3");
        > }
        > };
        >
        > *Error:*
        >
        > Problem at line 13 character 7: Move all 'var' declarations to the top of
        > the function.
        >
        > for(var i = 0; i< 9; i++) {
        >
        > Problem at line 13 character 7: Stopping, unable to continue. (81% scanned).
        >
        > If it is now a problem to use var in a for declaration, could the error be
        > more specific?
        >
        > Also JSLint now seems to be stopping on a lot of errors rather than
        > continuing so more than one error can be fixed at once.Is it possible to
        > change JSLint to the way it used to work, so that if(a) b; (e.g. missing
        > curly brackets) is not a stopping error?
        >
        >
        > [Non-text portions of this message have been removed]
        >
        >
        >
        > ------------------------------------
        >
        > Yahoo! Groups Links
        >
        >
        >
        >
      • Luke Page
        var scope: I agree, but in that case, please could it be consistent and report errors in both cases or else make the error more specific in order to aid people
        Message 3 of 4 , Jan 10, 2011
        • 0 Attachment
          var scope: I agree, but in that case, please could it be consistent and
          report errors in both cases or else make the error more specific in order to
          aid people in changing bad code

          braces: The problem is that re: the recent discussion about how to fix "a
          lot" of errors (say 300 per file over 150 files) in a editor, JSLint must be
          run per error corrected rather than being able to fix more than a few at
          once.

          On 10 January 2011 14:16, Douglas Crockford <douglas@...> wrote:

          >
          >
          > JavaScript does not have block scope. It only has function scope. So it
          > is wise to always declare at the top.
          >
          > Insert the braces, then it won't stop.
          >
          >
          > On 1/10/2011 1:27 AM, Luke Page wrote:
          > > Is it purposeful or a bug that when the onevar option is off it is
          > > complaining about vars needing to be moved to the top of the function if
          > > they appear in a for loop declaration, but not if they don't. e.g.
          > >
          > >
          > > var func = function() {
          > > while(true) {
          > > alert("test 1");
          > > }
          > > var i = 0; // no error
          > > i++;
          > > };
          > >
          > > var func = function() {
          > > while(true) {
          > > alert("test 2");
          > > }
          > > for(var i = 0; i< 9; i++) { //line 13 - error
          > > alert("test3");
          > > }
          > > };
          > >
          > > *Error:*
          > >
          > > Problem at line 13 character 7: Move all 'var' declarations to the top of
          > > the function.
          > >
          > > for(var i = 0; i< 9; i++) {
          > >
          > > Problem at line 13 character 7: Stopping, unable to continue. (81%
          > scanned).
          > >
          > > If it is now a problem to use var in a for declaration, could the error
          > be
          > > more specific?
          > >
          > > Also JSLint now seems to be stopping on a lot of errors rather than
          > > continuing so more than one error can be fixed at once.Is it possible to
          > > change JSLint to the way it used to work, so that if(a) b; (e.g. missing
          > > curly brackets) is not a stopping error?
          > >
          > >
          > > [Non-text portions of this message have been removed]
          > >
          > >
          > >
          > > ------------------------------------
          > >
          > > Yahoo! Groups Links
          > >
          > >
          > >
          > >
          >
          >
          >


          [Non-text portions of this message have been removed]
        • Praveenkumar
          If I may add to this conversation... Would it be possible to provide an option to tolerate this error?: Move all var declarations to the top of the
          Message 4 of 4 , Jan 10, 2011
          • 0 Attachment
            If I may add to this conversation...

            Would it be possible to provide an option to tolerate this error?:
            "Move all 'var' declarations to the top of the function."

            I made it a habit to run all my code (thousands of lines of code) through JSLint before checking them into our project repository to make sure that grave errors are absolutely avoided (for example: undefined variables).

            I would absolutely want to move all var declarations to the top of the function and in fact use only one var per function, but since my project is quite large I want to undertake that task on a clear sunny day when there aren't any deadline clouds in the vicinity.

            With the recent update to JSLint, I get a lot of instances of the above mentioned error in my project that stops further scanning as well. Right now, I'm stopped dead on my track unable to identify other more critical errors without an option to tolerate this error.

            Btw, I absolutely _love_ the inclusion of syntax tree output.

            Thanks
            Praveen.



            --- In jslint_com@yahoogroups.com, Luke Page <luke.a.page@...> wrote:
            >
            > var scope: I agree, but in that case, please could it be consistent and
            > report errors in both cases or else make the error more specific in order to
            > aid people in changing bad code
            >
            > braces: The problem is that re: the recent discussion about how to fix "a
            > lot" of errors (say 300 per file over 150 files) in a editor, JSLint must be
            > run per error corrected rather than being able to fix more than a few at
            > once.
            >
            > On 10 January 2011 14:16, Douglas Crockford <douglas@...> wrote:
            >
            > >
            > >
            > > JavaScript does not have block scope. It only has function scope. So it
            > > is wise to always declare at the top.
            > >
            > > Insert the braces, then it won't stop.
            > >
            > >
            > > On 1/10/2011 1:27 AM, Luke Page wrote:
            > > > Is it purposeful or a bug that when the onevar option is off it is
            > > > complaining about vars needing to be moved to the top of the function if
            > > > they appear in a for loop declaration, but not if they don't. e.g.
            > > >
            > > >
            > > > var func = function() {
            > > > while(true) {
            > > > alert("test 1");
            > > > }
            > > > var i = 0; // no error
            > > > i++;
            > > > };
            > > >
            > > > var func = function() {
            > > > while(true) {
            > > > alert("test 2");
            > > > }
            > > > for(var i = 0; i< 9; i++) { //line 13 - error
            > > > alert("test3");
            > > > }
            > > > };
            > > >
            > > > *Error:*
            > > >
            > > > Problem at line 13 character 7: Move all 'var' declarations to the top of
            > > > the function.
            > > >
            > > > for(var i = 0; i< 9; i++) {
            > > >
            > > > Problem at line 13 character 7: Stopping, unable to continue. (81%
            > > scanned).
            > > >
            > > > If it is now a problem to use var in a for declaration, could the error
            > > be
            > > > more specific?
            > > >
            > > > Also JSLint now seems to be stopping on a lot of errors rather than
            > > > continuing so more than one error can be fixed at once.Is it possible to
            > > > change JSLint to the way it used to work, so that if(a) b; (e.g. missing
            > > > curly brackets) is not a stopping error?
            > > >
            > > >
            > > > [Non-text portions of this message have been removed]
            > > >
            > > >
            > > >
            > > > ------------------------------------
            > > >
            > > > Yahoo! Groups Links
            > > >
            > > >
            > > >
            > > >
            > >
            > >
            > >
            >
            >
            > [Non-text portions of this message have been removed]
            >
          Your message has been successfully submitted and would be delivered to recipients shortly.