From 054ffbb44a9bf8864e86c5e787c6f6b984fb5f0c Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Thu, 30 Nov 2023 18:14:59 +0100 Subject: [PATCH] fix: dots in island + route name not working (#2105) --- src/dev/manifest.ts | 22 ++-------------------- src/server/fs_extract.ts | 3 ++- src/server/init_safe_deps.ts | 25 +++++++++++++++++++++++++ tests/fixture/fresh.gen.ts | 4 ++++ tests/fixture/islands/Foo.Bar.tsx | 9 +++++++++ tests/fixture/routes/foo.bar.tsx | 5 +++++ tests/main_test.ts | 7 +++++++ 7 files changed, 54 insertions(+), 21 deletions(-) create mode 100644 tests/fixture/islands/Foo.Bar.tsx create mode 100644 tests/fixture/routes/foo.bar.tsx diff --git a/src/dev/manifest.ts b/src/dev/manifest.ts index 05c98076..75850559 100644 --- a/src/dev/manifest.ts +++ b/src/dev/manifest.ts @@ -1,7 +1,4 @@ -import { - isIdentifierChar, - isIdentifierStart, -} from "../server/init_safe_deps.ts"; +import { stringToIdentifier } from "../server/init_safe_deps.ts"; import { extname, join, normalize } from "./deps.ts"; /** @@ -26,22 +23,7 @@ export function specifierToIdentifier(specifier: string, used: Set) { if (ext) specifier = specifier.slice(0, -ext.length); // Turn the specifier into a readable JS identifier - let 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]; - } - } + let ident = stringToIdentifier(specifier); if (used.has(ident)) { let check = ident; diff --git a/src/server/fs_extract.ts b/src/server/fs_extract.ts index 2078e3f3..4cfbcab0 100644 --- a/src/server/fs_extract.ts +++ b/src/server/fs_extract.ts @@ -32,6 +32,7 @@ import { } from "./deps.ts"; import { BUILD_ID } from "./build_id.ts"; import { toBaseRoute } from "./compose.ts"; +import { stringToIdentifier } from "./init_safe_deps.ts"; export const ROOT_BASE_ROUTE = toBaseRoute("/"); @@ -525,7 +526,7 @@ function toPascalCase(text: string): string { } function sanitizeIslandName(name: string): string { - const fileName = name.replaceAll(/[/\\\\\(\)\[\]]/g, "_"); + const fileName = stringToIdentifier(name); return toPascalCase(fileName); } diff --git a/src/server/init_safe_deps.ts b/src/server/init_safe_deps.ts index f51b9032..6a2a1283 100644 --- a/src/server/init_safe_deps.ts +++ b/src/server/init_safe_deps.ts @@ -7,3 +7,28 @@ export { isIdentifierChar, isIdentifierStart, } from "https://esm.sh/@babel/helper-validator-identifier@7.22.20"; +import { + isIdentifierChar, + isIdentifierStart, +} from "https://esm.sh/@babel/helper-validator-identifier@7.22.20"; + +export function stringToIdentifier(str: string): string { + let ident = ""; + for (let i = 0; i < str.length; i++) { + const char = str.charCodeAt(i); + if (i === 0 && !isIdentifierStart(char)) { + ident += "_"; + if (isIdentifierChar(char)) { + ident += str[i]; + } + } else if (!isIdentifierChar(char)) { + if (ident[ident.length - 1] !== "_") { + ident += "_"; + } + } else if (ident[ident.length - 1] !== "_" || str[i] !== "_") { + ident += str[i]; + } + } + + return ident; +} diff --git a/tests/fixture/fresh.gen.ts b/tests/fixture/fresh.gen.ts index f0798363..75aec47e 100644 --- a/tests/fixture/fresh.gen.ts +++ b/tests/fixture/fresh.gen.ts @@ -26,6 +26,7 @@ import * as $event_handler_string_island from "./routes/event_handler_string_isl 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 $foo_bar from "./routes/foo.bar.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"; @@ -82,6 +83,7 @@ import * as $status_overwrite from "./routes/status_overwrite.tsx"; import * as $umlaut_äöüß from "./routes/umlaut-äöüß.tsx"; import * as $wildcard from "./routes/wildcard.tsx"; import * as $Counter from "./islands/Counter.tsx"; +import * as $Foo_Bar from "./islands/Foo.Bar.tsx"; import * as $FormIsland from "./islands/FormIsland.tsx"; import * as $Greeter from "./islands/Greeter.tsx"; import * as $HookIsland from "./islands/HookIsland.tsx"; @@ -127,6 +129,7 @@ const manifest = { "./routes/evil.tsx": $evil, "./routes/failure.ts": $failure, "./routes/foo.bar.baz.tsx": $foo_bar_baz, + "./routes/foo.bar.tsx": $foo_bar, "./routes/head_deduplicate.tsx": $head_deduplicate, "./routes/hooks-server/island.tsx": $hooks_server_island, "./routes/hooks-server/useReducer.tsx": $hooks_server_useReducer, @@ -200,6 +203,7 @@ const manifest = { }, islands: { "./islands/Counter.tsx": $Counter, + "./islands/Foo.Bar.tsx": $Foo_Bar, "./islands/FormIsland.tsx": $FormIsland, "./islands/Greeter.tsx": $Greeter, "./islands/HookIsland.tsx": $HookIsland, diff --git a/tests/fixture/islands/Foo.Bar.tsx b/tests/fixture/islands/Foo.Bar.tsx new file mode 100644 index 00000000..9554001a --- /dev/null +++ b/tests/fixture/islands/Foo.Bar.tsx @@ -0,0 +1,9 @@ +import { useEffect, useState } from "preact/hooks"; + +export default function FooBar() { + const [css, setCss] = useState(""); + useEffect(() => { + setCss("ready"); + }, []); + return

FooBar island

; +} diff --git a/tests/fixture/routes/foo.bar.tsx b/tests/fixture/routes/foo.bar.tsx new file mode 100644 index 00000000..93427a1f --- /dev/null +++ b/tests/fixture/routes/foo.bar.tsx @@ -0,0 +1,5 @@ +import FooBar from "../islands/Foo.Bar.tsx"; + +export default function Page() { + return ; +} diff --git a/tests/main_test.ts b/tests/main_test.ts index 2e88056d..cc9aa6ab 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -1001,6 +1001,13 @@ Deno.test({ }, }); +Deno.test("support dots in route + island name", async () => { + await withPageName("./tests/fixture/main.ts", async (page, address) => { + await page.goto(`${address}/foo.bar`); + await page.waitForSelector(".ready"); + }); +}); + Deno.test("De-duplicates nodes by key", async () => { await withFakeServe("./tests/fixture/main.ts", async (server) => { const res = await server.get(`/head_deduplicate`);