Skip to content

Commit a9693cb

Browse files
committed
fix: smarter auto semi insertion
1 parent e1b894e commit a9693cb

File tree

6 files changed

+40
-30
lines changed

6 files changed

+40
-30
lines changed

.prettierignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
tests/fixtures/basic.js
1+
tests/fixtures/semi.js

src/core/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function transformQuansync(
4747
function findUpExpressionStatement(): t.ExpressionStatement | undefined {
4848
for (let i = nodeStack.length - 1; i >= 0; i--) {
4949
const node = nodeStack[i]
50-
if (isFunctionType(node)) return
50+
if (isFunctionType(node) || node.type === 'BlockStatement') return
5151
if (node.type === 'ExpressionStatement') {
5252
return node
5353
}
@@ -80,7 +80,9 @@ export function transformQuansync(
8080
s.appendLeft(node.end!, ')')
8181

8282
const stmt = findUpExpressionStatement()
83-
if (stmt) prependSemi(stmt)
83+
if (stmt && stmt.start === node.start) {
84+
prependSemi(stmt)
85+
}
8486
}
8587
return
8688
}

tests/__snapshots__/transform.test.ts.snap

+16-12
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,6 @@ export const fn7 = quansync(function* () {
8888
}
8989
})
9090
91-
quansync(function* () {
92-
;!(yield 1)
93-
;'' + (yield 1)
94-
;false && (yield 1)
95-
fn7()
96-
;(yield 1) < (yield 10)
97-
const x = (yield 1) < (yield 10)
98-
})
99-
10091
export default async () => {
10192
await expect(getNumber(1)).resolves.toBe(1)
10293
expect(getNumber.sync(1)).toBe(1)
@@ -170,12 +161,25 @@ export default async () => {
170161
"
171162
`;
172163
173-
exports[`transform > ./fixtures/parens.js 1`] = `
164+
exports[`transform > ./fixtures/semi.js 1`] = `
174165
"import { quansync } from 'quansync/macro'
175166
176-
const parens = quansync(function* (obj) {
177-
const config = yield (obj + 1)
167+
quansync(function* (obj) {
168+
let config = yield (obj + 1)
169+
if (true) config = (yield 10) || 10
178170
return config
179171
})
172+
173+
quansync(function* () {
174+
!(yield 1)
175+
'' + (yield 1)
176+
false && (yield 1)
177+
fn()
178+
;(yield 1) < (yield 10)
179+
const x = (yield 1) < (yield 10)
180+
181+
;[]
182+
[yield 10]
183+
})
180184
"
181185
`;

tests/fixtures/basic.js

-9
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,6 @@ export const fn7 = quansync(async () => {
5656
}
5757
})
5858

59-
quansync(async () => {
60-
!await 1
61-
'' + await 1
62-
false && await 1
63-
fn7()
64-
await 1 < await 10
65-
const x = await 1 < await 10
66-
})
67-
6859
export default async () => {
6960
await expect(getNumber(1)).resolves.toBe(1)
7061
expect(getNumber.sync(1)).toBe(1)

tests/fixtures/parens.js

-6
This file was deleted.

tests/fixtures/semi.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { quansync } from 'quansync/macro'
2+
3+
quansync(async (obj) => {
4+
let config = await (obj + 1)
5+
if (true) config = await 10 || 10
6+
return config
7+
})
8+
9+
quansync(async () => {
10+
!await 1
11+
'' + await 1
12+
false && await 1
13+
fn()
14+
await 1 < await 10
15+
const x = await 1 < await 10
16+
17+
;[]
18+
[await 10]
19+
})

0 commit comments

Comments
 (0)