Skip to content

Commit

Permalink
fix(always-return): treat process.exit() or process.abort() as an a…
Browse files Browse the repository at this point in the history
…cceptable "return" (#493)
  • Loading branch information
brettz9 authored Jul 21, 2024
1 parent 36d13f5 commit f368c5a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions __tests__/always-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ ruleTester.run('always-return', rule, {
'hey.then(x => { return; })',
'hey.then(x => { return x ? x.id : null })',
'hey.then(x => { return x * 10 })',
'hey.then(x => { process.exit(0); })',
'hey.then(x => { process.abort(); })',
'hey.then(function() { return 42; })',
'hey.then(function() { return new Promise(); })',
'hey.then(function() { return "x"; }).then(doSomethingWicked)',
'hey.then(x => x).then(function() { return "3" })',
'hey.then(function() { throw new Error("msg"); })',
'hey.then(function(x) { if (!x) { throw new Error("no x"); } return x; })',
'hey.then(function(x) { if (x) { return x; } throw new Error("no x"); })',
'hey.then(function(x) { if (x) { process.exit(0); } throw new Error("no x"); })',
'hey.then(function(x) { if (x) { process.abort(); } throw new Error("no x"); })',
'hey.then(x => { throw new Error("msg"); })',
'hey.then(x => { if (!x) { throw new Error("no x"); } return x; })',
'hey.then(x => { if (x) { return x; } throw new Error("no x"); })',
Expand Down Expand Up @@ -140,6 +144,10 @@ ruleTester.run('always-return', rule, {
code: 'hey.then(function() { if (x) { } else { return x; }})',
errors: [{ message }],
},
{
code: 'hey.then(function() { if (x) { process.chdir(); } else { return x; }})',
errors: [{ message }],
},
{
code: 'hey.then(function() { if (x) { return you.then(function() { return x; }); } })',
errors: [{ message }],
Expand Down
4 changes: 4 additions & 0 deletions rules/always-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ module.exports = {
return {
'ReturnStatement:exit': markCurrentBranchAsGood,
'ThrowStatement:exit': markCurrentBranchAsGood,
'ExpressionStatement > CallExpression > MemberExpression[object.name="process"][property.name="exit"]:exit':
markCurrentBranchAsGood,
'ExpressionStatement > CallExpression > MemberExpression[object.name="process"][property.name="abort"]:exit':
markCurrentBranchAsGood,

/**
* @param {CodePathSegment} segment
Expand Down

0 comments on commit f368c5a

Please sign in to comment.