Skip to content

Commit 317b31a

Browse files
committed
fix: invalid output when catch argument has type annotation
and add some flow and ts tests
1 parent fcf4cb1 commit 317b31a

11 files changed

+283
-5
lines changed

src/util/replaceLink.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,10 @@ export default function replaceLink<T extends t.Expression | t.BlockStatement>(
127127
return target.replaceWithMultiple(replacement.node.body) as any
128128
} else {
129129
const result = unboundIdentifier(replacement, 'result')
130+
const declarator = t.variableDeclarator(result)
130131
replacement.unshiftContainer(
131132
'body',
132-
template.statements.ast`let ${result}`
133+
t.variableDeclaration('let', [declarator])
133134
)
134135
replaceReturnStatements(replacement, argument =>
135136
t.expressionStatement(

src/util/unwindCatch.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export default function unwindCatch(
7171
'TODO: these catch parameter node types are not supported yet'
7272
)
7373
}
74+
if (inputNode) delete inputNode.typeAnnotation
7475
const catchClause = t.catchClause(
7576
inputNode || unboundIdentifier(handler, 'err'),
7677
convertBodyToBlockStatement(handlerFunction).node

test/fixtures/conditionalReturnCatch_FirstConsequentFallsThrough.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ async function createUser(args) {
77
console.log('a')
88
} else if (foo) {
99
console.log('b')
10-
return addUserToGroups(user, groups)
10+
return addUserToGroups(user, groups).then(() => user)
1111
} else {
1212
console.log('c')
13-
return addUserToGroups(user, groups)
13+
return addUserToGroups(user, groups).then(() => user)
1414
}
1515
return user
1616
})
@@ -34,10 +34,12 @@ async function createUser(args) {
3434
user = user0
3535
} else if (foo) {
3636
console.log('b')
37-
user = await addUserToGroups(user0, groups)
37+
await addUserToGroups(user0, groups)
38+
user = user0
3839
} else {
3940
console.log('c')
40-
user = await addUserToGroups(user0, groups)
41+
await addUserToGroups(user0, groups)
42+
user = user0
4143
}
4244
} catch (err) {
4345
console.error(err.stack)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
export const input = `
2+
async function createUser(args) {
3+
const {username, groups} = args
4+
const user = await Users.create({username})
5+
.then((user: User) => {
6+
if (groups) {
7+
console.log('a')
8+
} else if (foo) {
9+
console.log('b')
10+
return addUserToGroups(user, groups).then(() => user)
11+
} else {
12+
console.log('c')
13+
return addUserToGroups(user, groups).then(() => user)
14+
}
15+
return user
16+
})
17+
.catch((err: Error) => {
18+
console.error(err.stack)
19+
return dummyUser()
20+
})
21+
}
22+
`
23+
24+
export const options = {}
25+
export const parser = 'babylon'
26+
27+
export const expected = `
28+
async function createUser(args) {
29+
const {username, groups} = args
30+
let user
31+
try {
32+
const user0: User = await Users.create({ username })
33+
if (groups) {
34+
console.log('a')
35+
user = user0
36+
} else if (foo) {
37+
console.log('b')
38+
await addUserToGroups(user0, groups)
39+
user = user0
40+
} else {
41+
console.log('c')
42+
await addUserToGroups(user0, groups)
43+
user = user0
44+
}
45+
} catch (err) {
46+
console.error(err.stack)
47+
user = await dummyUser()
48+
}
49+
}
50+
`
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
export const input = `
2+
async function createUser(args) {
3+
const {username, groups} = args
4+
const user = await Users.create({username})
5+
.then((user: User) => {
6+
if (groups) {
7+
console.log('a')
8+
} else if (foo) {
9+
console.log('b')
10+
return addUserToGroups(user, groups).then(() => user)
11+
} else {
12+
console.log('c')
13+
return addUserToGroups(user, groups).then(() => user)
14+
}
15+
return user
16+
})
17+
.catch((err: Error) => {
18+
console.error(err.stack)
19+
return dummyUser()
20+
})
21+
}
22+
`
23+
24+
export const options = {}
25+
export const parser = 'ts'
26+
27+
export const expected = `
28+
async function createUser(args) {
29+
const {username, groups} = args
30+
let user
31+
try {
32+
const user0: User = await Users.create({ username })
33+
if (groups) {
34+
console.log('a')
35+
user = user0
36+
} else if (foo) {
37+
console.log('b')
38+
await addUserToGroups(user0, groups)
39+
user = user0
40+
} else {
41+
console.log('c')
42+
await addUserToGroups(user0, groups)
43+
user = user0
44+
}
45+
} catch (err) {
46+
console.error(err.stack)
47+
user = await dummyUser()
48+
}
49+
}
50+
`
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export const input = `
2+
function createUser(args) {
3+
const {username, organizationId} = args
4+
return Users.create({username})
5+
.then((user: User) => {
6+
return addUserToOrganization(user, organizationId)
7+
})
8+
.catch((err: Error) => {
9+
console.error(err.stack)
10+
return failedUser()
11+
})
12+
.finally(() => {
13+
return cleanup()
14+
})
15+
}
16+
`
17+
18+
export const options = {}
19+
export const parser = 'babylon'
20+
21+
export const expected = `
22+
async function createUser(args) {
23+
const {username, organizationId} = args
24+
try {
25+
const user: User = await Users.create({ username })
26+
return await addUserToOrganization(user, organizationId)
27+
}
28+
catch (err) {
29+
console.error(err.stack)
30+
return failedUser()
31+
}
32+
finally {
33+
await cleanup()
34+
}
35+
}
36+
`

test/fixtures/thenCatchFinally_ts.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export const input = `
2+
function createUser(args) {
3+
const {username, organizationId} = args
4+
return Users.create({username})
5+
.then((user: User) => {
6+
return addUserToOrganization(user, organizationId)
7+
})
8+
.catch((err: Error) => {
9+
console.error(err.stack)
10+
return failedUser()
11+
})
12+
.finally(() => {
13+
return cleanup()
14+
})
15+
}
16+
`
17+
18+
export const options = {}
19+
export const parser = 'ts'
20+
21+
export const expected = `
22+
async function createUser(args) {
23+
const {username, organizationId} = args
24+
try {
25+
const user: User = await Users.create({ username })
26+
return await addUserToOrganization(user, organizationId)
27+
}
28+
catch (err) {
29+
console.error(err.stack)
30+
return failedUser()
31+
}
32+
finally {
33+
await cleanup()
34+
}
35+
}
36+
`
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export const input = `
2+
async function foo() {
3+
const {stuff} = await baz.then(
4+
(value: Thing) => {
5+
if (value instanceof Blargh) return processBlargh(value)
6+
else return processOther(value)
7+
},
8+
(err: Error) => {
9+
if (err instanceof ConstraintViolation) return processConstraintViolation(err)
10+
else return processOther(err)
11+
}
12+
)
13+
}
14+
`
15+
16+
export const options = {}
17+
export const parser = 'babylon'
18+
19+
export const expected = `
20+
async function foo() {
21+
let result
22+
try {
23+
const value: Thing = await baz
24+
if (value instanceof Blargh) result = await processBlargh(value)
25+
else result = await processOther(value)
26+
} catch (err) {
27+
if (err instanceof ConstraintViolation) result = await processConstraintViolation(err)
28+
else result = await processOther(err)
29+
}
30+
const {stuff} = result
31+
}
32+
`
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export const input = `
2+
async function foo() {
3+
const {stuff} = await baz.then(
4+
(value: Thing) => {
5+
if (value instanceof Blargh) return processBlargh(value)
6+
else return processOther(value)
7+
},
8+
(err: Error) => {
9+
if (err instanceof ConstraintViolation) return processConstraintViolation(err)
10+
else return processOther(err)
11+
}
12+
)
13+
}
14+
`
15+
16+
export const options = {}
17+
export const parser = 'ts'
18+
19+
export const expected = `
20+
async function foo() {
21+
let result
22+
try {
23+
const value: Thing = await baz
24+
if (value instanceof Blargh) result = await processBlargh(value)
25+
else result = await processOther(value)
26+
} catch (err) {
27+
if (err instanceof ConstraintViolation) result = await processConstraintViolation(err)
28+
else result = await processOther(err)
29+
}
30+
const {stuff} = result
31+
}
32+
`
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export const input = `
2+
function foo() {
3+
return baz.then(({value}: {value: string}) => {
4+
if (value instanceof Blargh) return processBlargh(value)
5+
else return processOther(value)
6+
})
7+
}
8+
`
9+
10+
export const options = {}
11+
export const parser = 'babylon'
12+
13+
export const expected = `
14+
async function foo() {
15+
const {value}: {value: string} = await baz
16+
if (value instanceof Blargh) return processBlargh(value)
17+
else return processOther(value)
18+
}
19+
`

0 commit comments

Comments
 (0)