Skip to content

Commit 7ab715e

Browse files
committed
fixup! fix: avoid creating invalid syntax async get method
Use the same check for isGetterOrSetter in shouldIgnoreChain as in unwindPromiseChain for consistency so that getters that return a short Promise chain are not converted to a verbose IIFE
1 parent 10ac51c commit 7ab715e

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

src/util/shouldIgnoreChain.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import iterateChain from './iterateChain'
55
import getThenHandler from './getThenHandler'
66
import getCatchHandler from './getCatchHandler'
77
import getFinallyHandler from './getFinallyHandler'
8+
import isGetterOrSetter from './isGetterOrSetter'
89

910
function chainLength(path: NodePath<t.CallExpression>): number {
1011
let length = 0
@@ -36,9 +37,10 @@ export default function shouldIgnoreChain(
3637
): boolean {
3738
const { parentPath } = path
3839
if (
39-
!parentPath.isReturnStatement() &&
40-
!parentPath.isAwaitExpression() &&
41-
!parentPath.isFunction()
40+
(!parentPath.isReturnStatement() &&
41+
!parentPath.isAwaitExpression() &&
42+
!parentPath.isFunction()) ||
43+
isGetterOrSetter(path.getFunctionParent())
4244
) {
4345
if (chainLength(path) <= 2 && !hasComplexHandlers(path)) return true
4446
}

test/fixtures/bugs_GetterAndSetterCannotBeAsync.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class A {
33
method() {return p.then(x => f(x))}
44
get prop() {return p.then(x => f(x))}
55
set prop(val) {return p.then(x => f(x))}
6+
get longchain() {return p.then(x => f(x)).then(y => g(y)).then(z => h(z))}
67
}
78
const obj = {
89
method() {return p.then(x => f(x))},
@@ -17,15 +18,17 @@ class A {
1718
return f(x);
1819
}
1920
get prop() {
20-
return (async () => {
21-
const x = await p;
22-
return await f(x);
23-
})()
21+
return p.then(x => f(x))
2422
}
2523
set prop(val) {
24+
return p.then(x => f(x))
25+
}
26+
get longchain() {
2627
return (async () => {
27-
const x = await p;
28-
return await f(x);
28+
const x = await p
29+
const y = await f(x)
30+
const z = await g(y)
31+
return await h(z)
2932
})()
3033
}
3134
}
@@ -35,16 +38,10 @@ const obj = {
3538
return f(x);
3639
},
3740
get prop() {
38-
return (async () => {
39-
const x = await p;
40-
return await f(x);
41-
})()
41+
return p.then(x => f(x))
4242
},
4343
set prop(val) {
44-
return (async () => {
45-
const x = await p;
46-
return await f(x);
47-
})()
44+
return p.then(x => f(x))
4845
}
4946
};
5047
`

0 commit comments

Comments
 (0)