Skip to content

Commit

Permalink
chore: normalize error messages, move tests to getRouteSegments
Browse files Browse the repository at this point in the history
Signed-off-by: Logan McAnsh <logan@mcan.sh>
  • Loading branch information
mcansh committed Jan 5, 2023
1 parent df340a4 commit 3f3129e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 50 deletions.
83 changes: 36 additions & 47 deletions packages/remix-dev/__tests__/flat-routes-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,46 @@ describe("flatRoutes", () => {
});
}

let invalidRoutes = [
let invalidSlashFiles = [
"($[$dollabills]).([.]lol)[/](what)/([$]).$",
"$[$dollabills].[.]lol[/]what/[$].$",
];

for (let invalid of invalidRoutes) {
expect(() => getRouteSegments(invalid)).toThrow(
"Route segment cannot contain a slash"
);
for (let invalid of invalidSlashFiles) {
test("should error when using `/` in a route segment", () => {
let regex = new RegExp(
/Route segment (".*?") for (".*?") cannot contain "\/"/
);
expect(() => getRouteSegments(invalid)).toThrow(regex);
});
}

let invalidSplatFiles: string[] = [
"routes/about.[*].tsx",
"routes/about.*.tsx",
];

for (let invalid of invalidSplatFiles) {
test("should error when using `*` in a route segment", () => {
let regex = new RegExp(
/Route segment (".*?") for (".*?") cannot contain "\*"/
);
expect(() => getRouteSegments(invalid)).toThrow(regex);
});
}

let invalidParamFiles: string[] = [
"routes/about.[:name].tsx",
"routes/about.:name.tsx",
];

for (let invalid of invalidParamFiles) {
test("should error when using `:` in a route segment", () => {
let regex = new RegExp(
/Route segment (".*?") for (".*?") cannot contain ":"/
);
expect(() => getRouteSegments(invalid)).toThrow(regex);
});
}
});

Expand Down Expand Up @@ -582,46 +613,4 @@ describe("flatRoutes", () => {
});
}
});

let invalidSlashFiles: string[] = [
"routes/($[$dollabills]).([.]lol)[/](what)/([$]).$.tsx",
];

for (let file of invalidSlashFiles) {
test("should error when using `/` in a route segment", () => {
expect(() =>
flatRoutesUniversal(APP_DIR, [path.join(APP_DIR, file)])
).toThrowError(`Route segment cannot contain a slash`);
});
}

let invalidSplatFiles: string[] = [
"routes/about.[*].tsx",
"routes/about.*.tsx",
];

for (let file of invalidSplatFiles) {
test("should error when using `*` in a route segment", () => {
expect(() =>
flatRoutesUniversal(APP_DIR, [path.join(APP_DIR, file)])
).toThrowError(
`Route segment for "${path.parse(file).name}" cannot contain "*"`
);
});
}

let invalidParamFiles: string[] = [
"routes/about.[:name].tsx",
"routes/about.:name.tsx",
];

for (let file of invalidParamFiles) {
test("should error when using `:` in a route segment", () => {
expect(() =>
flatRoutesUniversal(APP_DIR, [path.join(APP_DIR, file)])
).toThrowError(
`Route segment for "${path.parse(file).name}" cannot contain ":"`
);
});
}
});
10 changes: 7 additions & 3 deletions packages/remix-dev/config/flat-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,20 @@ export function getRouteSegments(routeId: string) {
if (!routeSegment) return;

if (rawRouteSegment === "*") {
throw new Error(`Route segment for "${routeId}" cannot contain "*"`);
throw new Error(
`Route segment "${rawRouteSegment}" for "${routeId}" cannot contain "*"`
);
}

if (rawRouteSegment.includes(":")) {
throw new Error(`Route segment for "${routeId}" cannot contain ":"`);
throw new Error(
`Route segment "${rawRouteSegment}" for "${routeId}" cannot contain ":"`
);
}

if (rawRouteSegment.includes("/")) {
throw new Error(
`Route segment cannot contain a slash: ${routeSegment} (in route ${routeId})`
`Route segment "${routeSegment}" for "${routeId}" cannot contain "/"`
);
}
routeSegments.push(routeSegment);
Expand Down

0 comments on commit 3f3129e

Please sign in to comment.