Skip to content

Commit fc0648d

Browse files
committed
feat!: remove toGenerator helper
1 parent bf4e04e commit fc0648d

File tree

5 files changed

+27
-41
lines changed

5 files changed

+27
-41
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ For more details on usage, refer to [quansync's docs](https://github.com/antfu-c
138138
## How it works
139139

140140
`unplugin-quansync` transforms your async functions into generator functions with `quansyncMacro`,
141-
and transforms `await` into `yield * toGenerator(...)`.
141+
and transforms `await` into `yield`.
142142

143143
The example above is transformed into:
144144

145145
```ts
146146
import fs from 'node:fs'
147-
import { quansyncMacro, toGenerator } from 'quansync'
147+
import { quansyncMacro } from 'quansync'
148148

149149
// No transformations needed for objects
150150
const readFile = quansyncMacro({
@@ -154,15 +154,17 @@ const readFile = quansyncMacro({
154154

155155
// `async function` is transformed into a generator function
156156
const myFunction = quansyncMacro(function* (filename) {
157-
// `await` is transformed into `yield * toGenerator(...)`
158-
const code = yield* toGenerator(readFile(filename, 'utf8'))
157+
// `await` is transformed into `yield ...`
158+
const code = yield readFile(filename, 'utf8')
159159

160160
return `// some custom prefix\n${code}`
161161
})
162162
```
163163

164164
## Caveats
165165

166+
### Arrow functions
167+
166168
Both arrow functions and generators have been available since ES2015,
167169
but a "generator arrow function" syntax does not exist
168170
[yet](https://github.com/tc39/proposal-generator-arrow-functions).

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"prepublishOnly": "pnpm run build"
7171
},
7272
"peerDependencies": {
73-
"quansync": ">=0.0.5"
73+
"quansync": ">=0.1.0"
7474
},
7575
"dependencies": {
7676
"ast-kit": "^1.4.0",
@@ -88,7 +88,7 @@
8888
"eslint": "^9.21.0",
8989
"oxc-transform": "^0.52.0",
9090
"prettier": "^3.5.2",
91-
"quansync": "^0.0.5",
91+
"quansync": "^0.1.0",
9292
"tsdown": "^0.6.0",
9393
"tsx": "^4.19.3",
9494
"typescript": "^5.7.3",

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/index.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,10 @@ export function transformQuansync(
3939
const s = new MagicString(code)
4040
const functionScopes: boolean[] = []
4141

42-
let inject = false
43-
function injectHelper() {
44-
if (inject) return
45-
inject = true
46-
s.prepend(`import { toGenerator as _QUANSYNC_TO_GEN } from 'quansync'\n`)
47-
}
48-
4942
walkAST<t.Node>(program, {
5043
enter(node, parent) {
5144
if (node.type === 'AwaitExpression' && functionScopes.at(-1)) {
52-
injectHelper()
53-
s.overwrite(
54-
node.start!,
55-
node.argument.start!,
56-
'yield * _QUANSYNC_TO_GEN(',
57-
)
58-
s.appendLeft(node.end!, ')')
45+
s.overwrite(node.start!, node.argument.start!, 'yield ')
5946
return
6047
}
6148

tests/__snapshots__/transform.test.ts.snap

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`transform > ./fixtures/alias.js 1`] = `
4-
"import { toGenerator as _QUANSYNC_TO_GEN } from 'quansync'
5-
// @ts-check
4+
"// @ts-check
65
import { quansync, quansyncMacro as x } from 'quansync'
76
import { expect } from 'vitest'
87
@@ -13,7 +12,7 @@ export const echo = quansync({
1312
1413
const echoNewLine = x(
1514
/** @param {string|Promise<string>} v */ function* (v) {
16-
const contents = yield * _QUANSYNC_TO_GEN(echo(yield * _QUANSYNC_TO_GEN(v)))
15+
const contents = yield echo(yield v)
1716
return contents + '\\n'
1817
},
1918
)
@@ -31,8 +30,7 @@ export default async () => {
3130
`;
3231
3332
exports[`transform > ./fixtures/basic.js 1`] = `
34-
"import { toGenerator as _QUANSYNC_TO_GEN } from 'quansync'
35-
// @ts-check
33+
"// @ts-check
3634
import { quansyncMacro } from 'quansync'
3735
import { expect } from 'vitest'
3836
@@ -42,43 +40,43 @@ export const getNumber = quansyncMacro({
4240
})
4341
4442
const inc1 = quansyncMacro(function* (){
45-
const value = yield * _QUANSYNC_TO_GEN(getNumber(1))
43+
const value = yield getNumber(1)
4644
return value + 1
4745
})
4846
4947
const inc2 = quansyncMacro(function* (id) {
50-
const value = yield * _QUANSYNC_TO_GEN(getNumber(1))
48+
const value = yield getNumber(1)
5149
return value + 1
5250
})
5351
5452
const inc3 = quansyncMacro(function* named(){
55-
const value = yield * _QUANSYNC_TO_GEN(getNumber(1))
53+
const value = yield getNumber(1)
5654
return value + 1
5755
})
5856
5957
const inc4 = quansyncMacro(function* named(id) {
60-
const value = yield * _QUANSYNC_TO_GEN(getNumber(1))
58+
const value = yield getNumber(1)
6159
return value + 1
6260
})
6361
6462
const inc5 = quansyncMacro(() => {
6563
return function* () {
66-
const value = yield * _QUANSYNC_TO_GEN(getNumber(1))
64+
const value = yield getNumber(1)
6765
return value + 1
6866
}.call(this)
6967
})
7068
7169
const inc6 = quansyncMacro((id) => {
7270
return function* () {
73-
const value = yield * _QUANSYNC_TO_GEN(getNumber(1))
71+
const value = yield getNumber(1)
7472
return value + 1
7573
}.call(this)
7674
})
7775
7876
export const fn7 = quansyncMacro(() => {
7977
return function* () {
80-
yield * _QUANSYNC_TO_GEN(1)
81-
yield * _QUANSYNC_TO_GEN(2)
78+
yield 1
79+
yield 2
8280
const fn = async () => {
8381
await 3
8482
}
@@ -108,8 +106,7 @@ export default async () => {
108106
`;
109107
110108
exports[`transform > ./fixtures/inlined-arrow.js 1`] = `
111-
"import { toGenerator as _QUANSYNC_TO_GEN } from 'quansync'
112-
// @ts-check
109+
"// @ts-check
113110
import { quansyncMacro } from 'quansync'
114111
import { expect } from 'vitest'
115112
@@ -122,7 +119,7 @@ const echoNewLine = quansyncMacro(
122119
/** @param {string|Promise<string>} v */ (v) =>
123120
{
124121
return function* () {
125-
return (yield * _QUANSYNC_TO_GEN(echo(yield * _QUANSYNC_TO_GEN(v)))) + '\\n'
122+
return (yield echo(yield v)) + '\\n'
126123
}.call(this)
127124
},
128125
)

0 commit comments

Comments
 (0)