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