Skip to content

Commit 512670c

Browse files
committed
fix(require-returns-check): allow infinite loops to only have one branch return; fixes #932
1 parent 42fd03b commit 512670c

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17881,6 +17881,20 @@ function foo() {
1788117881
}
1788217882
};
1788317883
// Message: JSDoc @returns declaration present but return expression not available in function.
17884+
17885+
/**
17886+
* @returns {number}
17887+
*/
17888+
function foo() {
17889+
let n = 1;
17890+
while (n > 0.5) {
17891+
n = Math.random();
17892+
if (n < 0.2) {
17893+
return n;
17894+
}
17895+
}
17896+
}
17897+
// Message: JSDoc @returns declaration present but return expression not available in function.
1788417898
````
1788517899

1788617900
The following patterns are not considered problems:
@@ -18517,6 +18531,18 @@ const quux = (someVar) => {
1851718531
return true;
1851818532
}
1851918533
};
18534+
18535+
/**
18536+
* @returns {number}
18537+
*/
18538+
function foo() {
18539+
while (true) {
18540+
const n = Math.random();
18541+
if (n < 0.5) {
18542+
return n;
18543+
}
18544+
}
18545+
}
1852018546
````
1852118547

1852218548

src/utils/hasReturnValue.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,16 @@ const allBrancheshaveReturnValues = (node, promFilter) => {
149149
return allBrancheshaveReturnValues(lastBodyNode, promFilter);
150150
}
151151

152-
case 'LabeledStatement':
153152
case 'WhileStatement':
154153
case 'DoWhileStatement':
154+
if (node.test.value === true) {
155+
// If this is an infinite loop, we assume only one branch
156+
// is needed to provide a return
157+
return hasReturnValue(node.body, false, promFilter);
158+
}
159+
160+
// Fallthrough
161+
case 'LabeledStatement':
155162
case 'ForStatement':
156163
case 'ForInStatement':
157164
case 'ForOfStatement':

test/rules/assertions/requireReturnsCheck.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,29 @@ export default {
654654
},
655655
],
656656
},
657+
{
658+
code: `
659+
/**
660+
* @returns {number}
661+
*/
662+
function foo() {
663+
let n = 1;
664+
while (n > 0.5) {
665+
n = Math.random();
666+
if (n < 0.2) {
667+
return n;
668+
}
669+
}
670+
}
671+
`,
672+
errors: [
673+
{
674+
line: 2,
675+
message: 'JSDoc @returns declaration present but return expression not available in function.',
676+
},
677+
],
678+
},
679+
657680
],
658681
valid: [
659682
{
@@ -1576,5 +1599,20 @@ export default {
15761599
};
15771600
`,
15781601
},
1602+
{
1603+
code: `
1604+
/**
1605+
* @returns {number}
1606+
*/
1607+
function foo() {
1608+
while (true) {
1609+
const n = Math.random();
1610+
if (n < 0.5) {
1611+
return n;
1612+
}
1613+
}
1614+
}
1615+
`,
1616+
},
15791617
],
15801618
};

0 commit comments

Comments
 (0)