Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(always-return): false positives for logical expr #363

Merged
merged 1 commit into from Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions __tests__/always-return.js
Expand Up @@ -42,6 +42,12 @@ ruleTester.run('always-return', rule, {
}
})
})`,
`hey.then(({x, y}) => {
if (y) {
throw new Error(x || y)
}
return x
})`,
],

invalid: [
Expand Down Expand Up @@ -106,5 +112,23 @@ ruleTester.run('always-return', rule, {
})()`,
errors: [{ message }],
},
{
code: `
hey.then(({x, y}) => {
if (y) {
throw new Error(x || y)
}
})`,
errors: [{ message }],
},
{
code: `
hey.then(({x, y}) => {
if (y) {
return x
}
})`,
errors: [{ message }],
},
],
})
26 changes: 2 additions & 24 deletions rules/always-return.js
Expand Up @@ -34,24 +34,6 @@ function isInlineThenFunctionExpression(node) {
)
}

function hasParentReturnStatement(node) {
// istanbul ignore else -- not reachable given not checking `Program`
if (node && node.parent && node.parent.type) {
// if the parent is a then, and we haven't returned anything, fail
if (isThenCallExpression(node.parent)) {
return false
}

if (node.parent.type === 'ReturnStatement') {
return true
}
return hasParentReturnStatement(node.parent)
}

// istanbul ignore next -- not reachable given not checking `Program`
return false
}

function peek(arr) {
return arr[arr.length - 1]
}
Expand Down Expand Up @@ -106,8 +88,8 @@ module.exports = {
}

return {
ReturnStatement: markCurrentBranchAsGood,
ThrowStatement: markCurrentBranchAsGood,
'ReturnStatement:exit': markCurrentBranchAsGood,
'ThrowStatement:exit': markCurrentBranchAsGood,

onCodePathSegmentStart(segment, node) {
const funcInfo = peek(funcInfoStack)
Expand Down Expand Up @@ -138,10 +120,6 @@ module.exports = {
const id = segment.id
const branch = funcInfo.branchInfoMap[id]
if (!branch.good) {
if (hasParentReturnStatement(branch.node)) {
return
}

context.report({
message: 'Each then() should return a value or throw',
node: branch.node,
Expand Down