Skip to content

Commit 37a80f8

Browse files
committed
fix: findNextLinkToUnwind bug
fix #16
1 parent a2ddcff commit 37a80f8

File tree

8 files changed

+235
-75
lines changed

8 files changed

+235
-75
lines changed

src/util/findNextLinkToUnwind.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/util/findNode.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as t from '@babel/types'
2+
import { NodePath } from '@babel/traverse'
3+
4+
export default function findNode<N extends t.Node>(
5+
path: NodePath<any> | NodePath<any>[],
6+
node: N
7+
): NodePath<any> | null {
8+
if (Array.isArray(path)) {
9+
for (const p of path) {
10+
const result = findNode(p, node)
11+
if (result) return result
12+
}
13+
return null
14+
}
15+
16+
let result: NodePath<any> | null = null
17+
18+
path.traverse({
19+
[node.type](path: NodePath<any>) {
20+
if (path.node === node) {
21+
result = path
22+
path.stop()
23+
}
24+
},
25+
Function(path: NodePath<t.Function>) {
26+
path.skip()
27+
},
28+
})
29+
30+
return result
31+
}

src/util/unwindCatch.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,26 @@ import convertBodyToBlockStatement from './convertBodyToBlockStatement'
1111
import mergeCatchIntoTryFinally from './mergeCatchIntoFinally'
1212
import { awaited } from './builders'
1313
import convertConditionalReturns from './convertConditionalReturns'
14+
import findNode from './findNode'
1415

1516
export default function unwindCatch(
1617
handler: NodePath<t.Expression>
17-
): NodePath<any> | NodePath<any>[] {
18+
): NodePath<any> | null {
1819
const link = handler.parentPath as NodePath<t.CallExpression>
20+
let preceedingLink
1921
let preceeding
2022
if (link.node.arguments.length === 2) {
21-
preceeding = t.awaitExpression(
22-
t.callExpression(link.node.callee, [link.node.arguments[0]])
23-
)
23+
preceedingLink = t.callExpression(link.node.callee, [
24+
link.node.arguments[0],
25+
])
26+
preceeding = t.awaitExpression(preceedingLink)
2427
} else {
25-
preceeding = awaited(getPreceedingLink(link).node)
28+
preceedingLink = getPreceedingLink(link).node
29+
preceeding = awaited(preceedingLink)
2630
}
2731

2832
if (isNullish(handler.node)) {
29-
return replaceLink(link, preceeding)
33+
return findNode(replaceLink(link, preceeding), preceedingLink)
3034
}
3135

3236
if (!handler.isFunction()) {
@@ -46,7 +50,7 @@ export default function unwindCatch(
4650
!isPromiseMethodCall(getPreceedingLink(link).node)) ||
4751
(!canUnwindAsIs(link) && !convertConditionalReturns(body)))
4852
) {
49-
return []
53+
return null
5054
}
5155

