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

Re: [jslint] Possible bug in reporting 'Bad for in variable'

Expand Messages
  • Jerry
    ... Sorry I forgot to mention that the code had been greatly hacked down. I was trying to get the minimal amount of code that would reproduce what I suspect is
    Message 1 of 8 , May 10, 2011
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, Erik Eckhardt <erik@...> wrote:
      >
      > Jerry,
      >
      > I don't have any thoughts on jslint's handling, but I did have a question
      > about your code (which I realize is probably hacked down so my comment may
      > be useless).
      >
      > Is there really a need to use prop in a closure in the saveBasicData
      > function? It seems better to me to make the function take two parameters
      > instead of using the outer-scope-level prop variable.
      >
      > Erik
      >
      > On Tue, May 10, 2011 at 9:17 AM, Jerry <jluke@...> wrote:
      >
      > >
      > >
      > > I saw this posted in a previous message, but the thread ended because the
      > > provided code sample did not reproduce the bug. I was able to produce the
      > > following code sample that reproduces the 'Bad for in variable' problem:
      > > =======================================================
      > > function badForInVar() {
      > > var basicData = { 'Status' : 'NORMAL' , 'Priority' : 'High' },
      > > prop;
      > >
      > > function saveBasicData(value){
      > > var lookup = {};
      > > lookup.displayName = value;
      > > lookup.type = prop;
      > > }
      > > for (prop in basicData) {
      > > if (basicData.hasOwnProperty(prop)) {
      > > saveBasicData(basicData[prop]);
      > > }
      > > }
      > > }
      > > =======================================================
      > > I believe the saveBasicData function declaration between the var
      > > declaration of 'prop' and its usage in the for/in loop is contributing to
      > > JSLint reporting the 'Bad for in variable' error. If I move the
      > > saveBasicData function to follow the for/in loop, I no longer get the error
      > > (but instead get the message: 'saveBasicData' was used before it was
      > > defined).
      > >
      > > I would very much appreciate any help in confirming whether or not this is
      > > a bug in JSLint, I am missing the underlying cause of the error, or a
      > > workaround to appease JSLint.
      > >
      > >
      > >
      >
      >
      > [Non-text portions of this message have been removed]
      >
      Sorry I forgot to mention that the code had been greatly hacked down. I was trying to get the minimal amount of code that would reproduce what I suspect is a problem with jslint reporting the 'Bad for in variable' error. Obviously, the remaining code does not actually do much of anything.
    • Jerry
      Message 2 of 8 , May 10, 2011
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "Jerry" <jluke@...> wrote:
        >
        >
        > --- In jslint_com@yahoogroups.com, Erik Eckhardt <erik@> wrote:
        > >
        > > Jerry,
        > >
        > > I don't have any thoughts on jslint's handling, but I did have a question
        > > about your code (which I realize is probably hacked down so my comment may
        > > be useless).
        > >
        > > Is there really a need to use prop in a closure in the saveBasicData
        > > function? It seems better to me to make the function take two parameters
        > > instead of using the outer-scope-level prop variable.
        > >
        > > Erik
        > >
        > > On Tue, May 10, 2011 at 9:17 AM, Jerry <jluke@> wrote:
        > >
        > > >
        > > >
        > > > I saw this posted in a previous message, but the thread ended because the
        > > > provided code sample did not reproduce the bug. I was able to produce the
        > > > following code sample that reproduces the 'Bad for in variable' problem:
        > > > =======================================================
        > > > function badForInVar() {
        > > > var basicData = { 'Status' : 'NORMAL' , 'Priority' : 'High' },
        > > > prop;
        > > >
        > > > function saveBasicData(value){
        > > > var lookup = {};
        > > > lookup.displayName = value;
        > > > lookup.type = prop;
        > > > }
        > > > for (prop in basicData) {
        > > > if (basicData.hasOwnProperty(prop)) {
        > > > saveBasicData(basicData[prop]);
        > > > }
        > > > }
        > > > }
        > > > =======================================================
        > > > I believe the saveBasicData function declaration between the var
        > > > declaration of 'prop' and its usage in the for/in loop is contributing to
        > > > JSLint reporting the 'Bad for in variable' error. If I move the
        > > > saveBasicData function to follow the for/in loop, I no longer get the error
        > > > (but instead get the message: 'saveBasicData' was used before it was
        > > > defined).
        > > >
        > > > I would very much appreciate any help in confirming whether or not this is
        > > > a bug in JSLint, I am missing the underlying cause of the error, or a
        > > > workaround to appease JSLint.
        > > >
        > > >
        > > >
        > >
        > >
        > > [Non-text portions of this message have been removed]
        > >
        > Sorry I forgot to mention that the code had been greatly hacked down. I was trying to get the minimal amount of code that would reproduce what I suspect is a problem with jslint reporting the 'Bad for in variable' error. Obviously, the remaining code does not actually do much of anything.
        > In reading through your response a second time, I realize I did not fully answer the question. There were intermediate calls to push the function into an array to be executed asynchronously. I believe we were stuck with the function signature and could only (easily) make use of the prop variable through closure.
      • Douglas Crockford
        ... Thank you. That was very helpful. Please try it now.
        Message 3 of 8 , May 10, 2011
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, "Jerry" <jluke@...> wrote:
          >
          > I saw this posted in a previous message, but the thread ended because the provided code sample did not reproduce the bug. I was able to produce the following code sample that reproduces the 'Bad for in variable' problem:
          > =======================================================
          > function badForInVar() {
          > var basicData = { 'Status' : 'NORMAL' , 'Priority' : 'High' },
          > prop;
          >
          > function saveBasicData(value){
          > var lookup = {};
          > lookup.displayName = value;
          > lookup.type = prop;
          > }
          > for (prop in basicData) {
          > if (basicData.hasOwnProperty(prop)) {
          > saveBasicData(basicData[prop]);
          > }
          > }
          > }
          > =======================================================
          > I believe the saveBasicData function declaration between the var declaration of 'prop' and its usage in the for/in loop is contributing to JSLint reporting the 'Bad for in variable' error. If I move the saveBasicData function to follow the for/in loop, I no longer get the error (but instead get the message: 'saveBasicData' was used before it was defined).


          Thank you. That was very helpful. Please try it now.
        • Jerry
          ... Boy, that was quick. Thanks, it works perfectly now.
          Message 4 of 8 , May 10, 2011
          • 0 Attachment
            --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
            >
            > --- In jslint_com@yahoogroups.com, "Jerry" <jluke@> wrote:
            > >
            > > I saw this posted in a previous message, but the thread ended because the provided code sample did not reproduce the bug. I was able to produce the following code sample that reproduces the 'Bad for in variable' problem:
            > > =======================================================
            > > function badForInVar() {
            > > var basicData = { 'Status' : 'NORMAL' , 'Priority' : 'High' },
            > > prop;
            > >
            > > function saveBasicData(value){
            > > var lookup = {};
            > > lookup.displayName = value;
            > > lookup.type = prop;
            > > }
            > > for (prop in basicData) {
            > > if (basicData.hasOwnProperty(prop)) {
            > > saveBasicData(basicData[prop]);
            > > }
            > > }
            > > }
            > > =======================================================
            > > I believe the saveBasicData function declaration between the var declaration of 'prop' and its usage in the for/in loop is contributing to JSLint reporting the 'Bad for in variable' error. If I move the saveBasicData function to follow the for/in loop, I no longer get the error (but instead get the message: 'saveBasicData' was used before it was defined).
            >
            >
            > Thank you. That was very helpful. Please try it now.
            >
            Boy, that was quick. Thanks, it works perfectly now.
          • pierremartineau
            ... I believe this is a similar problem: with these options: /*jslint es5: true, indent: 2 */ JSLint objects: Bad for in variable a . on this code: var a, b
            Message 5 of 8 , Aug 22, 2011
            • 0 Attachment
              --- In jslint_com@yahoogroups.com, "Jerry" <jluke@...> wrote:
              >
              >
              > --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@> wrote:
              > >
              > > --- In jslint_com@yahoogroups.com, "Jerry" <jluke@> wrote:
              > > >
              > > > I saw this posted in a previous message, but the thread ended because the provided code sample did not reproduce the bug. I was able to produce the following code sample that reproduces the 'Bad for in variable' problem:
              > > > =======================================================
              > > > function badForInVar() {
              > > > var basicData = { 'Status' : 'NORMAL' , 'Priority' : 'High' },
              > > > prop;
              > > >
              > > > function saveBasicData(value){
              > > > var lookup = {};
              > > > lookup.displayName = value;
              > > > lookup.type = prop;
              > > > }
              > > > for (prop in basicData) {
              > > > if (basicData.hasOwnProperty(prop)) {
              > > > saveBasicData(basicData[prop]);
              > > > }
              > > > }
              > > > }
              > > > =======================================================
              > > > I believe the saveBasicData function declaration between the var declaration of 'prop' and its usage in the for/in loop is contributing to JSLint reporting the 'Bad for in variable' error. If I move the saveBasicData function to follow the for/in loop, I no longer get the error (but instead get the message: 'saveBasicData' was used before it was defined).
              > >
              > >
              > > Thank you. That was very helpful. Please try it now.
              > >
              > Boy, that was quick. Thanks, it works perfectly now.
              >

              I believe this is a similar problem:

              with these options:
              /*jslint es5: true, indent: 2 */

              JSLint objects:
              "Bad for in variable 'a'."

              on this code:

              var a, b = {}, test;
              test = function () {
              'use strict';
              for (a in b) {
              if (b.hasOwnProperty(a)) {
              // do anything
              return true;
              }
              }
              };

              but not on this code:

              var a, b = {}, test;
              test = function () {
              'use strict';
              var a;
              for (a in b) {
              if (b.hasOwnProperty(a)) {
              // do anything
              return true;
              }
              }
              };
            • Douglas Crockford
              ... JSLint expects that the induction variable is local to the function containing the loop.
              Message 6 of 8 , Aug 22, 2011
              • 0 Attachment
                --- In jslint_com@yahoogroups.com, "pierremartineau" <pierrem@...> wrote:
                > I believe this is a similar problem:
                >
                > with these options:
                > /*jslint es5: true, indent: 2 */
                >
                > JSLint objects:
                > "Bad for in variable 'a'."
                >
                > on this code:
                >
                > var a, b = {}, test;
                > test = function () {
                > 'use strict';
                > for (a in b) {
                > if (b.hasOwnProperty(a)) {
                > // do anything
                > return true;
                > }
                > }
                > };
                >
                > but not on this code:
                >
                > var a, b = {}, test;
                > test = function () {
                > 'use strict';
                > var a;
                > for (a in b) {
                > if (b.hasOwnProperty(a)) {
                > // do anything
                > return true;
                > }
                > }
                > };

                JSLint expects that the induction variable is local to the function containing the loop.
              Your message has been successfully submitted and would be delivered to recipients shortly.