Repository: eslint/eslint
File: tests/lib/rules/no-loop-func.js (JavaScript)

Code before:
                "  const letters = scores[score];",
                "  letters.split('').forEach(letter => {",
                "    result[letter] = score;",
                "  });",
                "}",
                "result.__default = 6;"
            ].join("\n"),
            parserOptions: { ecmaVersion: 6 }
        },
        {
            code: [
                "while (true) {",
                "    (function() { a; });",
                "}",
                "let a;"
            ].join("\n"),
            parserOptions: { ecmaVersion: 6 }
        },

        /*
         * These loops _look_ like they might be unsafe, but because i is undeclared, they're fine
         * at least as far as this rule is concerned - the loop doesn't declare/generate the variable.
         */
        {
            code: "while(i) { (function() { i; }) }",
            errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "FunctionExpression" }]
        },
        {
            code: "do { (function() { i; }) } while (i)",
            errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "FunctionExpression" }]
        },

        /**
         * These loops _look_ like they might be unsafe, but because i is declared outside the loop,
         * they're fine as far as this rule is concerned - the loop doesn't declare/generate the variable.
         * The variable that's captured is just the one variable shared by all the loops, but that's
         * explicitly expected in these cases.
         */
        {
            code: "var i; while(i) { (function() { i; }) }",
            errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "FunctionExpression" }]
        },
        {
            code: "var i; do { (function() { i; }) } while (i)",
            errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "FunctionExpression" }]
        },

        /**
         * These loops use an undeclared variable, and so shouldn't be flagged by this rule,
         * they'll be picked up by no-undef.
         */

Review comment (suggestion):
```suggestion
            code: "while(i) { (function() { i; }) }"
```

There should be no `errors` property in `valid` tests.

Code after:
                "  const letters = scores[score];",
                "  letters.split('').forEach(letter => {",
                "    result[letter] = score;",
                "  });",
                "}",
                "result.__default = 6;"
            ].join("\n"),
            parserOptions: { ecmaVersion: 6 }
        },
        {
            code: [
                "while (true) {",
                "    (function() { a; });",
                "}",
                "let a;"
            ].join("\n"),
            parserOptions: { ecmaVersion: 6 }
        },

        /*
         * These loops _look_ like they might be unsafe, but because i is undeclared, they're fine
         * at least as far as this rule is concerned - the loop doesn't declare/generate the variable.
         */
        "while(i) { (function() { i; }) }",
        "do { (function() { i; }) } while (i)",

        /**
         * These loops _look_ like they might be unsafe, but because i is declared outside the loop
         * and is not updated in or after the loop, they're fine as far as this rule is concerned.
         * The variable that's captured is just the one variable shared by all the loops, but that's
         * explicitly expected in these cases.
         */
        "var i; while(i) { (function() { i; }) }",
        "var i; do { (function() { i; }) } while (i)",

        /**
         * These loops use an undeclared variable, and so shouldn't be flagged by this rule,
         * they'll be picked up by no-undef.
         */
        {
            code: "for (var i=0; i<l; i++) { (function() { undeclared; }) }",
            parserOptions: { ecmaVersion: 6 }
        },
        {
            code: "for (let i=0; i<l; i++) { (function() { undeclared; }) }",
            parserOptions: { ecmaVersion: 6 }
        },
        {
            code: "for (var i in {}) { i = 7; (function() { undeclared; }) }",
            parserOptions: { ecmaVersion: 6 }
        },