Skip to content

Commit

Permalink
fix: Update no-loop-func to not overlap with no-undef (#17358)
Browse files Browse the repository at this point in the history
  • Loading branch information
matwilko committed Jul 18, 2023
1 parent 7e9be4b commit 42faa17
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/rules/no-loop-func.js
Expand Up @@ -186,7 +186,7 @@ module.exports = {
}

const references = sourceCode.getScope(node).through;
const unsafeRefs = references.filter(r => !isSafe(loopNode, r)).map(r => r.identifier.name);
const unsafeRefs = references.filter(r => r.resolved && !isSafe(loopNode, r)).map(r => r.identifier.name);

if (unsafeRefs.length > 0) {
context.report({
Expand Down
54 changes: 46 additions & 8 deletions tests/lib/rules/no-loop-func.js
Expand Up @@ -111,7 +111,53 @@ ruleTester.run("no-loop-func", rule, {
"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 }
},
{
code: "for (let i in {}) { i = 7; (function() { undeclared; }) }",
parserOptions: { ecmaVersion: 6 }
},
{
code: "for (const i of {}) { (function() { undeclared; }) }",
parserOptions: { ecmaVersion: 6 }
},
{
code: "for (let i = 0; i < 10; ++i) { for (let x in xs.filter(x => x != undeclared)) { } }",
parserOptions: { ecmaVersion: 6 }
}

],
invalid: [
{
Expand Down Expand Up @@ -152,14 +198,6 @@ ruleTester.run("no-loop-func", rule, {
code: "for (var i=0; i<l; (function() { i; })(), i++) { }",
errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "FunctionExpression" }]
},
{
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" }]
},

// Warns functions which are using modified variables.
{
Expand Down

0 comments on commit 42faa17

Please sign in to comment.