File tree Expand file tree Collapse file tree 4 files changed +69
-4
lines changed Expand file tree Collapse file tree 4 files changed +69
-4
lines changed Original file line number Diff line number Diff line change @@ -8,9 +8,10 @@ import unwindPromiseChain from './util/unwindPromiseChain'
88import finalCleanup from './util/finalCleanup'
99import codeLength from './util/codeLength'
1010import babelBugWorkarounds from './util/babelBugWorkarounds'
11+ import isGetterOrSetter from './util/isGetterOrSetter'
1112
1213function asyncifyFunction ( path : NodePath < t . Function > ) : void {
13- if ( returnsOrAwaitsPromises ( path ) ) {
14+ if ( returnsOrAwaitsPromises ( path ) && ! isGetterOrSetter ( path ) ) {
1415 path . node . async = true
1516 }
1617 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 @@ -9,14 +9,16 @@ import { unwindThen } from './unwindThen'
99import unwindFinally from './unwindFinally'
1010import parentStatement from './parentStatement'
1111import replaceWithImmediatelyInvokedAsyncArrowFunction from './replaceWithImmediatelyInvokedAsyncArrowFunction'
12+ import isGetterOrSetter from './isGetterOrSetter'
1213
1314export default function unwindPromiseChain (
1415 path : NodePath < t . CallExpression >
1516) : void {
1617 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 ( ) )
2022 ) {
2123 path = replaceWithImmediatelyInvokedAsyncArrowFunction ( path ) [ 1 ]
2224 }
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+ }
7+ const obj = {
8+ method() {return p.then(x => f(x))},
9+ get prop() {return p.then(x => f(x))},
10+ set prop(val) {return p.then(x => f(x))}
11+ };
12+ `
13+ export const expected = `
14+ class A {
15+ async method() {
16+ const x = await p;
17+ return f(x);
18+ }
19+ get prop() {
20+ return (async () => {
21+ const x = await p;
22+ return await f(x);
23+ })()
24+ }
25+ set prop(val) {
26+ return (async () => {
27+ const x = await p;
28+ return await f(x);
29+ })()
30+ }
31+ }
32+ const obj = {
33+ async method() {
34+ const x = await p;
35+ return f(x);
36+ },
37+ get prop() {
38+ return (async () => {
39+ const x = await p;
40+ return await f(x);
41+ })()
42+ },
43+ set prop(val) {
44+ return (async () => {
45+ const x = await p;
46+ return await f(x);
47+ })()
48+ }
49+ };
50+ `
You can’t perform that action at this time.
0 commit comments