|
1
|
import * as path from "path" |
|
2
|
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils" |
|
3
|
import { createRule } from "../../create-rule" |
|
4
|
import { toPosix } from "../../util/filename" |
|
5
|
|
|
6
|
type MessageIds = "wrongFileName" |
|
7
|
|
|
8
|
const ADMIN_ROUTES_RE = /(?:^|\/)src\/admin\/routes\// |
|
9
|
const ADMIN_ROUTES_RE_NO_SRC = /(?:^|\/)admin\/routes\// |
|
10
|
const PAGE_BASENAMES = new Set(["page.tsx", "page.jsx"]) |
|
11
|
const ADMIN_SDK_SOURCE = "@medusajs/admin-sdk" |
|
12
|
const DEFINE_ROUTE_CONFIG = "defineRouteConfig" |
|
13
|
|
|
14
|
function isUnderAdminRoutesDir(filename: string): boolean { |
|
15
|
const posix = toPosix(filename) |
|
16
|
return ADMIN_ROUTES_RE.test(posix) || ADMIN_ROUTES_RE_NO_SRC.test(posix) |
|
17
|
} |
|
18
|
|
|
19
|
export const rule = createRule<[], MessageIds>({ |
|
20
|
name: "ui-route-file-name-page-tsx", |
|
21
|
meta: { |
|
22
|
type: "problem", |
|
23
|
docs: { |
|
24
|
description: |
|
25
|
"UI route files under `src/admin/routes/` must be named `page.tsx`.", |
|
26
|
}, |
|
27
|
messages: { |
|
28
|
wrongFileName: |
|
29
|
"UI route files under `src/admin/routes/` must be named `page.tsx`. Rename this file to `page.tsx`.", |
|
30
|
}, |
|
31
|
schema: [], |
|
32
|
}, |
|
33
|
defaultOptions: [], |
|
34
|
create(context) { |
|
35
|
const filename = context.filename |
|
36
|
if (!filename || filename.startsWith("<")) { |
|
37
|
return {} |
|
38
|
} |
|
39
|
if (!isUnderAdminRoutesDir(filename)) { |
|
40
|
return {} |
|
41
|
} |
|
42
|
|
|
43
|
const basename = path.basename(filename) |
|
44
|
if (PAGE_BASENAMES.has(basename)) { |
|
45
|
return {} |
|
46
|
} |
|
47
|
|
|
48
|
const defineRouteConfigNames = new Set<string>() |
|
49
|
const callSites: TSESTree.CallExpression[] = [] |
|
50
|
|
|
51
|
return { |
|
52
|
ImportDeclaration(node) { |
|
53
|
if (node.source.value !== ADMIN_SDK_SOURCE) { |
|
54
|
return |
|
55
|
} |
|
56
|
for (const spec of node.specifiers) { |
|
57
|
if ( |
|
58
|
spec.type === AST_NODE_TYPES.ImportSpecifier && |
|
59
|
spec.imported.type === AST_NODE_TYPES.Identifier && |
|
60
|
spec.imported.name === DEFINE_ROUTE_CONFIG |
|
61
|
) { |
|
62
|
defineRouteConfigNames.add(spec.local.name) |
|
63
|
} |
|
64
|
} |
|
65
|
}, |
|
66
|
CallExpression(node) { |
|
67
|
if (node.callee.type === AST_NODE_TYPES.Identifier) { |
|
68
|
callSites.push(node) |
|
69
|
} |
|
70
|
}, |
|
71
|
"Program:exit"(node) { |
|
72
|
for (const call of callSites) { |
|
73
|
if ( |
|
74
|
call.callee.type === AST_NODE_TYPES.Identifier && |
|
75
|
defineRouteConfigNames.has(call.callee.name) |
|
76
|
) { |
|
77
|
context.report({ node, messageId: "wrongFileName" }) |
|
78
|
return |
|
79
|
} |
|
80
|
} |
|
81
|
}, |
|
82
|
} |
|
83
|
}, |
|
84
|
}) |
|
85
|
|
|
86
|
export default rule |