Skip to content

Commit

Permalink
chore: error when using : or *, not just escaping
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 7515c19 commit df340a4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 43 deletions.
69 changes: 36 additions & 33 deletions packages/remix-dev/__tests__/flat-routes-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,42 +583,45 @@ describe("flatRoutes", () => {
}
});

test("should error when using `/` in a route segment", () => {
let files: string[] = [
"routes/($[$dollabills]).([.]lol)[/](what)/([$]).$.tsx",
];
let invalidSlashFiles: string[] = [
"routes/($[$dollabills]).([.]lol)[/](what)/([$]).$.tsx",
];

expect(() =>
flatRoutesUniversal(
APP_DIR,
files.map((file) => path.join(APP_DIR, file))
)
).toThrowError(
`Route segment cannot contain a slash: .lol?/what? (in route ($[$dollabills]).([.]lol)[/](what)/([$]).$)`
);
});
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`);
});
}

test("should error when using `*` in an escaped route segment", () => {
let files: string[] = ["routes/about.[*].tsx"];
let invalidSplatFiles: string[] = [
"routes/about.[*].tsx",
"routes/about.*.tsx",
];

expect(() =>
flatRoutesUniversal(
APP_DIR,
files.map((file) => path.join(APP_DIR, file))
)
).toThrowError(`Escaped route segment for "about.[*]" cannot contain "*"`);
});
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 "*"`
);
});
}

test("should error when using `:` in an escaped route segment", () => {
let files: string[] = ["routes/about.[:name].tsx"];
let invalidParamFiles: string[] = [
"routes/about.[:name].tsx",
"routes/about.:name.tsx",
];

expect(() =>
flatRoutesUniversal(
APP_DIR,
files.map((file) => path.join(APP_DIR, file))
)
).toThrowError(
`Escaped route segment for "about.[:name]" cannot contain ":"`
);
});
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 ":"`
);
});
}
});
18 changes: 8 additions & 10 deletions packages/remix-dev/config/flat-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ export function getRouteSegments(routeId: string) {
let pushRouteSegment = (routeSegment: string) => {
if (!routeSegment) return;

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

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

if (rawRouteSegment.includes("/")) {
throw new Error(
`Route segment cannot contain a slash: ${routeSegment} (in route ${routeId})`
Expand Down Expand Up @@ -172,16 +180,6 @@ export function getRouteSegments(routeId: string) {
}
case "ESCAPE": {
if (char === escapeEnd) {
if (rawRouteSegment === "*") {
throw new Error(
`Escaped route segment for "${routeId}" cannot contain "*"`
);
}
if (rawRouteSegment.includes(":")) {
throw new Error(
`Escaped route segment for "${routeId}" cannot contain ":"`
);
}
state = "NORMAL";
break;
}
Expand Down

0 comments on commit df340a4

Please sign in to comment.