|
| 1 | +// Function declarations nested inside another function carrying a |
| 2 | +// function-level `"use server"` directive. |
| 3 | +// |
| 4 | +// The bubbling pre-pass turns every function declaration into |
| 5 | +// `const name = function name() {}` at the top of its block so the transform |
| 6 | +// only has to handle expression forms. The Babel original skipped the body of |
| 7 | +// each bubbled declaration, so a declaration nested inside another function |
| 8 | +// was never bubbled and a directive on it was silently ignored: the body |
| 9 | +// shipped to the client and ran there, while the capture validator (which |
| 10 | +// walks every function) still rejected its captures. These tests pin that a |
| 11 | +// nested declaration is extracted like any other marked function, in both |
| 12 | +// modes, and that hoisting still holds after the rewrite. |
| 13 | + |
| 14 | +const path = require("path"); |
| 15 | + |
| 16 | +const compilerDir = path.resolve(__dirname, ".."); |
| 17 | +const { transformDirectives } = require(compilerDir); |
| 18 | + |
| 19 | +const RUNTIME = "@solidjs/web/server-functions"; |
| 20 | +const ROOT = "/project"; |
| 21 | +const FILENAME = `${ROOT}/src/module.js`; |
| 22 | + |
| 23 | +function compile(code, overrides = {}) { |
| 24 | + return transformDirectives(code, { |
| 25 | + filename: FILENAME, |
| 26 | + root: ROOT, |
| 27 | + mode: "server", |
| 28 | + env: "production", |
| 29 | + directive: "use server", |
| 30 | + register: { kind: "named", name: "registerServerReference", source: RUNTIME }, |
| 31 | + create: { kind: "named", name: "createServerReference", source: RUNTIME }, |
| 32 | + ...overrides |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +const lines = code => code.split("\n").map(line => line.trim()); |
| 37 | + |
| 38 | +describe("nested function declarations", () => { |
| 39 | + const source = [ |
| 40 | + "export function outer() {", |
| 41 | + " async function inner() {", |
| 42 | + ' "use server";', |
| 43 | + " return 1;", |
| 44 | + " }", |
| 45 | + " return inner;", |
| 46 | + "}" |
| 47 | + ].join("\n"); |
| 48 | + |
| 49 | + it("extracts a marked declaration nested in a function (server)", () => { |
| 50 | + const result = compile(source); |
| 51 | + expect(result.valid).toBe(true); |
| 52 | + expect(result.functions.map(f => f.name)).toEqual(["outer.inner"]); |
| 53 | + const out = lines(result.code); |
| 54 | + // The body is registered at module top level and the binding inside |
| 55 | + // `outer` becomes a reference to it, exactly like a top-level function. |
| 56 | + expect(out).toContain( |
| 57 | + 'const serverFunction_1 = registerServerReference_1("outer.inner-' + |
| 58 | + hashOf(result) + |
| 59 | + '", async function inner() {' |
| 60 | + ); |
| 61 | + expect(out).toContain("const inner = createServerReference_1(serverFunction_1);"); |
| 62 | + expect(result.code).not.toMatch(/"use server"/); |
| 63 | + }); |
| 64 | + |
| 65 | + it("replaces the declaration with a proxy on the client", () => { |
| 66 | + const result = compile(source, { mode: "client" }); |
| 67 | + expect(result.valid).toBe(true); |
| 68 | + const out = lines(result.code); |
| 69 | + expect(out).toContain( |
| 70 | + `const inner = createServerReference_1("outer.inner-${hashOf(result)}");` |
| 71 | + ); |
| 72 | + // The server body is gone from the client build. |
| 73 | + expect(result.code).not.toMatch(/return 1/); |
| 74 | + expect(result.code).not.toMatch(/registerServerReference/); |
| 75 | + }); |
| 76 | + |
| 77 | + it("keeps the declaration callable before its source position", () => { |
| 78 | + // A function declaration is hoisted to the top of its scope. The bubbled |
| 79 | + // `const` lands at the top of the block, so a use above the declaration |
| 80 | + // still resolves after the rewrite. |
| 81 | + const code = [ |
| 82 | + "export function outer() {", |
| 83 | + " const ref = inner;", |
| 84 | + " async function inner() {", |
| 85 | + ' "use server";', |
| 86 | + " return 1;", |
| 87 | + " }", |
| 88 | + " return ref;", |
| 89 | + "}" |
| 90 | + ].join("\n"); |
| 91 | + const out = lines(compile(code).code); |
| 92 | + const declaration = out.indexOf("const inner = createServerReference_1(serverFunction_1);"); |
| 93 | + const use = out.indexOf("const ref = inner;"); |
| 94 | + expect(declaration).toBeGreaterThan(-1); |
| 95 | + expect(use).toBeGreaterThan(declaration); |
| 96 | + }); |
| 97 | + |
| 98 | + it("extracts through several levels of nesting", () => { |
| 99 | + const code = [ |
| 100 | + "export function outer() {", |
| 101 | + " function mid() {", |
| 102 | + ' async function inner() { "use server"; return 1; }', |
| 103 | + " return inner;", |
| 104 | + " }", |
| 105 | + " return mid;", |
| 106 | + "}" |
| 107 | + ].join("\n"); |
| 108 | + const result = compile(code); |
| 109 | + expect(result.functions.map(f => f.name)).toEqual(["outer.mid.inner"]); |
| 110 | + }); |
| 111 | + |
| 112 | + it("extracts a declaration inside a block inside a function", () => { |
| 113 | + const code = [ |
| 114 | + "export function outer(flag) {", |
| 115 | + " if (flag) {", |
| 116 | + ' async function inner() { "use server"; return 1; }', |
| 117 | + " return inner;", |
| 118 | + " }", |
| 119 | + "}" |
| 120 | + ].join("\n"); |
| 121 | + const result = compile(code); |
| 122 | + expect(result.functions.map(f => f.name)).toEqual(["outer.inner"]); |
| 123 | + }); |
| 124 | + |
| 125 | + it("rejects a nested declaration that captures an enclosing local", () => { |
| 126 | + // Extraction and validation agree: the same function that is now |
| 127 | + // extracted is the one whose captures were already being checked. |
| 128 | + const code = [ |
| 129 | + "export function outer(db) {", |
| 130 | + ' async function inner() { "use server"; return db.x; }', |
| 131 | + " return inner;", |
| 132 | + "}" |
| 133 | + ].join("\n"); |
| 134 | + expect(() => compile(code)).toThrow(/`db` is declared in an enclosing function/); |
| 135 | + }); |
| 136 | + |
| 137 | + it("leaves an unmarked nested declaration's body in place", () => { |
| 138 | + const code = [ |
| 139 | + "export function outer() {", |
| 140 | + " function helper() { return 2; }", |
| 141 | + ' const go = async () => { "use server"; return 1; };', |
| 142 | + " return [helper, go];", |
| 143 | + "}" |
| 144 | + ].join("\n"); |
| 145 | + const result = compile(code, { mode: "client" }); |
| 146 | + expect(result.functions.map(f => f.name)).toEqual(["outer.go"]); |
| 147 | + expect(result.code).toMatch(/return 2/); |
| 148 | + }); |
| 149 | +}); |
| 150 | + |
| 151 | +// The file hash is whatever the pass produced; the scheme itself is pinned |
| 152 | +// differentially in directives-id-scheme.test.js. |
| 153 | +function hashOf(result) { |
| 154 | + return result.functions[0].id.split("-")[1]; |
| 155 | +} |
0 commit comments