File tree 5 files changed +71
-7
lines changed
5 files changed +71
-7
lines changed Original file line number Diff line number Diff line change @@ -8,9 +8,10 @@ import unwindPromiseChain from './util/unwindPromiseChain'
8
8
import finalCleanup from './util/finalCleanup'
9
9
import codeLength from './util/codeLength'
10
10
import babelBugWorkarounds from './util/babelBugWorkarounds'
11
+ import isGetterOrSetter from './util/isGetterOrSetter'
11
12
12
13
function asyncifyFunction ( path : NodePath < t . Function > ) : void {
13
- if ( returnsOrAwaitsPromises ( path ) ) {
14
+ if ( returnsOrAwaitsPromises ( path ) && ! isGetterOrSetter ( path ) ) {
14
15
path . node . async = true
15
16
}
16
17
const chains = findPromiseChains ( path )
Original file line number Diff line number Diff line change
1
+ import * as t from '@babel/types'
2
+ import { NodePath } from '@babel/traverse'
3
+
4
+ export default function isGetterOrSetter (
5
+ path : NodePath < t . Function > | null
6
+ ) : boolean {
7
+ return (
8
+ path !== null &&
9
+ ( path . isObjectMethod ( ) || path . isClassMethod ( ) ) &&
10
+ ( path . node . kind === 'get' || path . node . kind === 'set' )
11
+ )
12
+ }
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import iterateChain from './iterateChain'
5
5
import getThenHandler from './getThenHandler'
6
6
import getCatchHandler from './getCatchHandler'
7
7
import getFinallyHandler from './getFinallyHandler'
8
+ import isGetterOrSetter from './isGetterOrSetter'
8
9
9
10
function chainLength ( path : NodePath < t . CallExpression > ) : number {
10
11
let length = 0
@@ -36,9 +37,10 @@ export default function shouldIgnoreChain(
36
37
) : boolean {
37
38
const { parentPath } = path
38
39
if (
39
- ! parentPath . isReturnStatement ( ) &&
40
- ! parentPath . isAwaitExpression ( ) &&
41
- ! parentPath . isFunction ( )
40
+ ( ! parentPath . isReturnStatement ( ) &&
41
+ ! parentPath . isAwaitExpression ( ) &&
42
+ ! parentPath . isFunction ( ) ) ||
43
+ isGetterOrSetter ( path . getFunctionParent ( ) )
42
44
) {
43
45
if ( chainLength ( path ) <= 2 && ! hasComplexHandlers ( path ) ) return true
44
46
}
Original file line number Diff line number Diff line change @@ -9,14 +9,16 @@ import { unwindThen } from './unwindThen'
9
9
import unwindFinally from './unwindFinally'
10
10
import parentStatement from './parentStatement'
11
11
import replaceWithImmediatelyInvokedAsyncArrowFunction from './replaceWithImmediatelyInvokedAsyncArrowFunction'
12
+ import isGetterOrSetter from './isGetterOrSetter'
12
13
13
14
export default function unwindPromiseChain (
14
15
path : NodePath < t . CallExpression >
15
16
) : void {
16
17
if (
17
- ! path . parentPath . isAwaitExpression ( ) &&
18
- ! path . parentPath . isReturnStatement ( ) &&
19
- ! path . parentPath . isFunction ( )
18
+ ( ! path . parentPath . isAwaitExpression ( ) &&
19
+ ! path . parentPath . isReturnStatement ( ) &&
20
+ ! path . parentPath . isFunction ( ) ) ||
21
+ isGetterOrSetter ( path . getFunctionParent ( ) )
20
22
) {
21
23
path = replaceWithImmediatelyInvokedAsyncArrowFunction ( path ) [ 1 ]
22
24
}
Original file line number Diff line number Diff line change
1
+ export const input = `
2
+ class A {
3
+ method() {return p.then(x => f(x))}
4
+ get prop() {return p.then(x => f(x))}
5
+ 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))}
7
+ }
8
+ const obj = {
9
+ method() {return p.then(x => f(x))},
10
+ get prop() {return p.then(x => f(x))},
11
+ set prop(val) {return p.then(x => f(x))}
12
+ };
13
+ `
14
+ export const expected = `
15
+ class A {
16
+ async method() {
17
+ const x = await p;
18
+ return f(x);
19
+ }
20
+ get prop() {
21
+ return p.then(x => f(x))
22
+ }
23
+ set prop(val) {
24
+ return p.then(x => f(x))
25
+ }
26
+ get longchain() {
27
+ return (async () => {
28
+ const x = await p
29
+ const y = await f(x)
30
+ const z = await g(y)
31
+ return await h(z)
32
+ })()
33
+ }
34
+ }
35
+ const obj = {
36
+ async method() {
37
+ const x = await p;
38
+ return f(x);
39
+ },
40
+ get prop() {
41
+ return p.then(x => f(x))
42
+ },
43
+ set prop(val) {
44
+ return p.then(x => f(x))
45
+ }
46
+ };
47
+ `
You can’t perform that action at this time.
0 commit comments