Skip to content

Commit 0844aee

Browse files
committed
fix: handle existing parens
1 parent ac2d987 commit 0844aee

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

src/core/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ export function transformQuansync(
2424
id: string,
2525
): CodeTransform | undefined {
2626
const lang = getLang(id)
27-
const program = babelParse(code, lang)
27+
const program = babelParse(code, lang, {
28+
createParenthesizedExpressions: true,
29+
})
2830
const imports: Record<string, ImportBinding> = Object.create(null)
2931

3032
for (const node of program.body) {

tests/__snapshots__/transform.test.ts.snap

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export const echo = quansync({
154154
155155
const echoNewLine = quansync(
156156
/** @param {string|Promise<string>} v */ function* (v) {
157-
return ((yield echo(yield v))) + '\\n'
157+
return (yield echo(yield v)) + '\\n'
158158
},
159159
)
160160
@@ -169,3 +169,13 @@ export default async () => {
169169
}
170170
"
171171
`;
172+
173+
exports[`transform > ./fixtures/parens.js 1`] = `
174+
"import { quansync } from 'quansync/macro'
175+
176+
const parens = quansync(function* (obj) {
177+
const config = yield (obj + 1)
178+
return config
179+
})
180+
"
181+
`;

tests/fixtures/parens.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { quansync } from 'quansync/macro'
2+
3+
const parens = quansync(async (obj) => {
4+
const config = await (obj + 1)
5+
return config
6+
})

tests/transform.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('transform', async () => {
2121
const filePath = path.resolve(dirname, `temp/${path.basename(id)}`)
2222
await writeFile(filePath, result)
2323
const mod = await import(`${pathToFileURL(filePath).href}?${Date.now()}`)
24-
await mod.default()
24+
await mod.default?.()
2525

2626
return result
2727
},

0 commit comments

Comments
 (0)