Skip to content

Commit

Permalink
feat(fs-router) add console errors for duplicated paths (#2259)
Browse files Browse the repository at this point in the history
  • Loading branch information
cromoteca authored Mar 26, 2024
1 parent 3f90fdc commit d959da5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/ts/file-router/src/vite-plugin/createRoutesFromMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,19 @@ export default function createRoutesFromMeta(views: RouteMeta, { code: codeFile
([statement]) => statement as ts.ImportDeclaration,
),
];
const errors: string[] = [];
let id = 0;

const routes = transformTreeSync<RouteMeta, CallExpression>(
views,
(view) => view.children.values(),
(view) => {
const paths = view.children.map((c) => c.path);
const uniquePaths = new Set(paths);
paths
.filter((p) => !uniquePaths.delete(p))
.forEach((p) => errors.push(`console.error("Two views share the same path: ${p}");`));
return view.children.values();
},
({ file, layout, path }, children) => {
const currentId = id;
id += 1;
Expand All @@ -101,6 +109,8 @@ export default function createRoutesFromMeta(views: RouteMeta, { code: codeFile
const routeDeclaration = template(
`import a from 'IMPORTS';
${errors.join('\n')}
const routes = ROUTE;
export default routes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,16 @@ const routes = createRoute("", [createRoute("nameToReplace", Page0), createRoute
export default routes;
`);
});

it('should add console.error calls for duplicated paths', () => {
const metaWithDuplicatedPaths = createTestingRouteMeta(new URL('./views/', dir));
metaWithDuplicatedPaths.children.push({
path: 'profile',
file: new URL('profile/$index.tsx', dir),
children: [],
});
const generated = createRoutesFromMeta(metaWithDuplicatedPaths, runtimeUrls);
expect(generated).to.contain('console.error("Two views share the same path: profile");');
});
});
});

0 comments on commit d959da5

Please sign in to comment.