fix: account for JS syntax when generating island names (#3341)

Fixes https://github.com/denoland/fresh/issues/3339
This commit is contained in:
Marvin Hagemeister
2025-09-05 11:33:50 +02:00
committed by GitHub
parent 892aefbf94
commit 971163ff66
2 changed files with 104 additions and 1 deletions
+21 -1
View File
@@ -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$__");
});