Skip to content

Commit ebfedfa

Browse files
committed
fix(finalCleanup): remove final returns that would be empty
1 parent 4302d46 commit ebfedfa

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/util/finalCleanup.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
needsAwait,
66
isPromiseRejectCall,
77
isInTryBlock,
8+
isLastStatementInFunction,
89
} from './predicates'
910
import { awaited } from './builders'
1011

@@ -14,6 +15,8 @@ function unwrapPromiseResolves(
1415
while (node && isPromiseResolveCall(node)) {
1516
node = (node as t.CallExpression).arguments[0]
1617
}
18+
if (node && node.type === 'Identifier' && node.name === 'undefined')
19+
return undefined
1720
return node as t.Expression
1821
}
1922

@@ -93,7 +96,11 @@ export default function finalCleanup(path: NodePath<t.Function>): void {
9396
? awaited(unwrapped)
9497
: unwrapped
9598
)
96-
} else argument.remove()
99+
} else if (isLastStatementInFunction(path)) {
100+
path.remove()
101+
} else {
102+
argument.remove()
103+
}
97104
} else if (value.isCallExpression() && isPromiseRejectCall(value)) {
98105
const argument = value.node.arguments[0]
99106
if (t.isExpression(argument)) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export const input = `
2+
function foo() {
3+
return Promise.resolve(2)
4+
}
5+
function bar() {
6+
return Promise.resolve()
7+
}
8+
function baz() {
9+
if (qux) {
10+
return Promise.resolve()
11+
}
12+
return Promise.resolve(3)
13+
}
14+
`
15+
16+
export const options = {}
17+
18+
export const expected = `
19+
async function foo() {
20+
return 2
21+
}
22+
async function bar() {
23+
}
24+
async function baz() {
25+
if (qux) {
26+
return
27+
}
28+
return 3
29+
}
30+
`

0 commit comments

Comments
 (0)