SonarQube JavaScript
  1. SonarQube JavaScript
  2. SONARJS-112

Rule: Avoid definition of functions inside loops

    Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.3
    • Component/s: None
    • Labels:
      None
    • Number of attachments :
      0

      Description

      See "loopfunc" in http://www.jshint.com/docs/ and http://stackoverflow.com/questions/3927054/jslint-error-dont-make-functions-within-a-loop-leads-to-question-about-javas

      Title

      Avoid definition of functions inside loops

      Severity

      Major

      Description

      <p>Defining functions inside of loops can lead to bugs such as this one:</p>
      
      <pre>
      var funs = [];
      for (var i = 0; i < 13; i++) {
        funs[i] = function() { // Non-Complian
          return i;
        };
      }
      print(funs[0]()); // 13 instead of 0
      </pre>
      

      Message

      Avoid definition of function inside loop.

      Activated?

      Yes

      Cardinality

      Single

        Issue Links

          Activity

          Hide
          Evgeny Mandrikov added a comment -

          Integration tests were updated.

          Show
          Evgeny Mandrikov added a comment - Integration tests were updated.
          Hide
          Evgeny Mandrikov added a comment -

          Freddy, please check that description of this rule is ok.

          Show
          Evgeny Mandrikov added a comment - Freddy, please check that description of this rule is ok.
          Hide
          Freddy Mallet added a comment -

          Works well Evgeny, could you just update :

          • the violation message: "Define this function outside of a loop"
          • the description by appending the following lines :
            • print(funs[1]()); // 13 instead of 1
            • print(funs[2]()); // 13 instead of 2
            • print(funs[3]()); // 13 instead of 3
            • ...
          Show
          Freddy Mallet added a comment - Works well Evgeny, could you just update : the violation message: "Define this function outside of a loop" the description by appending the following lines : print(funs [1] ()); // 13 instead of 1 print(funs [2] ()); // 13 instead of 2 print(funs [3] ()); // 13 instead of 3 ...
          Hide
          Evgeny Mandrikov added a comment -

          Done.

          Show
          Evgeny Mandrikov added a comment - Done.
          Hide
          Freddy Mallet added a comment -

          Manually tested !

          Show
          Freddy Mallet added a comment - Manually tested !

            People

            • Assignee:
              Evgeny Mandrikov
              Reporter:
              Evgeny Mandrikov
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: