Skip to content

Commit c5cb87d

Browse files
committed
fix: unsafe catch unwinding case, simple noop catch handler
1 parent 990a7e7 commit c5cb87d

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

src/util/unwindCatch.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as t from '@babel/types'
22
import { NodePath } from '@babel/traverse'
33

44
import getPreceedingLink from './getPreceedingLink'
5-
import { isNullish } from './predicates'
5+
import { isNullish, isPromiseMethodCall } from './predicates'
66
import canUnwindAsIs from './canUnwindAsIs'
77
import replaceLink from './replaceLink'
88
import renameBoundIdentifiers from './renameBoundIdentifiers'
@@ -39,16 +39,17 @@ export default function unwindCatch(
3939
) as any
4040
}
4141
const handlerFunction = handler as NodePath<t.Function>
42-
handlerFunction.node.async = true
4342
const body = handlerFunction.get('body')
4443
if (
4544
body.isBlockStatement() &&
46-
!canUnwindAsIs(link) &&
47-
!convertConditionalReturns(body)
45+
((body.node.body.length === 0 &&
46+
!isPromiseMethodCall(getPreceedingLink(link).node)) ||
47+
(!canUnwindAsIs(link) && !convertConditionalReturns(body)))
4848
) {
49-
return getPreceedingLink(link)
49+
return []
5050
}
5151

52+
handlerFunction.node.async = true
5253
const input = handlerFunction.get('params')[0]
5354
if (input) renameBoundIdentifiers(input, link.scope)
5455
const inputNode = input?.node

test/fixtures/bugs_Sequelize.transaction.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,7 @@ class Sequelize {
5454
} catch (err) {
5555
// Rollback transaction if not already finished (commit, rollback, etc)
5656
// and reject with original error (ignore any error in rollback)
57-
if (!transaction.finished) {
58-
try {
59-
await transaction.rollback();
60-
} catch (err0) {
61-
}
62-
}
57+
if (!transaction.finished) await transaction.rollback().catch(() => {});
6358
throw err;
6459
}
6560
});

test/fixtures/catch_Swallow.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const input = `
2+
function foo() {
3+
return baz.catch(() => {})
4+
}
5+
`
6+
7+
export const options = {}
8+
9+
export const expected = `
10+
async function foo() {
11+
return baz.catch(() => {})
12+
}
13+
`

test/fixtures/catch_handlersThatCantBeUnwound.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,31 @@ export const options = {}
3939

4040
export const expected = `
4141
async function foo() {
42-
return String(await bar.catch(async baz => {
42+
return String(await bar.catch(baz => {
4343
switch (baz) {
4444
case 2: return
4545
}
4646
console.log('test')
4747
}))
4848
}
4949
async function bar() {
50-
return String(await a.catch(async b => {
50+
return String(await a.catch(b => {
5151
for (const i of [1, 2, 3]) {
5252
return
5353
}
5454
console.log('test')
5555
}))
5656
}
5757
async function qux() {
58-
return String(await a.catch(async b => {
58+
return String(await a.catch(b => {
5959
while (i) {
6060
return
6161
}
6262
console.log('test')
6363
}))
6464
}
6565
async function baz() {
66-
return String(await a.catch(async b => {
66+
return String(await a.catch(b => {
6767
if (a) {
6868
if (b) {
6969
return

0 commit comments

Comments
 (0)