Skip to content

Commit 606346a

Browse files
authored
Merge pull request #11 from codemodsquad/fix/promise-reject
fix(cleanup): fix Promise.reject conversion
2 parents e95f926 + faa5914 commit 606346a

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/util/finalCleanup.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,10 @@ export default function finalCleanup(path: NodePath<t.Function>): void {
7979
)
8080
} else argument.remove()
8181
} else if (value.isCallExpression() && isPromiseRejectCall(value)) {
82-
path.replaceWith(
83-
t.throwStatement(
84-
t.newExpression(
85-
t.identifier('Error'),
86-
value.node.arguments.slice(0, 1)
87-
)
88-
)
89-
)
82+
const argument = value.node.arguments[0]
83+
if (t.isExpression(argument)) {
84+
path.replaceWith(t.throwStatement(argument))
85+
}
9086
} else if (argument.isAwaitExpression() && !isInTryBlock(path)) {
9187
argument.replaceWith(argument.node.argument)
9288
}
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 Promise.reject(new Error('test'))
4+
}
5+
`
6+
7+
export const options = {}
8+
9+
export const expected = `
10+
async function foo() {
11+
throw new Error('test')
12+
}
13+
`

0 commit comments

Comments
 (0)