Skip to content

Commit d278ef6

Browse files
committed
test: add test
1 parent 7396c9f commit d278ef6

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

packages/plugin-rsc/src/transforms/hoist.test.ts

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@ import { debugSourceMap } from './test-utils'
66
describe(transformHoistInlineDirective, () => {
77
async function testTransform(
88
input: string,
9-
options?: { encode?: boolean; noExport?: boolean; directive?: string },
9+
options?: {
10+
encode?: boolean
11+
noExport?: boolean
12+
directive?: string | RegExp
13+
},
1014
) {
1115
const ast = await parseAstAsync(input)
1216
const { output } = transformHoistInlineDirective(input, ast, {
13-
runtime: (value, name) =>
14-
`$$register(${value}, "<id>", ${JSON.stringify(name)})`,
17+
runtime: (value, name, meta) =>
18+
`$$register(${value}, "<id>", ${JSON.stringify(name)}` +
19+
`${
20+
options?.directive instanceof RegExp
21+
? `, ${JSON.stringify(meta)}`
22+
: ''
23+
})`,
1524
directive: options?.directive ?? 'use server',
1625
encode: options?.encode ? (v) => `__enc(${v})` : undefined,
1726
decode: options?.encode ? (v) => `__dec(${v})` : undefined,
@@ -369,4 +378,55 @@ export async function test() {
369378
"
370379
`)
371380
})
381+
382+
it('directive pattern', async () => {
383+
const input = `
384+
export async function none() {
385+
"use cache";
386+
return "test";
387+
}
388+
389+
export async function fs() {
390+
"use cache: fs";
391+
return "test";
392+
}
393+
394+
export async function kv() {
395+
"use cache: kv";
396+
return "test";
397+
}
398+
`
399+
expect(
400+
await testTransform(input, {
401+
directive: /^use cache(: .+)?$/,
402+
noExport: true,
403+
}),
404+
).toMatchInlineSnapshot(`
405+
"
406+
export const none = /* #__PURE__ */ $$register($$hoist_0_none, "<id>", "$$hoist_0_none", {"directiveMatch":["use cache",null]});
407+
408+
export const fs = /* #__PURE__ */ $$register($$hoist_1_fs, "<id>", "$$hoist_1_fs", {"directiveMatch":["use cache: fs",": fs"]});
409+
410+
export const kv = /* #__PURE__ */ $$register($$hoist_2_kv, "<id>", "$$hoist_2_kv", {"directiveMatch":["use cache: kv",": kv"]});
411+
412+
;async function $$hoist_0_none() {
413+
"use cache";
414+
return "test";
415+
};
416+
/* #__PURE__ */ Object.defineProperty($$hoist_0_none, "name", { value: "none" });
417+
418+
;async function $$hoist_1_fs() {
419+
"use cache: fs";
420+
return "test";
421+
};
422+
/* #__PURE__ */ Object.defineProperty($$hoist_1_fs, "name", { value: "fs" });
423+
424+
;async function $$hoist_2_kv() {
425+
"use cache: kv";
426+
return "test";
427+
};
428+
/* #__PURE__ */ Object.defineProperty($$hoist_2_kv, "name", { value: "kv" });
429+
"
430+
`)
431+
})
372432
})

packages/plugin-rsc/src/transforms/hoist.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function transformHoistInlineDirective(
2828
names: string[]
2929
} {
3030
const output = new MagicString(input)
31-
const directiveRegex =
31+
const directive =
3232
typeof options.directive === 'string'
3333
? exactRegex(options.directive)
3434
: options.directive
@@ -56,11 +56,11 @@ export function transformHoistInlineDirective(
5656
node.type === 'ArrowFunctionExpression') &&
5757
node.body.type === 'BlockStatement'
5858
) {
59-
const match = matchDirective(node.body.body, directiveRegex)
59+
const match = matchDirective(node.body.body, directive)
6060
if (!match) return
6161
if (!node.async && rejectNonAsyncFunction) {
6262
throw Object.assign(
63-
new Error(`"${directiveRegex}" doesn't allow non async function`),
63+
new Error(`"${directive}" doesn't allow non async function`),
6464
{
6565
pos: node.start,
6666
},
@@ -151,7 +151,7 @@ export function transformHoistInlineDirective(
151151
}
152152

153153
const exactRegex = (s: string): RegExp =>
154-
new RegExp('^' + s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&') + '$', 'u')
154+
new RegExp('^' + s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&') + '$')
155155

156156
function matchDirective(
157157
body: Program['body'],
@@ -161,8 +161,7 @@ function matchDirective(
161161
if (
162162
stable.type === 'ExpressionStatement' &&
163163
stable.expression.type === 'Literal' &&
164-
typeof stable.expression.value === 'string' &&
165-
stable.expression.value.match(directive)
164+
typeof stable.expression.value === 'string'
166165
) {
167166
const match = stable.expression.value.match(directive)
168167
if (match) {

0 commit comments

Comments
 (0)