Skip to content

Commit

Permalink
fix(require-returns-check): allow infinite for loops to have only o…
Browse files Browse the repository at this point in the history
…ne branch to return; fixes #1315
  • Loading branch information
brettz9 committed Sep 18, 2024
1 parent 8222262 commit e7ab475
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
12 changes: 12 additions & 0 deletions docs/rules/require-returns-check.md
Original file line number Diff line number Diff line change
Expand Up @@ -992,5 +992,17 @@ function foo() {
}
}
}

/**
* @returns {number}
*/
function foo() {
for (;;) {
const n = Math.random();
if (n < 0.5) {
return n;
}
}
}
````

7 changes: 6 additions & 1 deletion src/utils/hasReturnValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,13 @@ const allBrancheshaveReturnValues = (node, promFilter) => {
}

// Fallthrough
case 'LabeledStatement':
case 'ForStatement':
if (node.test === null) {
// If this is an infinite loop, we assume only one branch
// is needed to provide a return
return hasReturnValue(node.body, false, promFilter);
}
case 'LabeledStatement':
case 'ForInStatement':
case 'ForOfStatement':
case 'WithStatement': {
Expand Down
15 changes: 15 additions & 0 deletions test/rules/assertions/requireReturnsCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -1558,5 +1558,20 @@ export default {
}
`,
},
{
code: `
/**
* @returns {number}
*/
function foo() {
for (;;) {
const n = Math.random();
if (n < 0.5) {
return n;
}
}
}
`,
},
],
};

0 comments on commit e7ab475

Please sign in to comment.