Skip to content

Commit a60d1cb

Browse files
authored
fix(always-return): false positives for logical expr (#363)
1 parent 9b3ef57 commit a60d1cb

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

__tests__/always-return.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ ruleTester.run('always-return', rule, {
4242
}
4343
})
4444
})`,
45+
`hey.then(({x, y}) => {
46+
if (y) {
47+
throw new Error(x || y)
48+
}
49+
return x
50+
})`,
4551
],
4652

4753
invalid: [
@@ -106,5 +112,23 @@ ruleTester.run('always-return', rule, {
106112
})()`,
107113
errors: [{ message }],
108114
},
115+
{
116+
code: `
117+
hey.then(({x, y}) => {
118+
if (y) {
119+
throw new Error(x || y)
120+
}
121+
})`,
122+
errors: [{ message }],
123+
},
124+
{
125+
code: `
126+
hey.then(({x, y}) => {
127+
if (y) {
128+
return x
129+
}
130+
})`,
131+
errors: [{ message }],
132+
},
109133
],
110134
})

rules/always-return.js

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,6 @@ function isInlineThenFunctionExpression(node) {
3434
)
3535
}
3636

37-
function hasParentReturnStatement(node) {
38-
// istanbul ignore else -- not reachable given not checking `Program`
39-
if (node && node.parent && node.parent.type) {
40-
// if the parent is a then, and we haven't returned anything, fail
41-
if (isThenCallExpression(node.parent)) {
42-
return false
43-
}
44-
45-
if (node.parent.type === 'ReturnStatement') {
46-
return true
47-
}
48-
return hasParentReturnStatement(node.parent)
49-
}
50-
51-
// istanbul ignore next -- not reachable given not checking `Program`
52-
return false
53-
}
54-
5537
function peek(arr) {
5638
return arr[arr.length - 1]
5739
}
@@ -106,8 +88,8 @@ module.exports = {
10688
}
10789

10890
return {
109-
ReturnStatement: markCurrentBranchAsGood,
110-
ThrowStatement: markCurrentBranchAsGood,
91+
'ReturnStatement:exit': markCurrentBranchAsGood,
92+
'ThrowStatement:exit': markCurrentBranchAsGood,
11193

11294
onCodePathSegmentStart(segment, node) {
11395
const funcInfo = peek(funcInfoStack)
@@ -138,10 +120,6 @@ module.exports = {
138120
const id = segment.id
139121
const branch = funcInfo.branchInfoMap[id]
140122
if (!branch.good) {
141-
if (hasParentReturnStatement(branch.node)) {
142-
return
143-
}
144-
145123
context.report({
146124
message: 'Each then() should return a value or throw',
147125
node: branch.node,

0 commit comments

Comments
 (0)