1 import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils"
2 import { ADMIN_SDK_SOURCE } from "../../constants"
3 import { createRule } from "../../create-rule"
4 import { isDynamicUiRoutePath } from "../../util/admin-scope"
5
6 type MessageIds = "configOnDynamicRoute"
7
8 const DEFINE_ROUTE_CONFIG = "defineRouteConfig"
9
10 export const rule = createRule<[], MessageIds>({
11 name: "no-config-on-dynamic-ui-route",
12 meta: {
13 type: "suggestion",
14 docs: {
15 description:
16 "Dynamic UI routes (e.g. `[id]/page.tsx`) must not use `defineRouteConfig` — they aren't added to the sidebar.",
17 },
18 messages: {
19 configOnDynamicRoute:
20 "Dynamic UI routes aren't added to the sidebar, so `defineRouteConfig` has no effect here and the framework warns at runtime. Remove it from this dynamic route.",
21 },
22 schema: [],
23 },
24 defaultOptions: [],
25 create(context) {
26 // Only dynamic routes are affected — a non-dynamic route's `defineRouteConfig`
27 // is fine (and enforced by `ui-route-config-via-define-route-config`).
28 if (!isDynamicUiRoutePath(context.filename)) {
29 return {}
30 }
31
32 const defineRouteConfigLocalNames = new Set<string>()
33 // Every `defineRouteConfig(...)` call we find, reported at `Program:exit`
34 // so import tracking (which may appear after a call in source order) is
35 // complete before we decide.
36 const callNodes: TSESTree.CallExpression[] = []
37
38 return {
39 ImportDeclaration(node: TSESTree.ImportDeclaration) {
40 if (node.source.value !== ADMIN_SDK_SOURCE) {
41 return
42 }
43 for (const spec of node.specifiers) {
44 if (
45 spec.type === AST_NODE_TYPES.ImportSpecifier &&
46 spec.imported.type === AST_NODE_TYPES.Identifier &&
47 spec.imported.name === DEFINE_ROUTE_CONFIG
48 ) {
49 defineRouteConfigLocalNames.add(spec.local.name)
50 }
51 }
52 },
53 CallExpression(node: TSESTree.CallExpression) {
54 if (node.callee.type === AST_NODE_TYPES.Identifier) {
55 callNodes.push(node)
56 }
57 },
58 "Program:exit"() {
59 for (const node of callNodes) {
60 const callee = node.callee as TSESTree.Identifier
61 if (defineRouteConfigLocalNames.has(callee.name)) {
62 context.report({ node, messageId: "configOnDynamicRoute" })
63 }
64 }
65 },
66 }
67 },
68 })
69
70 export default rule