Skip to content

Commit e7ab475

Browse files
committedSep 18, 2024·
fix(require-returns-check): allow infinite for loops to have only one branch to return; fixes #1315
1 parent 8222262 commit e7ab475

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed
 

‎docs/rules/require-returns-check.md

+12
Original file line numberDiff line numberDiff line change
@@ -992,5 +992,17 @@ function foo() {
992992
}
993993
}
994994
}
995+
996+
/**
997+
* @returns {number}
998+
*/
999+
function foo() {
1000+
for (;;) {
1001+
const n = Math.random();
1002+
if (n < 0.5) {
1003+
return n;
1004+
}
1005+
}
1006+
}
9951007
````
9961008

‎src/utils/hasReturnValue.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,13 @@ const allBrancheshaveReturnValues = (node, promFilter) => {
177177
}
178178

179179
// Fallthrough
180-
case 'LabeledStatement':
181180
case 'ForStatement':
181+
if (node.test === null) {
182+
// If this is an infinite loop, we assume only one branch
183+
// is needed to provide a return
184+
return hasReturnValue(node.body, false, promFilter);
185+
}
186+
case 'LabeledStatement':
182187
case 'ForInStatement':
183188
case 'ForOfStatement':
184189
case 'WithStatement': {

‎test/rules/assertions/requireReturnsCheck.js

+15
Original file line numberDiff line numberDiff line change
@@ -1558,5 +1558,20 @@ export default {
15581558
}
15591559
`,
15601560
},
1561+
{
1562+
code: `
1563+
/**
1564+
* @returns {number}
1565+
*/
1566+
function foo() {
1567+
for (;;) {
1568+
const n = Math.random();
1569+
if (n < 0.5) {
1570+
return n;
1571+
}
1572+
}
1573+
}
1574+
`,
1575+
},
15611576
],
15621577
};

0 commit comments

Comments
 (0)
Please sign in to comment.