Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export default async function Page() {}
['literal', `export const action = 1`],
['object', `export const action = {}`],
['array', `export const action = []`],
['uninitialized variable', `export let action`],
])('rejects %s exports', async (_name, input) => {
await expect(
transform(input, { rejectNonAsyncFunction: true }),
Expand Down
11 changes: 8 additions & 3 deletions packages/plugin-rsc/src/transforms/module-export-effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Identifier } from 'estree'
import MagicString from 'magic-string'
import type { ESTree } from 'vite'
import { scanModuleExports, type ModuleExportMeta } from './module-export-scan'
import { validateNonAsyncFunction } from './utils'
import { rejectNonAsyncFunction, validateNonAsyncFunction } from './utils'

// TODO: Metadata, filtering, and returned reference contexts are currently
// ported only for transformWrapExport compatibility. Remove them if no
Expand Down Expand Up @@ -128,8 +128,13 @@ export function transformModuleExportEffect(
effects.push(generate({ binding, exportName, meta }))
}
}
if (validate && declarator.node.init) {
validateNonAsyncFunction(options, declarator.node.init)
if (validate) {
const init = declarator.node.init
if (init) {
validateNonAsyncFunction(options, init)
} else {
rejectNonAsyncFunction(options, declarator.node.start)
}
}
}
if (effects.length > 0) {
Expand Down
14 changes: 10 additions & 4 deletions packages/plugin-rsc/src/transforms/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export function validateNonAsyncFunction(
// export default function/class can be unnamed
node: Node | ExportDefaultDeclaration['declaration'],
): void {
if (!opts.rejectNonAsyncFunction) return
if (
node.type === 'Literal' ||
node.type === 'ObjectExpression' ||
Expand All @@ -90,8 +89,15 @@ export function validateNonAsyncFunction(
node.type === 'ArrowFunctionExpression') &&
!node.async)
) {
throw Object.assign(new Error(`unsupported non async function`), {
pos: node.start,
})
rejectNonAsyncFunction(opts, node.start)
}
}

export function rejectNonAsyncFunction(
opts: { rejectNonAsyncFunction?: boolean },
pos: number,
): void {
if (opts.rejectNonAsyncFunction) {
throw Object.assign(new Error(`unsupported non async function`), { pos })
}
}
Loading