Skip to content

Commit 10ac51c

Browse files
committed
fix: avoid creating invalid syntax async get method
1 parent b10f84c commit 10ac51c

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

src/asyncify.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import unwindPromiseChain from './util/unwindPromiseChain'
88
import finalCleanup from './util/finalCleanup'
99
import codeLength from './util/codeLength'
1010
import babelBugWorkarounds from './util/babelBugWorkarounds'
11+
import isGetterOrSetter from './util/isGetterOrSetter'
1112

1213
function 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)

src/util/isGetterOrSetter.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}

src/util/unwindPromiseChain.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ import { unwindThen } from './unwindThen'
99
import unwindFinally from './unwindFinally'
1010
import parentStatement from './parentStatement'
1111
import replaceWithImmediatelyInvokedAsyncArrowFunction from './replaceWithImmediatelyInvokedAsyncArrowFunction'
12+
import isGetterOrSetter from './isGetterOrSetter'
1213

1314
export 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
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
`

0 commit comments

Comments
 (0)