From 9d5e916e4cfeb9d68841ed5b1c67549d5c6959b1 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Mon, 27 Nov 2023 11:20:40 +0100 Subject: [PATCH] fix: invalid identifier in manifest (#2070) --- plugins/tailwind.ts | 1 - src/dev/manifest.ts | 19 ++++++++++++++++--- src/dev/manifest_test.ts | 3 +++ src/server/deps.ts | 4 ++++ src/server/router.ts | 7 ++----- tests/fixture/fresh.gen.ts | 2 ++ tests/fixture/routes/foo.bar.baz.tsx | 3 +++ tests/render_test.ts | 10 ++++++++++ tests/www/routes/index.test.tsx | 2 -- 9 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 tests/fixture/routes/foo.bar.baz.tsx diff --git a/plugins/tailwind.ts b/plugins/tailwind.ts index 2ab3657a..d3c2e47f 100644 --- a/plugins/tailwind.ts +++ b/plugins/tailwind.ts @@ -42,7 +42,6 @@ async function initTailwind( const root = path.dirname(config.staticDir); const configPath = await findTailwindConfigFile(root); - console.log({ configPath, cwd: Deno.cwd() }); const url = path.toFileUrl(configPath).href; const tailwindConfig = (await import(url)).default as Config; diff --git a/src/dev/manifest.ts b/src/dev/manifest.ts index 97d8a86b..a5434fec 100644 --- a/src/dev/manifest.ts +++ b/src/dev/manifest.ts @@ -1,3 +1,4 @@ +import { isIdentifierChar, isIdentifierStart } from "$fresh/src/server/deps.ts"; import { extname, join, normalize } from "./deps.ts"; /** @@ -21,11 +22,23 @@ export function specifierToIdentifier(specifier: string, used: Set) { const ext = extname(specifier); if (ext) specifier = specifier.slice(0, -ext.length); + // Turn the specifier into a readable JS identifier let ident = ""; - if (/^[\d]/.test(specifier)) { - ident += "_"; + for (let i = 0; i < specifier.length; i++) { + const char = specifier.charCodeAt(i); + if (i === 0 && !isIdentifierStart(char)) { + ident += "_"; + if (isIdentifierChar(char)) { + ident += specifier[i]; + } + } else if (!isIdentifierChar(char)) { + if (ident[ident.length - 1] !== "_") { + ident += "_"; + } + } else if (ident[ident.length - 1] !== "_" || specifier[i] !== "_") { + ident += specifier[i]; + } } - ident += specifier.replace(/[\/.\(\)\[\]\s@-]/g, "_").replace(/_+/g, "_"); if (used.has(ident)) { let check = ident; diff --git a/src/dev/manifest_test.ts b/src/dev/manifest_test.ts index b2a2db13..b428dd36 100644 --- a/src/dev/manifest_test.ts +++ b/src/dev/manifest_test.ts @@ -12,6 +12,9 @@ Deno.test("specifierToIdentifier", () => { assertEquals(run("foo/[[optional]]/bar", used), "foo_optional_bar"); assertEquals(run("foo/as-df/bar", used), "foo_as_df_bar"); assertEquals(run("foo/as@df", used), "foo_as_df"); + assertEquals(run("foo/foo.bar.baz.tsx", used), "foo_foo_bar_baz"); + assertEquals(run("404", used), "_404"); + assertEquals(run("foo/_middleware", used), "foo_middleware"); }); Deno.test("specifierToIdentifier deals with duplicates", () => { diff --git a/src/server/deps.ts b/src/server/deps.ts index 13220051..8aa6283f 100644 --- a/src/server/deps.ts +++ b/src/server/deps.ts @@ -26,3 +26,7 @@ export { assertEquals, assertThrows, } from "https://deno.land/std@0.205.0/assert/mod.ts"; +export { + isIdentifierChar, + isIdentifierStart, +} from "https://esm.sh/@babel/helper-validator-identifier@7.22.20"; diff --git a/src/server/router.ts b/src/server/router.ts index 6239df71..36d916d4 100644 --- a/src/server/router.ts +++ b/src/server/router.ts @@ -1,9 +1,6 @@ -import { PARTIAL_SEARCH_PARAM } from "$fresh/src/constants.ts"; +import { PARTIAL_SEARCH_PARAM } from "../constants.ts"; import { BaseRoute, ErrorHandlerContext, ServeHandlerInfo } from "./types.ts"; -import { - isIdentifierChar, - isIdentifierStart, -} from "https://esm.sh/@babel/helper-validator-identifier@7.22.20"; +import { isIdentifierChar, isIdentifierStart } from "./deps.ts"; type HandlerContext = T & ServeHandlerInfo & { isPartial: boolean; diff --git a/tests/fixture/fresh.gen.ts b/tests/fixture/fresh.gen.ts index 47cb2338..86a53b23 100644 --- a/tests/fixture/fresh.gen.ts +++ b/tests/fixture/fresh.gen.ts @@ -23,6 +23,7 @@ import * as $event_handler_string from "./routes/event_handler_string.tsx"; import * as $event_handler_string_island from "./routes/event_handler_string_island.tsx"; import * as $evil from "./routes/evil.tsx"; import * as $failure from "./routes/failure.ts"; +import * as $foo_bar_baz from "./routes/foo.bar.baz.tsx"; import * as $head_deduplicate from "./routes/head_deduplicate.tsx"; import * as $hooks_server_island from "./routes/hooks-server/island.tsx"; import * as $hooks_server_useReducer from "./routes/hooks-server/useReducer.tsx"; @@ -121,6 +122,7 @@ const manifest = { "./routes/event_handler_string_island.tsx": $event_handler_string_island, "./routes/evil.tsx": $evil, "./routes/failure.ts": $failure, + "./routes/foo.bar.baz.tsx": $foo_bar_baz, "./routes/head_deduplicate.tsx": $head_deduplicate, "./routes/hooks-server/island.tsx": $hooks_server_island, "./routes/hooks-server/useReducer.tsx": $hooks_server_useReducer, diff --git a/tests/fixture/routes/foo.bar.baz.tsx b/tests/fixture/routes/foo.bar.baz.tsx new file mode 100644 index 00000000..1d040076 --- /dev/null +++ b/tests/fixture/routes/foo.bar.baz.tsx @@ -0,0 +1,3 @@ +export default function Page() { + return

it works

; +} diff --git a/tests/render_test.ts b/tests/render_test.ts index 8ebce692..e0c1b021 100644 --- a/tests/render_test.ts +++ b/tests/render_test.ts @@ -77,3 +77,13 @@ Deno.test("support with jsx precompile", async () => { }, ); }); + +Deno.test("Ensure manifest has valid specifiers", async () => { + await withFakeServe( + "./tests/fixture/main.ts", + async (server) => { + const doc = await server.getHtml("/foo.bar.baz"); + assertTextMany(doc, "p", ["it works"]); + }, + ); +}); diff --git a/tests/www/routes/index.test.tsx b/tests/www/routes/index.test.tsx index 3981af60..1952cd12 100644 --- a/tests/www/routes/index.test.tsx +++ b/tests/www/routes/index.test.tsx @@ -12,7 +12,6 @@ Deno.test("index.tsx route handler test...", async (t) => { const ctx = createHandlerContext(req, { manifest }); // @ts-ignore handler is not null const resp = handler.GET(req, ctx) as Response; - console.log("resp", resp); expect(resp.status).toBe(307); expect(resp.headers.get("Location")?.endsWith("init.ts")); }); @@ -31,7 +30,6 @@ Deno.test("index.tsx route handler test...", async (t) => { }; // @ts-ignore handler is not null const resp = handler.GET(req, ctx) as Response; - // console.log("resp", resp); expect(resp.status).toBe(200); }); });