@@ -14,15 +14,17 @@ export type TransformWrapExportFilter = (
14
14
meta : ExportMeta ,
15
15
) => boolean
16
16
17
+ export type TransformWrapExportOptions = {
18
+ runtime : ( value : string , name : string , meta : ExportMeta ) => string
19
+ ignoreExportAllDeclaration ?: boolean
20
+ rejectNonAsyncFunction ?: boolean
21
+ filter ?: TransformWrapExportFilter
22
+ }
23
+
17
24
export function transformWrapExport (
18
25
input : string ,
19
26
ast : Program ,
20
- options : {
21
- runtime : ( value : string , name : string , meta : ExportMeta ) => string
22
- ignoreExportAllDeclaration ?: boolean
23
- rejectNonAsyncFunction ?: boolean
24
- filter ?: TransformWrapExportFilter
25
- } ,
27
+ options : TransformWrapExportOptions ,
26
28
) : {
27
29
exportNames : string [ ]
28
30
output : MagicString
@@ -81,8 +83,15 @@ export function transformWrapExport(
81
83
)
82
84
}
83
85
84
- function validateNonAsyncFunction ( node : Node , ok ?: boolean ) {
85
- if ( options . rejectNonAsyncFunction && ! ok ) {
86
+ function validateNonAsyncFunction ( node : Node ) {
87
+ if ( ! options . rejectNonAsyncFunction ) return
88
+ if (
89
+ node . type === 'ClassDeclaration' ||
90
+ ( ( node . type === 'FunctionDeclaration' ||
91
+ node . type === 'FunctionExpression' ||
92
+ node . type === 'ArrowFunctionExpression' ) &&
93
+ ! node . async )
94
+ ) {
86
95
throw Object . assign ( new Error ( `unsupported non async function` ) , {
87
96
pos : node . start ,
88
97
} )
@@ -100,11 +109,7 @@ export function transformWrapExport(
100
109
/**
101
110
* export function foo() {}
102
111
*/
103
- validateNonAsyncFunction (
104
- node ,
105
- node . declaration . type === 'FunctionDeclaration' &&
106
- node . declaration . async ,
107
- )
112
+ validateNonAsyncFunction ( node . declaration )
108
113
const name = node . declaration . id . name
109
114
wrapSimple ( node . start , node . declaration . start , [
110
115
{ name, meta : { isFunction : true , declName : name } } ,
@@ -113,14 +118,11 @@ export function transformWrapExport(
113
118
/**
114
119
* export const foo = 1, bar = 2
115
120
*/
116
- validateNonAsyncFunction (
117
- node ,
118
- node . declaration . declarations . every (
119
- ( decl ) =>
120
- decl . init ?. type === 'ArrowFunctionExpression' &&
121
- decl . init . async ,
122
- ) ,
123
- )
121
+ for ( const decl of node . declaration . declarations ) {
122
+ if ( decl . init ) {
123
+ validateNonAsyncFunction ( decl . init )
124
+ }
125
+ }
124
126
if ( node . declaration . kind === 'const' ) {
125
127
output . update (
126
128
node . declaration . start ,
@@ -201,14 +203,7 @@ export function transformWrapExport(
201
203
* export default () => {}
202
204
*/
203
205
if ( node . type === 'ExportDefaultDeclaration' ) {
204
- validateNonAsyncFunction (
205
- node ,
206
- // TODO: somehow identifier is allowed in next.js?
207
- // (see packages/react-server/examples/next/app/actions/server/actions.ts)
208
- node . declaration . type === 'Identifier' ||
209
- ( node . declaration . type === 'FunctionDeclaration' &&
210
- node . declaration . async ) ,
211
- )
206
+ validateNonAsyncFunction ( node . declaration as Node )
212
207
let localName : string
213
208
let isFunction = false
214
209
let declName : string | undefined
0 commit comments