SonarQube JavaScript
  1. SonarQube JavaScript
  2. SONARJS-96

Rule: For-in statement must filter items

    Details

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

      Issue Links

        Activity

        Hide
        Freddy Mallet added a comment - - edited

        This rule requires a extended documentation and I guess the one provided by jslint is correct :

        <p>
        The for in statement allows for looping through the names of all of the properties of an object. Unfortunately, it also loops through all of the properties that were inherited through the prototype chain. This has the bad side effect of serving up method functions when the interest is in data properties. If a program is written without awareness of this situation, then it can fail.
        </p>
        
        <p>
        The body of every for in statement should be wrapped in an if statement that does filtering. It can select for a particular type or range of values, or it can exclude functions, or it can exclude properties from the prototype. For example,</p>
        
        <pre>
        for (name in object) {
            if (object.hasOwnProperty(name)) {
                ....
            }
        
        }
        </pre>
        

        But in that case I expect to get an exception on the following piece of code which is not the case :

        for (name in object) {
            print(something);
        }
        
        Show
        Freddy Mallet added a comment - - edited This rule requires a extended documentation and I guess the one provided by jslint is correct : <p> The for in statement allows for looping through the names of all of the properties of an object. Unfortunately, it also loops through all of the properties that were inherited through the prototype chain. This has the bad side effect of serving up method functions when the interest is in data properties. If a program is written without awareness of this situation, then it can fail. </p> <p> The body of every for in statement should be wrapped in an if statement that does filtering. It can select for a particular type or range of values, or it can exclude functions, or it can exclude properties from the prototype. For example,</p> <pre> for (name in object) { if (object.hasOwnProperty(name)) { .... } } </pre> But in that case I expect to get an exception on the following piece of code which is not the case : for (name in object) { print(something); }
        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:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: