fix: invalid identifier in manifest (#2070)

This commit is contained in:
Marvin Hagemeister
2023-11-27 11:20:40 +01:00
committed by GitHub
parent 81e71adad6
commit 9d5e916e4c
9 changed files with 40 additions and 11 deletions
+16 -3
View File
@@ -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<string>) {
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;
+3
View File
@@ -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", () => {
+4
View File
@@ -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";
+2 -5
View File
@@ -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 = unknown> = T & ServeHandlerInfo & {
isPartial: boolean;