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 | default: () => src_default,
|
---|
24 | rules: () => rules
|
---|
25 | });
|
---|
26 | module.exports = __toCommonJS(src_exports);
|
---|
27 |
|
---|
28 | // src/only-export-components.ts
|
---|
29 | var possibleReactExportRE = /^[A-Z][a-zA-Z0-9]*$/u;
|
---|
30 | var strictReactExportRE = /^[A-Z][a-zA-Z0-9]*[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 | allowConstantExport: { type: "boolean" },
|
---|
47 | checkJS: { type: "boolean" },
|
---|
48 | allowExportNames: { type: "array", items: { type: "string" } }
|
---|
49 | },
|
---|
50 | additionalProperties: false
|
---|
51 | }
|
---|
52 | ]
|
---|
53 | },
|
---|
54 | defaultOptions: [],
|
---|
55 | create: (context) => {
|
---|
56 | const {
|
---|
57 | allowConstantExport = false,
|
---|
58 | checkJS = false,
|
---|
59 | allowExportNames
|
---|
60 | } = context.options[0] ?? {};
|
---|
61 | const filename = context.filename;
|
---|
62 | if (filename.includes(".test.") || filename.includes(".spec.") || filename.includes(".cy.") || filename.includes(".stories.")) {
|
---|
63 | return {};
|
---|
64 | }
|
---|
65 | const shouldScan = filename.endsWith(".jsx") || filename.endsWith(".tsx") || checkJS && filename.endsWith(".js");
|
---|
66 | if (!shouldScan)
|
---|
67 | return {};
|
---|
68 | const allowExportNamesSet = allowExportNames ? new Set(allowExportNames) : void 0;
|
---|
69 | return {
|
---|
70 | Program(program) {
|
---|
71 | let hasExports = false;
|
---|
72 | let mayHaveReactExport = false;
|
---|
73 | let reactIsInScope = false;
|
---|
74 | const localComponents = [];
|
---|
75 | const nonComponentExports = [];
|
---|
76 | const reactContextExports = [];
|
---|
77 | const handleLocalIdentifier = (identifierNode) => {
|
---|
78 | if (identifierNode.type !== "Identifier")
|
---|
79 | return;
|
---|
80 | if (possibleReactExportRE.test(identifierNode.name)) {
|
---|
81 | localComponents.push(identifierNode);
|
---|
82 | }
|
---|
83 | };
|
---|
84 | const handleExportIdentifier = (identifierNode, isFunction, init) => {
|
---|
85 | if (identifierNode.type !== "Identifier") {
|
---|
86 | nonComponentExports.push(identifierNode);
|
---|
87 | return;
|
---|
88 | }
|
---|
89 | if (allowExportNamesSet == null ? void 0 : allowExportNamesSet.has(identifierNode.name))
|
---|
90 | return;
|
---|
91 | if (allowConstantExport && init && (init.type === "Literal" || // 1, "foo"
|
---|
92 | init.type === "UnaryExpression" || // -1
|
---|
93 | init.type === "TemplateLiteral" || // `Some ${template}`
|
---|
94 | init.type === "BinaryExpression")) {
|
---|
95 | return;
|
---|
96 | }
|
---|
97 | if (isFunction) {
|
---|
98 | if (possibleReactExportRE.test(identifierNode.name)) {
|
---|
99 | mayHaveReactExport = true;
|
---|
100 | } else {
|
---|
101 | nonComponentExports.push(identifierNode);
|
---|
102 | }
|
---|
103 | } else {
|
---|
104 | if (init && init.type === "CallExpression" && // createContext || React.createContext
|
---|
105 | (init.callee.type === "Identifier" && init.callee.name === "createContext" || init.callee.type === "MemberExpression" && init.callee.property.type === "Identifier" && init.callee.property.name === "createContext")) {
|
---|
106 | reactContextExports.push(identifierNode);
|
---|
107 | return;
|
---|
108 | }
|
---|
109 | if (init && // Switch to allowList?
|
---|
110 | notReactComponentExpression.has(init.type)) {
|
---|
111 | nonComponentExports.push(identifierNode);
|
---|
112 | return;
|
---|
113 | }
|
---|
114 | if (!mayHaveReactExport && possibleReactExportRE.test(identifierNode.name)) {
|
---|
115 | mayHaveReactExport = true;
|
---|
116 | }
|
---|
117 | if (!strictReactExportRE.test(identifierNode.name)) {
|
---|
118 | nonComponentExports.push(identifierNode);
|
---|
119 | }
|
---|
120 | }
|
---|
121 | };
|
---|
122 | const handleExportDeclaration = (node) => {
|
---|
123 | var _a, _b;
|
---|
124 | if (node.type === "VariableDeclaration") {
|
---|
125 | for (const variable of node.declarations) {
|
---|
126 | handleExportIdentifier(
|
---|
127 | variable.id,
|
---|
128 | canBeReactFunctionComponent(variable.init),
|
---|
129 | variable.init
|
---|
130 | );
|
---|
131 | }
|
---|
132 | } else if (node.type === "FunctionDeclaration") {
|
---|
133 | if (node.id === null) {
|
---|
134 | context.report({ messageId: "anonymousExport", node });
|
---|
135 | } else {
|
---|
136 | handleExportIdentifier(node.id, true);
|
---|
137 | }
|
---|
138 | } else if (node.type === "CallExpression") {
|
---|
139 | if (node.callee.type === "CallExpression" && node.callee.callee.type === "Identifier" && node.callee.callee.name === "connect") {
|
---|
140 | mayHaveReactExport = true;
|
---|
141 | } else if (node.callee.type !== "Identifier") {
|
---|
142 | if (node.callee.type === "MemberExpression" && node.callee.property.type === "Identifier" && reactHOCs.has(node.callee.property.name)) {
|
---|
143 | mayHaveReactExport = true;
|
---|
144 | } else {
|
---|
145 | context.report({ messageId: "anonymousExport", node });
|
---|
146 | }
|
---|
147 | } else if (!reactHOCs.has(node.callee.name)) {
|
---|
148 | context.report({ messageId: "anonymousExport", node });
|
---|
149 | } else if (((_a = node.arguments[0]) == null ? void 0 : _a.type) === "FunctionExpression" && node.arguments[0].id) {
|
---|
150 | handleExportIdentifier(node.arguments[0].id, true);
|
---|
151 | } else if (((_b = node.arguments[0]) == null ? void 0 : _b.type) === "Identifier") {
|
---|
152 | mayHaveReactExport = true;
|
---|
153 | } else {
|
---|
154 | context.report({ messageId: "anonymousExport", node });
|
---|
155 | }
|
---|
156 | } else if (node.type === "TSEnumDeclaration") {
|
---|
157 | nonComponentExports.push(node.id);
|
---|
158 | }
|
---|
159 | };
|
---|
160 | for (const node of program.body) {
|
---|
161 | if (node.type === "ExportAllDeclaration") {
|
---|
162 | if (node.exportKind === "type")
|
---|
163 | continue;
|
---|
164 | hasExports = true;
|
---|
165 | context.report({ messageId: "exportAll", node });
|
---|
166 | } else if (node.type === "ExportDefaultDeclaration") {
|
---|
167 | hasExports = true;
|
---|
168 | const declaration = node.declaration.type === "TSAsExpression" || node.declaration.type === "TSSatisfiesExpression" ? node.declaration.expression : node.declaration;
|
---|
169 | if (declaration.type === "VariableDeclaration" || declaration.type === "FunctionDeclaration" || declaration.type === "CallExpression") {
|
---|
170 | handleExportDeclaration(declaration);
|
---|
171 | }
|
---|
172 | if (declaration.type === "Identifier") {
|
---|
173 | handleExportIdentifier(declaration);
|
---|
174 | }
|
---|
175 | if (declaration.type === "ArrowFunctionExpression") {
|
---|
176 | context.report({ messageId: "anonymousExport", node });
|
---|
177 | }
|
---|
178 | } else if (node.type === "ExportNamedDeclaration") {
|
---|
179 | if (node.exportKind === "type")
|
---|
180 | continue;
|
---|
181 | hasExports = true;
|
---|
182 | if (node.declaration)
|
---|
183 | handleExportDeclaration(node.declaration);
|
---|
184 | for (const specifier of node.specifiers) {
|
---|
185 | handleExportIdentifier(
|
---|
186 | specifier.exported.type === "Identifier" && specifier.exported.name === "default" ? specifier.local : specifier.exported
|
---|
187 | );
|
---|
188 | }
|
---|
189 | } else if (node.type === "VariableDeclaration") {
|
---|
190 | for (const variable of node.declarations) {
|
---|
191 | handleLocalIdentifier(variable.id);
|
---|
192 | }
|
---|
193 | } else if (node.type === "FunctionDeclaration") {
|
---|
194 | handleLocalIdentifier(node.id);
|
---|
195 | } else if (node.type === "ImportDeclaration" && node.source.value === "react") {
|
---|
196 | reactIsInScope = true;
|
---|
197 | }
|
---|
198 | }
|
---|
199 | if (checkJS && !reactIsInScope)
|
---|
200 | return;
|
---|
201 | if (hasExports) {
|
---|
202 | if (mayHaveReactExport) {
|
---|
203 | for (const node of nonComponentExports) {
|
---|
204 | context.report({ messageId: "namedExport", node });
|
---|
205 | }
|
---|
206 | for (const node of reactContextExports) {
|
---|
207 | context.report({ messageId: "reactContext", node });
|
---|
208 | }
|
---|
209 | } else if (localComponents.length) {
|
---|
210 | for (const node of localComponents) {
|
---|
211 | context.report({ messageId: "localComponents", node });
|
---|
212 | }
|
---|
213 | }
|
---|
214 | } else if (localComponents.length) {
|
---|
215 | for (const node of localComponents) {
|
---|
216 | context.report({ messageId: "noExport", node });
|
---|
217 | }
|
---|
218 | }
|
---|
219 | }
|
---|
220 | };
|
---|
221 | }
|
---|
222 | };
|
---|
223 | var reactHOCs = /* @__PURE__ */ new Set(["memo", "forwardRef"]);
|
---|
224 | var canBeReactFunctionComponent = (init) => {
|
---|
225 | if (!init)
|
---|
226 | return false;
|
---|
227 | if (init.type === "ArrowFunctionExpression")
|
---|
228 | return true;
|
---|
229 | if (init.type === "CallExpression" && init.callee.type === "Identifier") {
|
---|
230 | return reactHOCs.has(init.callee.name);
|
---|
231 | }
|
---|
232 | return false;
|
---|
233 | };
|
---|
234 | var notReactComponentExpression = /* @__PURE__ */ new Set([
|
---|
235 | "ArrayExpression",
|
---|
236 | "AwaitExpression",
|
---|
237 | "BinaryExpression",
|
---|
238 | "ChainExpression",
|
---|
239 | "ConditionalExpression",
|
---|
240 | "Literal",
|
---|
241 | "LogicalExpression",
|
---|
242 | "ObjectExpression",
|
---|
243 | "TemplateLiteral",
|
---|
244 | "ThisExpression",
|
---|
245 | "UnaryExpression",
|
---|
246 | "UpdateExpression"
|
---|
247 | ]);
|
---|
248 |
|
---|
249 | // src/index.ts
|
---|
250 | var rules = {
|
---|
251 | "only-export-components": onlyExportComponents
|
---|
252 | };
|
---|
253 | var src_default = { rules };
|
---|
254 | // Annotate the CommonJS export names for ESM import in node:
|
---|
255 | 0 && (module.exports = {
|
---|
256 | rules
|
---|
257 | });
|
---|