1 | "use strict";
|
---|
2 | var __defProp = Object.defineProperty;
|
---|
3 | var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
---|
4 | var __getOwnPropNames = Object.getOwnPropertyNames;
|
---|
5 | var __hasOwnProp = Object.prototype.hasOwnProperty;
|
---|
6 | var __export = (target, all) => {
|
---|
7 | for (var name in all)
|
---|
8 | __defProp(target, name, { get: all[name], enumerable: true });
|
---|
9 | };
|
---|
10 | var __copyProps = (to, from, except, desc) => {
|
---|
11 | if (from && typeof from === "object" || typeof from === "function") {
|
---|
12 | for (let key of __getOwnPropNames(from))
|
---|
13 | if (!__hasOwnProp.call(to, key) && key !== except)
|
---|
14 | __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
---|
15 | }
|
---|
16 | return to;
|
---|
17 | };
|
---|
18 | var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
---|
19 |
|
---|
20 | // src/index.ts
|
---|
21 | var src_exports = {};
|
---|
22 | __export(src_exports, {
|
---|
23 | configs: () => configs,
|
---|
24 | default: () => src_default,
|
---|
25 | rules: () => rules
|
---|
26 | });
|
---|
27 | module.exports = __toCommonJS(src_exports);
|
---|
28 |
|
---|
29 | // src/only-export-components.ts
|
---|
30 | var reactComponentNameRE = /^[A-Z][a-zA-Z0-9]*$/u;
|
---|
31 | var onlyExportComponents = {
|
---|
32 | meta: {
|
---|
33 | messages: {
|
---|
34 | exportAll: "This rule can't verify that `export *` only exports components.",
|
---|
35 | namedExport: "Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.",
|
---|
36 | anonymousExport: "Fast refresh can't handle anonymous components. Add a name to your export.",
|
---|
37 | localComponents: "Fast refresh only works when a file only exports components. Move your component(s) to a separate file.",
|
---|
38 | noExport: "Fast refresh only works when a file has exports. Move your component(s) to a separate file.",
|
---|
39 | reactContext: "Fast refresh only works when a file only exports components. Move your React context(s) to a separate file."
|
---|
40 | },
|
---|
41 | type: "problem",
|
---|
42 | schema: [
|
---|
43 | {
|
---|
44 | type: "object",
|
---|
45 | properties: {
|
---|
46 | allowExportNames: { type: "array", items: { type: "string" } },
|
---|
47 | allowConstantExport: { type: "boolean" },
|
---|
48 | customHOCs: { type: "array", items: { type: "string" } },
|
---|
49 | checkJS: { type: "boolean" }
|
---|
50 | },
|
---|
51 | additionalProperties: false
|
---|
52 | }
|
---|
53 | ]
|
---|
54 | },
|
---|
55 | defaultOptions: [],
|
---|
56 | create: (context) => {
|
---|
57 | const {
|
---|
58 | allowExportNames,
|
---|
59 | allowConstantExport = false,
|
---|
60 | customHOCs = [],
|
---|
61 | checkJS = false
|
---|
62 | } = context.options[0] ?? {};
|
---|
63 | const filename = context.filename;
|
---|
64 | if (filename.includes(".test.") || filename.includes(".spec.") || filename.includes(".cy.") || filename.includes(".stories.")) {
|
---|
65 | return {};
|
---|
66 | }
|
---|
67 | const shouldScan = filename.endsWith(".jsx") || filename.endsWith(".tsx") || checkJS && filename.endsWith(".js");
|
---|
68 | if (!shouldScan)
|
---|
69 | return {};
|
---|
70 | const allowExportNamesSet = allowExportNames ? new Set(allowExportNames) : void 0;
|
---|
71 | const reactHOCs = ["memo", "forwardRef", ...customHOCs];
|
---|
72 | const canBeReactFunctionComponent = (init) => {
|
---|
73 | if (!init)
|
---|
74 | return false;
|
---|
75 | if (init.type === "ArrowFunctionExpression")
|
---|
76 | return true;
|
---|
77 | if (init.type === "CallExpression" && init.callee.type === "Identifier") {
|
---|
78 | return reactHOCs.includes(init.callee.name);
|
---|
79 | }
|
---|
80 | return false;
|
---|
81 | };
|
---|
82 | return {
|
---|
83 | Program(program) {
|
---|
84 | let hasExports = false;
|
---|
85 | let hasReactExport = false;
|
---|
86 | let reactIsInScope = false;
|
---|
87 | const localComponents = [];
|
---|
88 | const nonComponentExports = [];
|
---|
89 | const reactContextExports = [];
|
---|
90 | const handleLocalIdentifier = (identifierNode) => {
|
---|
91 | if (identifierNode.type !== "Identifier")
|
---|
92 | return;
|
---|
93 | if (reactComponentNameRE.test(identifierNode.name)) {
|
---|
94 | localComponents.push(identifierNode);
|
---|
95 | }
|
---|
96 | };
|
---|
97 | const handleExportIdentifier = (identifierNode, isFunction, init) => {
|
---|
98 | if (identifierNode.type !== "Identifier") {
|
---|
99 | nonComponentExports.push(identifierNode);
|
---|
100 | return;
|
---|
101 | }
|
---|
102 | if (allowExportNamesSet == null ? void 0 : allowExportNamesSet.has(identifierNode.name))
|
---|
103 | return;
|
---|
104 | if (allowConstantExport && init && (init.type === "Literal" || // 1, "foo"
|
---|
105 | init.type === "UnaryExpression" || // -1
|
---|
106 | init.type === "TemplateLiteral" || // `Some ${template}`
|
---|
107 | init.type === "BinaryExpression")) {
|
---|
108 | return;
|
---|
109 | }
|
---|
110 | if (isFunction) {
|
---|
111 | if (reactComponentNameRE.test(identifierNode.name)) {
|
---|
112 | hasReactExport = true;
|
---|
113 | } else {
|
---|
114 | nonComponentExports.push(identifierNode);
|
---|
115 | }
|
---|
116 | } else {
|
---|
117 | if (init && init.type === "CallExpression" && // createContext || React.createContext
|
---|
118 | (init.callee.type === "Identifier" && init.callee.name === "createContext" || init.callee.type === "MemberExpression" && init.callee.property.type === "Identifier" && init.callee.property.name === "createContext")) {
|
---|
119 | reactContextExports.push(identifierNode);
|
---|
120 | return;
|
---|
121 | }
|
---|
122 | if (init && // Switch to allowList?
|
---|
123 | notReactComponentExpression.has(init.type)) {
|
---|
124 | nonComponentExports.push(identifierNode);
|
---|
125 | return;
|
---|
126 | }
|
---|
127 | if (reactComponentNameRE.test(identifierNode.name)) {
|
---|
128 | hasReactExport = true;
|
---|
129 | } else {
|
---|
130 | nonComponentExports.push(identifierNode);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | };
|
---|
134 | const handleExportDeclaration = (node) => {
|
---|
135 | var _a, _b;
|
---|
136 | if (node.type === "VariableDeclaration") {
|
---|
137 | for (const variable of node.declarations) {
|
---|
138 | handleExportIdentifier(
|
---|
139 | variable.id,
|
---|
140 | canBeReactFunctionComponent(variable.init),
|
---|
141 | variable.init
|
---|
142 | );
|
---|
143 | }
|
---|
144 | } else if (node.type === "FunctionDeclaration") {
|
---|
145 | if (node.id === null) {
|
---|
146 | context.report({ messageId: "anonymousExport", node });
|
---|
147 | } else {
|
---|
148 | handleExportIdentifier(node.id, true);
|
---|
149 | }
|
---|
150 | } else if (node.type === "CallExpression") {
|
---|
151 | if (node.callee.type === "CallExpression" && node.callee.callee.type === "Identifier" && node.callee.callee.name === "connect") {
|
---|
152 | hasReactExport = true;
|
---|
153 | } else if (node.callee.type !== "Identifier") {
|
---|
154 | if (node.callee.type === "MemberExpression" && node.callee.property.type === "Identifier" && reactHOCs.includes(node.callee.property.name)) {
|
---|
155 | hasReactExport = true;
|
---|
156 | } else {
|
---|
157 | context.report({ messageId: "anonymousExport", node });
|
---|
158 | }
|
---|
159 | } else if (!reactHOCs.includes(node.callee.name)) {
|
---|
160 | context.report({ messageId: "anonymousExport", node });
|
---|
161 | } else if (((_a = node.arguments[0]) == null ? void 0 : _a.type) === "FunctionExpression" && node.arguments[0].id) {
|
---|
162 | handleExportIdentifier(node.arguments[0].id, true);
|
---|
163 | } else if (((_b = node.arguments[0]) == null ? void 0 : _b.type) === "Identifier") {
|
---|
164 | hasReactExport = true;
|
---|
165 | } else {
|
---|
166 | context.report({ messageId: "anonymousExport", node });
|
---|
167 | }
|
---|
168 | } else if (node.type === "TSEnumDeclaration") {
|
---|
169 | nonComponentExports.push(node.id);
|
---|
170 | }
|
---|
171 | };
|
---|
172 | for (const node of program.body) {
|
---|
173 | if (node.type === "ExportAllDeclaration") {
|
---|
174 | if (node.exportKind === "type")
|
---|
175 | continue;
|
---|
176 | hasExports = true;
|
---|
177 | context.report({ messageId: "exportAll", node });
|
---|
178 | } else if (node.type === "ExportDefaultDeclaration") {
|
---|
179 | hasExports = true;
|
---|
180 | const declaration = node.declaration.type === "TSAsExpression" || node.declaration.type === "TSSatisfiesExpression" ? node.declaration.expression : node.declaration;
|
---|
181 | if (declaration.type === "VariableDeclaration" || declaration.type === "FunctionDeclaration" || declaration.type === "CallExpression") {
|
---|
182 | handleExportDeclaration(declaration);
|
---|
183 | }
|
---|
184 | if (declaration.type === "Identifier") {
|
---|
185 | handleExportIdentifier(declaration);
|
---|
186 | }
|
---|
187 | if (declaration.type === "ArrowFunctionExpression") {
|
---|
188 | context.report({ messageId: "anonymousExport", node });
|
---|
189 | }
|
---|
190 | } else if (node.type === "ExportNamedDeclaration") {
|
---|
191 | if (node.exportKind === "type")
|
---|
192 | continue;
|
---|
193 | hasExports = true;
|
---|
194 | if (node.declaration)
|
---|
195 | handleExportDeclaration(node.declaration);
|
---|
196 | for (const specifier of node.specifiers) {
|
---|
197 | handleExportIdentifier(
|
---|
198 | specifier.exported.type === "Identifier" && specifier.exported.name === "default" ? specifier.local : specifier.exported
|
---|
199 | );
|
---|
200 | }
|
---|
201 | } else if (node.type === "VariableDeclaration") {
|
---|
202 | for (const variable of node.declarations) {
|
---|
203 | handleLocalIdentifier(variable.id);
|
---|
204 | }
|
---|
205 | } else if (node.type === "FunctionDeclaration") {
|
---|
206 | handleLocalIdentifier(node.id);
|
---|
207 | } else if (node.type === "ImportDeclaration" && node.source.value === "react") {
|
---|
208 | reactIsInScope = true;
|
---|
209 | }
|
---|
210 | }
|
---|
211 | if (checkJS && !reactIsInScope)
|
---|
212 | return;
|
---|
213 | if (hasExports) {
|
---|
214 | if (hasReactExport) {
|
---|
215 | for (const node of nonComponentExports) {
|
---|
216 | context.report({ messageId: "namedExport", node });
|
---|
217 | }
|
---|
218 | for (const node of reactContextExports) {
|
---|
219 | context.report({ messageId: "reactContext", node });
|
---|
220 | }
|
---|
221 | } else if (localComponents.length) {
|
---|
222 | for (const node of localComponents) {
|
---|
223 | context.report({ messageId: "localComponents", node });
|
---|
224 | }
|
---|
225 | }
|
---|
226 | } else if (localComponents.length) {
|
---|
227 | for (const node of localComponents) {
|
---|
228 | context.report({ messageId: "noExport", node });
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|
232 | };
|
---|
233 | }
|
---|
234 | };
|
---|
235 | var notReactComponentExpression = /* @__PURE__ */ new Set([
|
---|
236 | "ArrayExpression",
|
---|
237 | "AwaitExpression",
|
---|
238 | "BinaryExpression",
|
---|
239 | "ChainExpression",
|
---|
240 | "ConditionalExpression",
|
---|
241 | "Literal",
|
---|
242 | "LogicalExpression",
|
---|
243 | "ObjectExpression",
|
---|
244 | "TemplateLiteral",
|
---|
245 | "ThisExpression",
|
---|
246 | "UnaryExpression",
|
---|
247 | "UpdateExpression"
|
---|
248 | ]);
|
---|
249 |
|
---|
250 | // src/index.ts
|
---|
251 | var rules = {
|
---|
252 | "only-export-components": onlyExportComponents
|
---|
253 | };
|
---|
254 | var plugin = { rules };
|
---|
255 | var configs = {
|
---|
256 | recommended: {
|
---|
257 | plugins: { "react-refresh": plugin },
|
---|
258 | rules: { "react-refresh/only-export-components": "error" }
|
---|
259 | },
|
---|
260 | vite: {
|
---|
261 | plugins: { "react-refresh": plugin },
|
---|
262 | rules: {
|
---|
263 | "react-refresh/only-export-components": [
|
---|
264 | "error",
|
---|
265 | { allowConstantExport: true }
|
---|
266 | ]
|
---|
267 | }
|
---|
268 | }
|
---|
269 | };
|
---|
270 | var src_default = { rules, configs };
|
---|
271 | // Annotate the CommonJS export names for ESM import in node:
|
---|
272 | 0 && (module.exports = {
|
---|
273 | configs,
|
---|
274 | rules
|
---|
275 | });
|
---|