Skip to content

Commit

Permalink
fix: handle existing parens
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Feb 28, 2025
1 parent ac2d987 commit 0844aee
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export function transformQuansync(
id: string,
): CodeTransform | undefined {
const lang = getLang(id)
const program = babelParse(code, lang)
const program = babelParse(code, lang, {
createParenthesizedExpressions: true,
})
const imports: Record<string, ImportBinding> = Object.create(null)

for (const node of program.body) {
Expand Down
12 changes: 11 additions & 1 deletion tests/__snapshots__/transform.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export const echo = quansync({
const echoNewLine = quansync(
/** @param {string|Promise<string>} v */ function* (v) {
return ((yield echo(yield v))) + '\\n'
return (yield echo(yield v)) + '\\n'
},
)
Expand All @@ -169,3 +169,13 @@ export default async () => {
}
"
`;
exports[`transform > ./fixtures/parens.js 1`] = `
"import { quansync } from 'quansync/macro'
const parens = quansync(function* (obj) {
const config = yield (obj + 1)
return config
})
"
`;
6 changes: 6 additions & 0 deletions tests/fixtures/parens.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { quansync } from 'quansync/macro'

const parens = quansync(async (obj) => {
const config = await (obj + 1)
return config
})
2 changes: 1 addition & 1 deletion tests/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('transform', async () => {
const filePath = path.resolve(dirname, `temp/${path.basename(id)}`)
await writeFile(filePath, result)
const mod = await import(`${pathToFileURL(filePath).href}?${Date.now()}`)
await mod.default()
await mod.default?.()

return result
},
Expand Down

0 comments on commit 0844aee

Please sign in to comment.