From 971163ff66ae8c046f1f03a929ee8ab446b40963 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 5 Sep 2025 11:33:50 +0200 Subject: [PATCH] fix: account for JS syntax when generating island names (#3341) Fixes https://github.com/denoland/fresh/issues/3339 --- packages/fresh/src/utils.ts | 83 ++++++++++++++++++++++++++++++++ packages/fresh/src/utils_test.ts | 22 ++++++++- 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/packages/fresh/src/utils.ts b/packages/fresh/src/utils.ts index 0f0a4090..ee2ebf64 100644 --- a/packages/fresh/src/utils.ts +++ b/packages/fresh/src/utils.ts @@ -54,6 +54,15 @@ export class UniqueNamer { #seen = new Map(); getUniqueName(name: string): string { + // These names are only internal, so we can convert everything to + // plain ASCII. + name = name.replaceAll(/([^A-Za-z0-9_$]+)/g, "_"); + + // Identifiers must not start with a number + if (/^\d/.test(name) || JS_RESERVED.has(name)) { + name = "_" + name; + } + const count = this.#seen.get(name); if (count === undefined) { this.#seen.set(name, 1); @@ -66,6 +75,80 @@ export class UniqueNamer { } } +// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#reserved_words +const JS_RESERVED = new Set([ + "break", + "case", + "catch", + "class", + "const", + "continue", + "debugger", + "default", + "delete", + "do", + "else", + "export", + "extends", + "false", + "finally", + "for", + "function", + "if", + "import", + "in", + "instanceof", + "new", + "null", + "return", + "super", + "switch", + "this", + "throw", + "true", + "try", + "typeof", + "var", + "void", + "while", + "with", + "let", + "static", + "yield", + "await", + "enum", + "implements", + "interface", + "package", + "private", + "protected", + "public", + "abstract", + "boolean", + "byte", + "char", + "double", + "final", + "float", + "goto", + "int", + "long", + "native", + "short", + "synchronized", + "throws", + "transient", + "volatile", + "arguments", + "as", + "async", + "eval", + "from", + "get", + "of", + "set", +]); + const PATH_TO_SPEC = /[\\/]+/g; export function pathToSpec(outDir: string, spec: string): string { const outDirUrl = pathToFileUrl(outDir); diff --git a/packages/fresh/src/utils_test.ts b/packages/fresh/src/utils_test.ts index cdb45f3a..4624842e 100644 --- a/packages/fresh/src/utils_test.ts +++ b/packages/fresh/src/utils_test.ts @@ -1,5 +1,10 @@ import { expect } from "@std/expect"; -import { escapeScript, pathToExportName, pathToSpec } from "./utils.ts"; +import { + escapeScript, + pathToExportName, + pathToSpec, + UniqueNamer, +} from "./utils.ts"; Deno.test("filenameToExportName", () => { expect(pathToExportName("/islands/foo.tsx")).toBe("foo"); @@ -67,3 +72,18 @@ Deno.test("pathToSpec", () => { ); expect(pathToSpec("/foo", "jsr:@foo/bar")).toEqual("jsr:@foo/bar"); }); + +Deno.test("UniqueNamer - generates unique name for same names", () => { + const namer = new UniqueNamer(); + expect(namer.getUniqueName("foo")).toEqual("foo"); + expect(namer.getUniqueName("foo")).toEqual("foo_1"); +}); + +Deno.test("UniqueNamer - accounts for JS syntax", () => { + const namer = new UniqueNamer(); + expect(namer.getUniqueName("foo-bar")).toEqual("foo_bar"); + expect(namer.getUniqueName("switch")).toEqual("_switch"); + expect(namer.getUniqueName("for")).toEqual("_for"); + expect(namer.getUniqueName("0foo")).toEqual("_0foo"); + expect(namer.getUniqueName("0foo$_🔥")).toEqual("_0foo$__"); +});