Skip to content

Commit a6cb465

Browse files
committed
feat: support inline arrow function
1 parent d3e6430 commit a6cb465

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

src/core/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import {
1414
} from 'magic-string-ast'
1515
import type * as t from '@babel/types'
1616

17+
const ARROW_FN_START = `\nreturn function* () {`
18+
const ARROW_FN_END = `}.call(this)\n`
19+
1720
export function transformQuansync(
1821
code: string,
1922
id: string,
@@ -75,10 +78,11 @@ export function transformQuansync(
7578
}
7679

7780
if (node.body.type === 'BlockStatement') {
78-
s.appendLeft(node.body.start! + 1, `\nreturn function* () {`)
79-
s.appendLeft(node.body.end! - 1, `}.call(this)\n`)
81+
s.appendLeft(node.body.start! + 1, ARROW_FN_START)
82+
s.appendLeft(node.body.end! - 1, ARROW_FN_END)
8083
} else {
81-
throw new SyntaxError('Inlined arrow function is not supported')
84+
s.appendLeft(node.body.start!, `{${ARROW_FN_START}\nreturn `)
85+
s.appendLeft(node.body.end!, `\n${ARROW_FN_END}}`)
8286
}
8387
} else if (firstParam) {
8488
s.overwrite(node.start!, firstParam.start!, `function* ${name}(`)

tests/__snapshots__/transform.test.ts.snap

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,35 @@ export default async () => {
106106
}
107107
"
108108
`;
109+
110+
exports[`transform > ./fixtures/inlined-arrow.js 1`] = `
111+
"import { toGenerator as _QUANSYNC_TO_GEN } from 'quansync'
112+
// @ts-check
113+
import { quansyncMacro } from 'quansync'
114+
import { expect } from 'vitest'
115+
116+
export const echo = quansyncMacro({
117+
sync: /** @param {string} v */ (v) => v,
118+
async: (v) => Promise.resolve(v),
119+
})
120+
121+
const echoNewLine = quansyncMacro(
122+
/** @param {string|Promise<string>} v */ (v) =>
123+
{
124+
return function* () {
125+
return (yield * _QUANSYNC_TO_GEN(echo(yield * _QUANSYNC_TO_GEN(v)))) + '\\n'
126+
}.call(this)
127+
},
128+
)
129+
130+
export default async () => {
131+
const iter = echoNewLine('hello')
132+
expect(iter).a('Generator')
133+
await expect(iter).resolves.toBe('hello\\n')
134+
expect(echoNewLine.sync('world')).toBe('world\\n')
135+
136+
await expect(echoNewLine(Promise.resolve('hello'))).resolves.toBe('hello\\n')
137+
expect(() => echoNewLine.sync(Promise.resolve('world'))).throw()
138+
}
139+
"
140+
`;

tests/fixtures/inlined-arrow.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @ts-check
2+
import { quansyncMacro } from 'quansync'
3+
import { expect } from 'vitest'
4+
5+
export const echo = quansyncMacro({
6+
sync: /** @param {string} v */ (v) => v,
7+
async: (v) => Promise.resolve(v),
8+
})
9+
10+
const echoNewLine = quansyncMacro(
11+
/** @param {string|Promise<string>} v */ async (v) =>
12+
(await echo(await v)) + '\n',
13+
)
14+
15+
export default async () => {
16+
const iter = echoNewLine('hello')
17+
expect(iter).a('Generator')
18+
await expect(iter).resolves.toBe('hello\n')
19+
expect(echoNewLine.sync('world')).toBe('world\n')
20+
21+
await expect(echoNewLine(Promise.resolve('hello'))).resolves.toBe('hello\n')
22+
expect(() => echoNewLine.sync(Promise.resolve('world'))).throw()
23+
}

0 commit comments

Comments
 (0)