forked from ArnaudBarre/eslint-plugin-react-refresh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
only-export-components.ts
206 lines (196 loc) · 7.03 KB
/
only-export-components.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { TSESLint } from "@typescript-eslint/utils";
import { TSESTree } from "@typescript-eslint/types";
const possibleReactExportRE = /^[A-Z][a-zA-Z0-9]*$/;
// Starts with uppercase and at least one lowercase
// This can lead to some false positive (ex: `const CMS = () => <></>; export default CMS`)
// But allow to catch `export const CONSTANT = 3`
// and the false positive can be avoided with direct name export
const strictReactExportRE = /^[A-Z][a-zA-Z0-9]*[a-z]+[a-zA-Z0-9]*$/;
export const onlyExportComponents: TSESLint.RuleModule<
| "exportAll"
| "namedExport"
| "anonymousExport"
| "noExport"
| "localComponents",
[] | [{ allowConstantExport?: boolean; checkJS?: boolean }]
> = {
meta: {
messages: {
exportAll:
"This rule can't verify that `export *` only exports components.",
namedExport:
"Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.",
anonymousExport:
"Fast refresh can't handle anonymous components. Add a name to your export.",
localComponents:
"Fast refresh only works when a file only exports components. Move your component(s) to a separate file.",
noExport:
"Fast refresh only works when a file has exports. Move your component(s) to a separate file.",
},
type: "problem",
schema: [
{
type: "object",
properties: {
allowConstantExport: { type: "boolean" },
checkJS: { type: "boolean" },
},
additionalProperties: false,
},
],
},
defaultOptions: [],
create: (context) => {
const { allowConstantExport = false, checkJS = false } =
context.options[0] || {};
const filename = context.getFilename();
// Skip tests & stories files
if (
filename.includes(".test.") ||
filename.includes(".spec.") ||
filename.includes(".stories.")
) {
return {};
}
const shouldScan =
filename.endsWith(".jsx") ||
filename.endsWith(".tsx") ||
(checkJS && filename.endsWith(".js"));
if (!shouldScan) return {};
return {
Program(program) {
let hasExports = false;
let mayHaveReactExport = false;
let reactIsInScope = false;
const localComponents: TSESTree.Identifier[] = [];
const nonComponentExports: TSESTree.BindingName[] = [];
const handleLocalIdentifier = (
identifierNode: TSESTree.BindingName,
) => {
if (identifierNode.type !== "Identifier") return;
if (possibleReactExportRE.test(identifierNode.name)) {
localComponents.push(identifierNode);
}
};
const handleExportIdentifier = (
identifierNode: TSESTree.BindingName,
isFunction?: boolean,
init?: TSESTree.Expression | null,
) => {
if (identifierNode.type !== "Identifier") {
nonComponentExports.push(identifierNode);
return;
}
if (
!mayHaveReactExport &&
possibleReactExportRE.test(identifierNode.name)
) {
mayHaveReactExport = true;
}
if (
allowConstantExport &&
init &&
(init.type === "Literal" ||
init.type === "TemplateLiteral" ||
init.type === "BinaryExpression")
) {
return;
}
if (
!(isFunction ? possibleReactExportRE : strictReactExportRE).test(
identifierNode.name,
)
) {
nonComponentExports.push(identifierNode);
}
};
const handleExportDeclaration = (node: TSESTree.ExportDeclaration) => {
if (node.type === "VariableDeclaration") {
for (const variable of node.declarations) {
handleExportIdentifier(
variable.id,
canBeReactFunctionComponent(variable.init),
variable.init,
);
}
} else if (node.type === "FunctionDeclaration") {
if (node.id === null) {
context.report({ messageId: "anonymousExport", node });
} else {
handleExportIdentifier(node.id, true);
}
} else if (node.type === "CallExpression") {
context.report({ messageId: "anonymousExport", node });
}
};
for (const node of program.body) {
if (node.type === "ExportAllDeclaration") {
if (node.exportKind === "type") continue;
hasExports = true;
context.report({ messageId: "exportAll", node });
} else if (node.type === "ExportDefaultDeclaration") {
hasExports = true;
if (
node.declaration.type === "VariableDeclaration" ||
node.declaration.type === "FunctionDeclaration" ||
node.declaration.type === "CallExpression"
) {
handleExportDeclaration(node.declaration);
}
if (node.declaration.type === "Identifier") {
handleExportIdentifier(node.declaration);
}
if (
node.declaration.type === "ArrowFunctionExpression" &&
!node.declaration.id
) {
context.report({ messageId: "anonymousExport", node });
}
} else if (node.type === "ExportNamedDeclaration") {
hasExports = true;
if (node.declaration) handleExportDeclaration(node.declaration);
for (const specifier of node.specifiers) {
handleExportIdentifier(specifier.exported);
}
} else if (node.type === "VariableDeclaration") {
for (const variable of node.declarations) {
handleLocalIdentifier(variable.id);
}
} else if (node.type === "FunctionDeclaration") {
handleLocalIdentifier(node.id);
} else if (node.type === "ImportDeclaration") {
if (node.source.value === "react") {
reactIsInScope = true;
}
}
}
if (checkJS && !reactIsInScope) return;
if (hasExports) {
if (mayHaveReactExport) {
for (const node of nonComponentExports) {
context.report({ messageId: "namedExport", node });
}
} else if (localComponents.length) {
for (const node of localComponents) {
context.report({ messageId: "localComponents", node });
}
}
} else if (localComponents.length) {
for (const node of localComponents) {
context.report({ messageId: "noExport", node });
}
}
},
};
},
};
const canBeReactFunctionComponent = (init: TSESTree.Expression | null) => {
if (!init) return false;
if (init.type === "ArrowFunctionExpression") return true;
if (init.type === "CallExpression") {
if (init.callee.type === "Identifier") {
return ["memo", "forwardRef"].includes(init.callee.name);
}
}
return false;
};