Skip to content

Commit 9091071

Browse files
authored
fix: prevent no-unreachable-loop crash when all loop types are ignored (#21116)
1 parent 044a627 commit 9091071

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

‎lib/rules/no-unreachable-loop.js‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,15 @@ module.exports = {
9898

9999
create(context) {
100100
const [{ ignore: ignoredLoopTypes }] = context.options;
101-
const loopTypesToCheck = getDifference(allLoopTypes, ignoredLoopTypes),
102-
loopSelector = loopTypesToCheck.join(","),
103-
loopsByTargetSegments = new Map(),
104-
loopsToReport = new Set();
101+
const loopTypesToCheck = getDifference(allLoopTypes, ignoredLoopTypes);
102+
const loopSelector = loopTypesToCheck.join(",");
105103

104+
if (!loopSelector) {
105+
return {};
106+
}
107+
108+
const loopsByTargetSegments = new Map();
109+
const loopsToReport = new Set();
106110
const codePathSegments = [];
107111
let currentCodePathSegments = new Set();
108112

‎tests/lib/rules/no-unreachable-loop.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,20 @@ ruleTester.run("no-unreachable-loop", rule, {
223223
code: "for (var key in obj) { hasEnumerableProperties = true; break; } for (const a of b) break;",
224224
options: [{ ignore: ["ForInStatement", "ForOfStatement"] }],
225225
},
226+
{
227+
code: "while (a) break;",
228+
options: [
229+
{
230+
ignore: [
231+
"WhileStatement",
232+
"DoWhileStatement",
233+
"ForStatement",
234+
"ForInStatement",
235+
"ForOfStatement",
236+
],
237+
},
238+
],
239+
},
226240
],
227241

228242
invalid: [

0 commit comments

Comments
 (0)