5256
handlerFunction.node.async = true
@@ -79,8 +83,9 @@ export default function unwindCatch(
7983
const finalBody = handlerFunction.get('body') as NodePath<t.BlockStatement>
8084
;(finalBody.scope as any).crawl()
8185
const tryStatement = finalBody.get('body')[0] as NodePath<t.TryStatement>
82-
return (
86+
return findNode(
8387
mergeCatchIntoTryFinally(link, tryStatement) ||
84-
(replaceLink(link, finalBody) as any)
88+
(replaceLink(link, finalBody) as any),
89+
preceedingLink
8590
)
8691
}

src/util/unwindFinally.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ import replaceReturnStatements from './replaceReturnStatements'
99
import convertBodyToBlockStatement from './convertBodyToBlockStatement'
1010
import convertConditionalReturns from './convertConditionalReturns'
1111
import mergeStatementsIntoTryFinally from './mergeStatementsIntoTryFinally'
12+
import findNode from './findNode'
1213

1314
export default function unwindFinally(
1415
handler: NodePath<t.Expression>
15-
): NodePath<any> | NodePath<any>[] {
16+
): NodePath<any> | null {
1617
const link = handler.parentPath as NodePath<t.CallExpression>
17-
const preceeding = awaited(getPreceedingLink(link).node)
18+
const preceedingLink = getPreceedingLink(link)
19+
const preceeding = awaited(preceedingLink.node)
1820

1921
if (isNullish(handler.node)) {
20-
return replaceLink(link, preceeding)
22+
return findNode(replaceLink(link, preceeding), preceedingLink.node)
2123
}
2224

2325
if (!handler.isFunction()) {
@@ -30,7 +32,7 @@ export default function unwindFinally(
3032
handlerFunction.node.async = true
3133
const body = handlerFunction.get('body')
3234
if (body.isBlockStatement() && !convertConditionalReturns(body)) {
33-
return getPreceedingLink(link)
35+
return preceedingLink
3436
}
3537
body.replaceWith(
3638
t.blockStatement([
@@ -50,8 +52,9 @@ export default function unwindFinally(
5052
const finalBody = handlerFunction.get('body')
5153
;(finalBody.scope as any).crawl()
5254
const tryStatement = finalBody.get('body.0') as NodePath<t.TryStatement>
53-
return (
55+
return findNode(
5456
mergeStatementsIntoTryFinally(link, tryStatement) ||
55-
(replaceLink(link, finalBody) as any)
57+
(replaceLink(link, finalBody) as any),
58+
preceedingLink.node
5659
)
5760
}

src/util/unwindPromiseChain.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { unwindThen } from './unwindThen'
99
import unwindFinally from './unwindFinally'
1010
import parentStatement from './parentStatement'
1111
import replaceWithImmediatelyInvokedAsyncArrowFunction from './replaceWithImmediatelyInvokedAsyncArrowFunction'
12-
import findNextLinkToUnwind from './findNextLinkToUnwind'
1312

1413
export default function unwindPromiseChain(
1514
path: NodePath<t.CallExpression>
@@ -27,23 +26,22 @@ export default function unwindPromiseChain(
2726
let link: NodePath<t.CallExpression> | null = path as any
2827

2928
while (link) {
30-
const origNode = link.node
3129
const callee = link.get('callee')
3230
if (!callee.isMemberExpression()) break
3331

3432
const thenHandler = getThenHandler(link)
3533
const catchHandler = getCatchHandler(link)
3634
const finallyHandler = getFinallyHandler(link)
3735

38-
let replacements: NodePath | NodePath[] | null = null
3936
if (catchHandler) {
40-
replacements = unwindCatch(catchHandler)
37+
link = unwindCatch(catchHandler)
4138
} else if (thenHandler) {
42-
replacements = unwindThen(thenHandler)
39+
link = unwindThen(thenHandler)
4340
} else if (finallyHandler) {
44-
replacements = unwindFinally(finallyHandler)
41+
link = unwindFinally(finallyHandler)
42+
} else {
43+
link = null
4544
}
46-
link = replacements ? findNextLinkToUnwind(replacements, origNode) : null
4745
;(scope as any).crawl()
4846
}
4947
}

src/util/unwindThen.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ import hasMutableIdentifiers from './hasMutableIdentifiers'
1010
import prependBodyStatement from './prependBodyStatement'
1111
import replaceLink from './replaceLink'
1212
import convertConditionalReturns from './convertConditionalReturns'
13+
import findNode from './findNode'
1314

1415
export function unwindThen(
1516
handler: NodePath<t.Expression>
16-
): NodePath<any> | NodePath<any>[] {
17+
): NodePath<any> | null {
1718
const link = handler.parentPath as NodePath<t.CallExpression>
18-
const preceeding = awaited(getPreceedingLink(link).node)
19+
const preceedingLink = getPreceedingLink(link)
20+
const preceeding = awaited(preceedingLink.node)
1921

2022
if (isNullish(handler.node)) {
21-
return replaceLink(link, preceeding)
23+
return findNode(replaceLink(link, preceeding), preceedingLink.node)
2224
}
2325

2426
if (handler.isFunction()) {
@@ -31,7 +33,7 @@ export function unwindThen(
3133
!canUnwindAsIs(link) &&
3234
!convertConditionalReturns(body)
3335
) {
34-
return getPreceedingLink(link)
36+
return preceedingLink
3537
}
3638

3739
if (input) renameBoundIdentifiers(input, link.scope)
@@ -52,7 +54,13 @@ export function unwindThen(
5254
prepended.get('declarations.0.id') as any
5355
)
5456
}
55-
return replaceLink(link, handlerFunction.get('body')) as any
57+
return findNode(
58+
replaceLink(link, handlerFunction.get('body')) as any,
59+
preceedingLink.node
60+
)
5661
}
57-
return replaceLink(link, t.callExpression(handler.node, [preceeding])) as any
62+
return findNode(
63+
replaceLink(link, t.callExpression(handler.node, [preceeding])) as any,
64+
preceedingLink.node
65+
)
5866
}

test/fixtures/bugs_findAll.test.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,11 @@ it('should support many levels of belongsTo', async function() {
108108
109109
await this.sequelize.sync({ force: true });
110110
111-
await A.bulkCreate([
112-
{},
113-
{},
114-
{},
115-
{},
116-
{},
117-
{},
118-
{},
119-
{}
120-
]);
121-
122-
const [as0, b] = await Promise.all([await A.findAll(), (function(singles) {
111+
const [as0, b] = await Promise.all([
112+
A.bulkCreate([{}, {}, {}, {}, {}, {}, {}, {}]).then(() => {
113+
return A.findAll()
114+
}),
115+
(function(singles) {
123116
let promise = Promise.resolve(),
124117
previousInstance,
125118
b;

0 commit comments

Comments
 (0)