From 0844aee2b93a6f7f64ef44b0598563bb49521048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Fri, 28 Feb 2025 10:55:08 +0800 Subject: [PATCH] fix: handle existing parens --- src/core/index.ts | 4 +++- tests/__snapshots__/transform.test.ts.snap | 12 +++++++++++- tests/fixtures/parens.js | 6 ++++++ tests/transform.test.ts | 2 +- 4 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/parens.js diff --git a/src/core/index.ts b/src/core/index.ts index f3039ce..53b0e9c 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -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 = Object.create(null) for (const node of program.body) { diff --git a/tests/__snapshots__/transform.test.ts.snap b/tests/__snapshots__/transform.test.ts.snap index 071195d..f6fe890 100644 --- a/tests/__snapshots__/transform.test.ts.snap +++ b/tests/__snapshots__/transform.test.ts.snap @@ -154,7 +154,7 @@ export const echo = quansync({ const echoNewLine = quansync( /** @param {string|Promise} v */ function* (v) { -return ((yield echo(yield v))) + '\\n' +return (yield echo(yield v)) + '\\n' }, ) @@ -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 +}) +" +`; diff --git a/tests/fixtures/parens.js b/tests/fixtures/parens.js new file mode 100644 index 0000000..0120502 --- /dev/null +++ b/tests/fixtures/parens.js @@ -0,0 +1,6 @@ +import { quansync } from 'quansync/macro' + +const parens = quansync(async (obj) => { + const config = await (obj + 1) + return config +}) diff --git a/tests/transform.test.ts b/tests/transform.test.ts index 48c4d99..44d932a 100644 --- a/tests/transform.test.ts +++ b/tests/transform.test.ts @@ -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 },