File tree Expand file tree Collapse file tree 3 files changed +62
-3
lines changed Expand file tree Collapse file tree 3 files changed +62
-3
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,9 @@ import {
14
14
} from 'magic-string-ast'
15
15
import type * as t from '@babel/types'
16
16
17
+ const ARROW_FN_START = `\nreturn function* () {`
18
+ const ARROW_FN_END = `}.call(this)\n`
19
+
17
20
export function transformQuansync (
18
21
code : string ,
19
22
id : string ,
@@ -75,10 +78,11 @@ export function transformQuansync(
75
78
}
76
79
77
80
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 )
80
83
} 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 } }` )
82
86
}
83
87
} else if ( firstParam ) {
84
88
s . overwrite ( node . start ! , firstParam . start ! , `function* ${ name } (` )
Original file line number Diff line number Diff line change @@ -106,3 +106,35 @@ export default async () => {
106
106
}
107
107
"
108
108
`;
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
+ `;
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments