1 import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils"
2 import { createRule } from "../../create-rule"
3 import { isAdminComponentFile } from "../../util/admin-scope"
4
5 type MessageIds = "mustBeArrowFunction"
6
7 function functionDeclarationToArrow(
8 context: Parameters<Parameters<typeof createRule>[0]["create"]>[0],
9 fn: TSESTree.FunctionDeclaration
10 ): string | null {
11 if (!fn.id) {
12 return null
13 }
14 const source = context.sourceCode ?? context.getSourceCode()
15 const name = fn.id.name
16 const params = fn.params
17 .map((p) => source.getText(p as unknown as TSESTree.Node))
18 .join(", ")
19 const body = source.getText(fn.body as unknown as TSESTree.Node)
20 const asyncPrefix = fn.async ? "async " : ""
21 return `const ${name} = ${asyncPrefix}(${params}) => ${body}`
22 }
23
24 function anonymousFunctionToArrow(
25 context: Parameters<Parameters<typeof createRule>[0]["create"]>[0],
26 fn: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression
27 ): string | null {
28 const source = context.sourceCode ?? context.getSourceCode()
29 const params = fn.params
30 .map((p) => source.getText(p as unknown as TSESTree.Node))
31 .join(", ")
32 const body = source.getText(fn.body as unknown as TSESTree.Node)
33 const asyncPrefix = fn.async ? "async " : ""
34 return `${asyncPrefix}(${params}) => ${body}`
35 }
36
37 export const rule = createRule<[], MessageIds>({
38 name: "admin-component-must-be-arrow-function",
39 meta: {
40 type: "problem",
41 docs: {
42 description:
43 "Admin widget and UI route components must be arrow functions, not function declarations.",
44 },
45 messages: {
46 mustBeArrowFunction:
47 "Admin widget and UI route components must be arrow functions (`const Foo = () => ...`), not function declarations.",
48 },
49 fixable: "code",
50 schema: [],
51 },
52 defaultOptions: [],
53 create(context) {
54 if (!isAdminComponentFile(context.filename)) {
55 return {}
56 }
57
58 return {
59 ExportDefaultDeclaration(node) {
60 const decl = node.declaration
61
62 // export default function Foo() {} | export default function () {}
63 if (decl.type === AST_NODE_TYPES.FunctionDeclaration) {
64 context.report({
65 node: decl,
66 messageId: "mustBeArrowFunction",
67 fix(fixer) {
68 if (decl.id) {
69 const arrow = functionDeclarationToArrow(context, decl)
70 if (!arrow) {
71 return null
72 }
73 return fixer.replaceText(
74 node,
75 `${arrow}\nexport default ${decl.id.name}`
76 )
77 }
78 const arrow = anonymousFunctionToArrow(context, decl)
79 if (!arrow) {
80 return null
81 }
82 return fixer.replaceText(decl, arrow)
83 },
84 })
85 return
86 }
87
88 // export default function () {} (parsed as FunctionExpression in some shapes)
89 if (decl.type === AST_NODE_TYPES.FunctionExpression) {
90 context.report({
91 node: decl,
92 messageId: "mustBeArrowFunction",
93 fix(fixer) {
94 const arrow = anonymousFunctionToArrow(context, decl)
95 if (!arrow) {
96 return null
97 }
98 return fixer.replaceText(decl, arrow)
99 },
100 })
101 return
102 }
103
104 // export default Foo — resolve binding to function declaration
105 if (decl.type === AST_NODE_TYPES.Identifier) {
106 const scope = context.sourceCode.getScope(node)
107 const variable = scope.variables.find((v) => v.name === decl.name)
108 if (!variable) {
109 return
110 }
111 for (const def of variable.defs) {
112 if (def.node.type === AST_NODE_TYPES.FunctionDeclaration) {
113 const fnDecl = def.node as TSESTree.FunctionDeclaration
114 context.report({
115 node: fnDecl,
116 messageId: "mustBeArrowFunction",
117 fix(fixer) {
118 const arrow = functionDeclarationToArrow(context, fnDecl)
119 if (!arrow) {
120 return null
121 }
122 return fixer.replaceText(fnDecl, arrow)
123 },
124 })
125 }
126 }
127 }
128 },
129 }
130 },
131 })
132
133 export default rule