Skip to content

Commit b23d1c5

Browse files
authored
fix: deduplicate variable names in no-loop-func error messages (#19595)
* fix: deduplicate variable names in no-loop-func error messages * chore: fix lint
1 parent 556c25b commit b23d1c5

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

lib/rules/no-loop-func.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,13 @@ module.exports = {
237237
}
238238
}
239239

240-
const unsafeRefs = references
241-
.filter(r => r.resolved && !isSafe(loopNode, r))
242-
.map(r => r.identifier.name);
240+
const unsafeRefs = [
241+
...new Set(
242+
references
243+
.filter(r => r.resolved && !isSafe(loopNode, r))
244+
.map(r => r.identifier.name),
245+
),
246+
];
243247

244248
if (unsafeRefs.length > 0) {
245249
context.report({

tests/lib/rules/no-loop-func.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,5 +740,28 @@ ruleTester.run("no-loop-func", rule, {
740740
},
741741
],
742742
},
743+
{
744+
code: `
745+
for (var i = 0; i < 10; i++) {
746+
items.push({
747+
id: i,
748+
name: "Item " + i
749+
});
750+
751+
const process = function (callback){
752+
callback({ id: i, name: "Item " + i });
753+
};
754+
}
755+
`,
756+
languageOptions: { ecmaVersion: 2022 },
757+
errors: [
758+
{
759+
messageId: "unsafeRefs",
760+
data: { varNames: "'i'" },
761+
type: "FunctionExpression",
762+
line: 8,
763+
},
764+
],
765+
},
743766
],
744767
});

0 commit comments

Comments
 (0)