@soujvnunes/eslint-config 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/dist/index.d.ts +4 -5
- package/dist/index.js +794 -36
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -61,4 +61,11 @@ These are the opinions most likely to surprise an existing codebase. All of them
|
|
|
61
61
|
|
|
62
62
|
**`security/detect-object-injection` is not included.** It predates TypeScript narrowing and reports every `obj[key]`, including keys already narrowed to a literal union and plain array indexes. Use `noUncheckedIndexedAccess` and an own-key (`Object.hasOwn`) guard instead.
|
|
63
63
|
|
|
64
|
+
**Two client-boundary rules on the Next preset, neither autofixable.** Both read `.jsx` and `.tsx` files that open with `'use client'`, and skip `error` and `global-error`, which Next requires to be client components.
|
|
65
|
+
|
|
66
|
+
- `soujvnunes/no-needless-use-client` reports the directive only when the file holds nothing a server module could not: it works from an allow-list rather than a list of known needs. Every module-level statement has to be an import with bindings (or a stylesheet), a type, a function declaration, a constant whose value, pattern defaults and markup included, builds by running nothing (literals, functions, objects and arrays of those, reads off local constants, and `cva`, `tv`, `memo` or `forwardRef`), or an export of a PascalCase component or a type; a side-effect import, an `if`, a call, `client-only`, `next/dynamic` and any value re-export each keep the directive. Every tag has to be one a server component can render: a tag name, an import, one level into a namespace import (`LabelPrimitive.Root`, and named imports from `radix-ui`, which exports namespaces), a component declared at module level, or a tag the parent passed in whose default passes the same test; an alias (`const MotionDiv = motion.div`), a styled tag, a higher-order result, or anything shaped like a context provider (a name ending in `Context`, `Ctx` or `Provider` under any import alias, or one lone `value` attribute on an element with children) does not qualify. Every value handed to a component, as a prop, a spread, a child, or a tag's `ref`, `action` or `formAction`, has to be data: literals (a RegExp is not), JSX, the props the component received, and objects, arrays and constants built only from those. A default, in a parameter, a destructured local or any pattern enclosing them, counts only when its value is data; a callback's argument is not a prop; a reassigned `let`, an object written through a member, a mutating method (`push`, `splice`) or a call it is handed to, and a key a later spread may replace are not data; and a read that names a string, array, number or object method (`LABEL.toUpperCase`, `ROWS.map`) is a function. A tag's other attributes and children can only hold data anyway, and `className` is a string by contract. On top of that, a hook call, `createContext`, `createPortal` or `flushSync` (matched by both the local and the imported name, so an alias hides neither), an `on*` handler, a class component or a browser-only global read as a value (bare or through `globalThis`) keeps the directive. A file without the directive still runs on the client when a client component imports it, so the fix is to delete the line.
|
|
67
|
+
- `soujvnunes/no-static-jsx-in-client` reports a subtree of three or more elements, or a run of static siblings under a dynamic parent, that reads only literals, imports and module-level constants whose value is itself static. It reports only in a file that needs the client, or whose directive is kept by disabling the other rule (file-wide, on the line before, or on the same line), since in any other file that rule's fix, deleting the directive, already moves the markup to the server. The two rules share one analysis per file. The tags have to pass the same test as above and be the same on every render, no handler, `ref` or form action may sit on a tag, and a value handed to a component may not be a bare import, a namespace member or anything holding one, since that is most likely a function; a read through a named import (a copy dictionary) is static. Markup a library callback or an event handler builds is skipped, since no server parent exists to take it. That markup ships to the browser and hydrates for nothing. Render it in the server parent and pass it in as `children` or a named `ReactNode` prop; moving it to a file without the directive does not help while the client file imports it. The threshold is the `minElements` option.
|
|
68
|
+
|
|
69
|
+
Measured on a Next app with 77 client files, with the TypeScript parser and browser globals the preset uses: the first rule found the 3 wrappers around a Radix primitive and `cn()` that were known to be needless, and nothing else once `error` files were exempt; the second found 16 static subtrees in 8 files, every one a block of labels and inputs or a heading a server parent could render. A threshold of two elements found 43, most of them label and input pairs that are not worth a refactor.
|
|
70
|
+
|
|
64
71
|
Adopting this in an existing repo is usually one `eslint --fix` pass plus a short list of genuine fixes from the type-aware rules.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { Rule, Linter } from 'eslint';
|
|
2
2
|
|
|
3
3
|
declare const oneLineComments: Rule.RuleModule;
|
|
4
|
-
|
|
4
|
+
|
|
5
|
+
type CompatiblePlugin = {
|
|
5
6
|
meta: {
|
|
6
7
|
name: string;
|
|
7
8
|
};
|
|
8
|
-
rules: {
|
|
9
|
-
'one-line-comments': Rule.RuleModule;
|
|
10
|
-
};
|
|
11
9
|
};
|
|
10
|
+
declare const soujvnunesPlugin: CompatiblePlugin;
|
|
12
11
|
|
|
13
12
|
interface ConfigOptions {
|
|
14
13
|
/** Extra ignore globs, merged after the defaults. */
|
|
@@ -27,4 +26,4 @@ declare const createBaseConfig: (options?: ConfigOptions) => any[];
|
|
|
27
26
|
/** Full config for Next.js apps: base plus React, React Hooks, jsx-a11y and Next plugins. */
|
|
28
27
|
declare const createNextConfig: (options?: ConfigOptions) => any[];
|
|
29
28
|
|
|
30
|
-
export { type ConfigOptions, createBaseConfig, createNextConfig, oneLineComments,
|
|
29
|
+
export { type ConfigOptions, createBaseConfig, createNextConfig, oneLineComments, soujvnunesPlugin };
|
package/dist/index.js
CHANGED
|
@@ -11,9 +11,618 @@ import react from "eslint-plugin-react";
|
|
|
11
11
|
import reactHooks from "eslint-plugin-react-hooks";
|
|
12
12
|
import security from "eslint-plugin-security";
|
|
13
13
|
import unusedImports from "eslint-plugin-unused-imports";
|
|
14
|
-
import
|
|
14
|
+
import globals2 from "globals";
|
|
15
15
|
import tseslint from "typescript-eslint";
|
|
16
16
|
|
|
17
|
+
// needlessUseClient.ts
|
|
18
|
+
import { ESLintUtils } from "@typescript-eslint/utils";
|
|
19
|
+
|
|
20
|
+
// findUseClientDirective.ts
|
|
21
|
+
import { AST_NODE_TYPES } from "@typescript-eslint/utils";
|
|
22
|
+
var findUseClientDirective = (program) => {
|
|
23
|
+
for (const statement of program.body) {
|
|
24
|
+
if (statement.type !== AST_NODE_TYPES.ExpressionStatement || statement.directive === void 0)
|
|
25
|
+
return null;
|
|
26
|
+
if (statement.directive === "use client") return statement;
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// needsClient.ts
|
|
32
|
+
import { AST_NODE_TYPES as AST_NODE_TYPES12, TSESLint as TSESLint4 } from "@typescript-eslint/utils";
|
|
33
|
+
import globals from "globals";
|
|
34
|
+
|
|
35
|
+
// calleeName.ts
|
|
36
|
+
import { AST_NODE_TYPES as AST_NODE_TYPES2 } from "@typescript-eslint/utils";
|
|
37
|
+
var calleeName = (callee) => {
|
|
38
|
+
if (callee.type === AST_NODE_TYPES2.Identifier) return callee.name;
|
|
39
|
+
if (callee.type === AST_NODE_TYPES2.MemberExpression && callee.property.type === AST_NODE_TYPES2.Identifier)
|
|
40
|
+
return callee.property.name;
|
|
41
|
+
return "";
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// childNodes.ts
|
|
45
|
+
var SKIPPED_KEYS = /* @__PURE__ */ new Set(["typeAnnotation", "typeArguments", "typeParameters", "returnType"]);
|
|
46
|
+
var isNode = (value) => typeof value === "object" && value !== null && "type" in value && typeof value.type === "string";
|
|
47
|
+
var childNodes = (node, visitorKeys) => (visitorKeys[node.type] ?? []).filter((key) => !SKIPPED_KEYS.has(key)).flatMap((key) => {
|
|
48
|
+
const value = Reflect.get(node, key);
|
|
49
|
+
return (Array.isArray(value) ? value : [value]).filter(isNode);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// createClassifier.ts
|
|
53
|
+
import { ASTUtils, AST_NODE_TYPES as AST_NODE_TYPES11, TSESLint as TSESLint3 } from "@typescript-eslint/utils";
|
|
54
|
+
|
|
55
|
+
// everyDef.ts
|
|
56
|
+
var everyDef = (variable, test) => !!variable && variable.defs.length > 0 && variable.defs.every(test);
|
|
57
|
+
|
|
58
|
+
// importedName.ts
|
|
59
|
+
import { AST_NODE_TYPES as AST_NODE_TYPES3, TSESLint } from "@typescript-eslint/utils";
|
|
60
|
+
var { DefinitionType } = TSESLint.Scope;
|
|
61
|
+
var importedName = (variable) => {
|
|
62
|
+
const [def] = variable?.defs ?? [];
|
|
63
|
+
if (def?.type !== DefinitionType.ImportBinding || def.node.type !== AST_NODE_TYPES3.ImportSpecifier)
|
|
64
|
+
return "";
|
|
65
|
+
const { imported } = def.node;
|
|
66
|
+
return imported.type === AST_NODE_TYPES3.Identifier ? imported.name : imported.value;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// isComponentFunction.ts
|
|
70
|
+
import { AST_NODE_TYPES as AST_NODE_TYPES6 } from "@typescript-eslint/utils";
|
|
71
|
+
|
|
72
|
+
// isComponentName.ts
|
|
73
|
+
var isComponentName = (name) => !!name && /^[A-Z]/u.test(name);
|
|
74
|
+
|
|
75
|
+
// isComponentWrapper.ts
|
|
76
|
+
import { AST_NODE_TYPES as AST_NODE_TYPES5 } from "@typescript-eslint/utils";
|
|
77
|
+
|
|
78
|
+
// isComponentWrapperCall.ts
|
|
79
|
+
import { AST_NODE_TYPES as AST_NODE_TYPES4 } from "@typescript-eslint/utils";
|
|
80
|
+
var WRAPPERS = /* @__PURE__ */ new Set(["memo", "forwardRef"]);
|
|
81
|
+
var isComponentWrapperCall = (node) => node.type === AST_NODE_TYPES4.CallExpression && WRAPPERS.has(calleeName(node.callee));
|
|
82
|
+
|
|
83
|
+
// isComponentWrapper.ts
|
|
84
|
+
var isComponentWrapper = (node) => isComponentWrapperCall(node) || node.type === AST_NODE_TYPES5.TSAsExpression || node.type === AST_NODE_TYPES5.TSSatisfiesExpression;
|
|
85
|
+
|
|
86
|
+
// isComponentFunction.ts
|
|
87
|
+
var MODULE_LEVEL = /* @__PURE__ */ new Set([
|
|
88
|
+
AST_NODE_TYPES6.Program,
|
|
89
|
+
AST_NODE_TYPES6.ExportNamedDeclaration,
|
|
90
|
+
AST_NODE_TYPES6.ExportDefaultDeclaration
|
|
91
|
+
]);
|
|
92
|
+
var isComponentFunction = (node) => {
|
|
93
|
+
if (node.type === AST_NODE_TYPES6.FunctionDeclaration)
|
|
94
|
+
return MODULE_LEVEL.has(node.parent.type) && (node.parent.type === AST_NODE_TYPES6.ExportDefaultDeclaration || isComponentName(node.id?.name));
|
|
95
|
+
if (node.type !== AST_NODE_TYPES6.ArrowFunctionExpression && node.type !== AST_NODE_TYPES6.FunctionExpression)
|
|
96
|
+
return false;
|
|
97
|
+
let holder = node.parent;
|
|
98
|
+
while (isComponentWrapper(holder) && holder.parent) holder = holder.parent;
|
|
99
|
+
if (holder.type === AST_NODE_TYPES6.ExportDefaultDeclaration) return true;
|
|
100
|
+
return holder.type === AST_NODE_TYPES6.VariableDeclarator && holder.id.type === AST_NODE_TYPES6.Identifier && isComponentName(holder.id.name) && MODULE_LEVEL.has(holder.parent.parent?.type ?? "");
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// isComponentInit.ts
|
|
104
|
+
import { AST_NODE_TYPES as AST_NODE_TYPES8 } from "@typescript-eslint/utils";
|
|
105
|
+
|
|
106
|
+
// isFunctionNode.ts
|
|
107
|
+
import { AST_NODE_TYPES as AST_NODE_TYPES7 } from "@typescript-eslint/utils";
|
|
108
|
+
var isFunctionNode = (node) => node.type === AST_NODE_TYPES7.ArrowFunctionExpression || node.type === AST_NODE_TYPES7.FunctionExpression || node.type === AST_NODE_TYPES7.FunctionDeclaration;
|
|
109
|
+
|
|
110
|
+
// isComponentInit.ts
|
|
111
|
+
var isComponentInit = (init) => {
|
|
112
|
+
if (!init) return false;
|
|
113
|
+
if (isFunctionNode(init)) return true;
|
|
114
|
+
if (!isComponentWrapper(init)) return false;
|
|
115
|
+
if (init.type !== AST_NODE_TYPES8.CallExpression) return isComponentInit(init.expression);
|
|
116
|
+
const [inner] = init.arguments;
|
|
117
|
+
if (inner?.type === AST_NODE_TYPES8.Identifier) return isComponentName(inner.name);
|
|
118
|
+
return isComponentInit(inner ?? null);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// isNamespaceImport.ts
|
|
122
|
+
import { AST_NODE_TYPES as AST_NODE_TYPES9, TSESLint as TSESLint2 } from "@typescript-eslint/utils";
|
|
123
|
+
var { DefinitionType: DefinitionType2 } = TSESLint2.Scope;
|
|
124
|
+
var NAMESPACE_PACKAGES = /* @__PURE__ */ new Set(["radix-ui"]);
|
|
125
|
+
var isNamespaceImport = (variable) => everyDef(
|
|
126
|
+
variable,
|
|
127
|
+
(def) => def.type === DefinitionType2.ImportBinding && (def.node.type === AST_NODE_TYPES9.ImportNamespaceSpecifier || def.node.type === AST_NODE_TYPES9.ImportSpecifier && def.parent.type === AST_NODE_TYPES9.ImportDeclaration && NAMESPACE_PACKAGES.has(def.parent.source.value))
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
// patternDefaults.ts
|
|
131
|
+
import { AST_NODE_TYPES as AST_NODE_TYPES10 } from "@typescript-eslint/utils";
|
|
132
|
+
var patternDefaults = (name) => {
|
|
133
|
+
const defaults = [];
|
|
134
|
+
let current = name;
|
|
135
|
+
while (current.parent) {
|
|
136
|
+
const { parent } = current;
|
|
137
|
+
if (parent.type === AST_NODE_TYPES10.AssignmentPattern && parent.left === current)
|
|
138
|
+
defaults.push(parent.right);
|
|
139
|
+
else if (parent.type === AST_NODE_TYPES10.Property && parent.value === current) {
|
|
140
|
+
if (parent.parent.type !== AST_NODE_TYPES10.ObjectPattern) break;
|
|
141
|
+
current = parent.parent;
|
|
142
|
+
continue;
|
|
143
|
+
} else if (parent.type !== AST_NODE_TYPES10.ArrayPattern && parent.type !== AST_NODE_TYPES10.RestElement)
|
|
144
|
+
break;
|
|
145
|
+
current = parent;
|
|
146
|
+
}
|
|
147
|
+
return defaults;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
// createClassifier.ts
|
|
151
|
+
var { DefinitionType: DefinitionType3, ScopeType } = TSESLint3.Scope;
|
|
152
|
+
var INTRINSIC = /^[a-z]/u;
|
|
153
|
+
var PROVIDER = /(?:Context|Ctx|Provider)$/u;
|
|
154
|
+
var MUTATING_METHODS = /* @__PURE__ */ new Set([
|
|
155
|
+
"push",
|
|
156
|
+
"pop",
|
|
157
|
+
"shift",
|
|
158
|
+
"unshift",
|
|
159
|
+
"splice",
|
|
160
|
+
"sort",
|
|
161
|
+
"reverse",
|
|
162
|
+
"fill",
|
|
163
|
+
"copyWithin",
|
|
164
|
+
"set",
|
|
165
|
+
"add",
|
|
166
|
+
"delete",
|
|
167
|
+
"clear"
|
|
168
|
+
]);
|
|
169
|
+
var MODULE_SCOPES = /* @__PURE__ */ new Set([ScopeType.module, ScopeType.global]);
|
|
170
|
+
var LITERAL_INITS = /* @__PURE__ */ new Set([
|
|
171
|
+
AST_NODE_TYPES11.ArrayExpression,
|
|
172
|
+
AST_NODE_TYPES11.Literal,
|
|
173
|
+
AST_NODE_TYPES11.TemplateLiteral
|
|
174
|
+
]);
|
|
175
|
+
var METHOD_NAMES = new Set(
|
|
176
|
+
[String.prototype, Array.prototype, Number.prototype, Object.prototype].flatMap(
|
|
177
|
+
(prototype) => Object.getOwnPropertyNames(prototype).filter(
|
|
178
|
+
(name) => typeof Reflect.get(prototype, name) === "function"
|
|
179
|
+
)
|
|
180
|
+
)
|
|
181
|
+
);
|
|
182
|
+
var ALWAYS = { data: true, stable: true };
|
|
183
|
+
var NEVER = { data: false, stable: false };
|
|
184
|
+
var PER_RENDER = { data: true, stable: false };
|
|
185
|
+
var DECLARATION = { data: false, stable: true };
|
|
186
|
+
var both = (left, right) => ({
|
|
187
|
+
data: left.data && right.data,
|
|
188
|
+
stable: left.stable && right.stable
|
|
189
|
+
});
|
|
190
|
+
var all = (verdicts2) => verdicts2.reduce(both, ALWAYS);
|
|
191
|
+
var keyName = (key, computed) => {
|
|
192
|
+
if (!computed && key.type === AST_NODE_TYPES11.Identifier) return key.name;
|
|
193
|
+
if (key.type === AST_NODE_TYPES11.Literal && typeof key.value === "string") return key.value;
|
|
194
|
+
return void 0;
|
|
195
|
+
};
|
|
196
|
+
var isMutated = (variable) => variable.references.some(({ identifier }) => {
|
|
197
|
+
let current = identifier;
|
|
198
|
+
while (current.parent?.type === AST_NODE_TYPES11.MemberExpression && current.parent.object === current)
|
|
199
|
+
current = current.parent;
|
|
200
|
+
const { parent } = current;
|
|
201
|
+
if (current === identifier)
|
|
202
|
+
return parent?.type === AST_NODE_TYPES11.CallExpression && parent.callee !== current;
|
|
203
|
+
const method = current.type === AST_NODE_TYPES11.MemberExpression ? keyName(current.property, current.computed) : void 0;
|
|
204
|
+
return parent?.type === AST_NODE_TYPES11.AssignmentExpression && parent.left === current || parent?.type === AST_NODE_TYPES11.UpdateExpression || parent?.type === AST_NODE_TYPES11.UnaryExpression && parent.operator === "delete" || parent?.type === AST_NODE_TYPES11.CallExpression && parent.callee === current && MUTATING_METHODS.has(method ?? "");
|
|
205
|
+
});
|
|
206
|
+
var createClassifier = (sourceCode, importMembersAreData) => {
|
|
207
|
+
const variableOf = (node, name) => ASTUtils.findVariable(sourceCode.getScope(node), name);
|
|
208
|
+
const memo = /* @__PURE__ */ new Map();
|
|
209
|
+
const resolving = /* @__PURE__ */ new Set();
|
|
210
|
+
let cycled = false;
|
|
211
|
+
const withDefault = (name, verdict) => all([verdict, ...patternDefaults(name).map((fallback) => classify(fallback, "prop"))]);
|
|
212
|
+
const written = /* @__PURE__ */ new Map();
|
|
213
|
+
const isWritten = (variable, kind) => {
|
|
214
|
+
const cached = written.get(variable);
|
|
215
|
+
if (cached !== void 0) return cached;
|
|
216
|
+
const verdict = kind !== "const" && variable.references.some((reference) => reference.isWrite() && !reference.init) || isMutated(variable);
|
|
217
|
+
written.set(variable, verdict);
|
|
218
|
+
return verdict;
|
|
219
|
+
};
|
|
220
|
+
const ofBinding = (variable) => {
|
|
221
|
+
const cached = memo.get(variable);
|
|
222
|
+
if (cached) return cached;
|
|
223
|
+
if (resolving.has(variable)) {
|
|
224
|
+
cycled = true;
|
|
225
|
+
return NEVER;
|
|
226
|
+
}
|
|
227
|
+
const outermost = resolving.size === 0;
|
|
228
|
+
if (outermost) cycled = false;
|
|
229
|
+
resolving.add(variable);
|
|
230
|
+
const verdict = all(
|
|
231
|
+
variable.defs.map((def) => {
|
|
232
|
+
if (def.type === DefinitionType3.Parameter)
|
|
233
|
+
return isComponentFunction(def.node) ? withDefault(def.name, PER_RENDER) : NEVER;
|
|
234
|
+
if (def.type === DefinitionType3.FunctionName || def.type === DefinitionType3.ClassName)
|
|
235
|
+
return DECLARATION;
|
|
236
|
+
if (def.type !== DefinitionType3.Variable || !def.node.init) return NEVER;
|
|
237
|
+
if (isWritten(variable, def.parent.kind)) return NEVER;
|
|
238
|
+
const init = classify(def.node.init, "prop");
|
|
239
|
+
return withDefault(def.name, {
|
|
240
|
+
data: init.data,
|
|
241
|
+
stable: init.stable && MODULE_SCOPES.has(variable.scope.type) && def.parent.kind === "const"
|
|
242
|
+
});
|
|
243
|
+
})
|
|
244
|
+
);
|
|
245
|
+
resolving.delete(variable);
|
|
246
|
+
if (outermost && !cycled) memo.set(variable, verdict);
|
|
247
|
+
return verdict;
|
|
248
|
+
};
|
|
249
|
+
const ofImport = (position, isDictionaryRead) => ({
|
|
250
|
+
data: position === "text" || isDictionaryRead && importMembersAreData,
|
|
251
|
+
stable: true
|
|
252
|
+
});
|
|
253
|
+
const isImport = (variable) => everyDef(variable, (def) => def.type === DefinitionType3.ImportBinding);
|
|
254
|
+
const ofIdentifier = (node, position) => {
|
|
255
|
+
if (node.name === "undefined") return ALWAYS;
|
|
256
|
+
const variable = variableOf(node, node.name);
|
|
257
|
+
if (!variable || variable.defs.length === 0) return NEVER;
|
|
258
|
+
if (isImport(variable)) return ofImport(position, false);
|
|
259
|
+
return ofBinding(variable);
|
|
260
|
+
};
|
|
261
|
+
const properties = /* @__PURE__ */ new Map();
|
|
262
|
+
const ofProperty = (object, name, position) => {
|
|
263
|
+
const memoKey = `${position} ${name ?? ""}`;
|
|
264
|
+
const cached = properties.get(object)?.get(memoKey);
|
|
265
|
+
if (cached) return cached;
|
|
266
|
+
let verdict = null;
|
|
267
|
+
for (const property of [...object.properties].reverse()) {
|
|
268
|
+
const isProperty = property.type === AST_NODE_TYPES11.Property;
|
|
269
|
+
const propertyName = isProperty ? keyName(property.key, property.computed) : void 0;
|
|
270
|
+
const isMatch = name !== void 0 && propertyName === name;
|
|
271
|
+
if (isMatch || name === void 0 || propertyName === void 0) {
|
|
272
|
+
const value = classify(isProperty ? property.value : property.argument, position);
|
|
273
|
+
verdict = verdict ? both(verdict, value) : value;
|
|
274
|
+
if (isMatch) break;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
const result = verdict ?? NEVER;
|
|
278
|
+
if (resolving.size === 0)
|
|
279
|
+
properties.set(object, (properties.get(object) ?? /* @__PURE__ */ new Map()).set(memoKey, result));
|
|
280
|
+
return result;
|
|
281
|
+
};
|
|
282
|
+
const ofMember = (node, position) => {
|
|
283
|
+
const name = keyName(node.property, node.computed);
|
|
284
|
+
const isMethod = name !== void 0 && METHOD_NAMES.has(name);
|
|
285
|
+
const key = node.computed ? classify(node.property, position) : ALWAYS;
|
|
286
|
+
if (node.object.type !== AST_NODE_TYPES11.Identifier)
|
|
287
|
+
return isMethod ? NEVER : both(classify(node.object, position), key);
|
|
288
|
+
const variable = variableOf(node.object, node.object.name);
|
|
289
|
+
if (isImport(variable)) return both(ofImport(position, !isNamespaceImport(variable)), key);
|
|
290
|
+
const [def] = variable?.defs ?? [];
|
|
291
|
+
if (!variable || def?.type !== DefinitionType3.Variable || !def.node.init || def.node.id.type !== AST_NODE_TYPES11.Identifier)
|
|
292
|
+
return isMethod ? NEVER : both(ofIdentifier(node.object, position), key);
|
|
293
|
+
if (isWritten(variable, def.parent.kind)) return NEVER;
|
|
294
|
+
const binding = ofBinding(variable);
|
|
295
|
+
const { init } = def.node;
|
|
296
|
+
if (init.type === AST_NODE_TYPES11.ObjectExpression) {
|
|
297
|
+
const value = ofProperty(init, name, position);
|
|
298
|
+
return { data: value.data && key.data, stable: value.stable && binding.stable && key.stable };
|
|
299
|
+
}
|
|
300
|
+
if (init.type === AST_NODE_TYPES11.ArrayExpression && node.computed) return both(binding, key);
|
|
301
|
+
if (LITERAL_INITS.has(init.type))
|
|
302
|
+
return name === "length" ? { data: true, stable: binding.stable } : NEVER;
|
|
303
|
+
return isMethod ? NEVER : both(binding, key);
|
|
304
|
+
};
|
|
305
|
+
const classify = (node, position) => {
|
|
306
|
+
if (!node) return NEVER;
|
|
307
|
+
switch (node.type) {
|
|
308
|
+
case AST_NODE_TYPES11.Literal:
|
|
309
|
+
return "regex" in node ? NEVER : ALWAYS;
|
|
310
|
+
case AST_NODE_TYPES11.JSXEmptyExpression:
|
|
311
|
+
return ALWAYS;
|
|
312
|
+
case AST_NODE_TYPES11.TemplateLiteral:
|
|
313
|
+
return {
|
|
314
|
+
data: true,
|
|
315
|
+
stable: all(node.expressions.map((part) => classify(part, position))).stable
|
|
316
|
+
};
|
|
317
|
+
case AST_NODE_TYPES11.UnaryExpression:
|
|
318
|
+
return { data: true, stable: classify(node.argument, position).stable };
|
|
319
|
+
case AST_NODE_TYPES11.BinaryExpression:
|
|
320
|
+
return {
|
|
321
|
+
data: true,
|
|
322
|
+
stable: both(classify(node.left, position), classify(node.right, position)).stable
|
|
323
|
+
};
|
|
324
|
+
case AST_NODE_TYPES11.LogicalExpression:
|
|
325
|
+
return both(classify(node.left, position), classify(node.right, position));
|
|
326
|
+
case AST_NODE_TYPES11.ConditionalExpression: {
|
|
327
|
+
const branches = both(classify(node.consequent, position), classify(node.alternate, position));
|
|
328
|
+
return { data: branches.data, stable: branches.stable && classify(node.test, position).stable };
|
|
329
|
+
}
|
|
330
|
+
case AST_NODE_TYPES11.ArrayExpression:
|
|
331
|
+
return all(
|
|
332
|
+
node.elements.map((element) => {
|
|
333
|
+
if (!element) return ALWAYS;
|
|
334
|
+
return classify(
|
|
335
|
+
element.type === AST_NODE_TYPES11.SpreadElement ? element.argument : element,
|
|
336
|
+
position
|
|
337
|
+
);
|
|
338
|
+
})
|
|
339
|
+
);
|
|
340
|
+
case AST_NODE_TYPES11.ObjectExpression:
|
|
341
|
+
return all(
|
|
342
|
+
node.properties.map((property) => {
|
|
343
|
+
if (property.type === AST_NODE_TYPES11.SpreadElement)
|
|
344
|
+
return classify(property.argument, position);
|
|
345
|
+
if (property.kind !== "init" || property.method) return NEVER;
|
|
346
|
+
const value = classify(property.value, position);
|
|
347
|
+
return property.computed ? both(value, classify(property.key, position)) : value;
|
|
348
|
+
})
|
|
349
|
+
);
|
|
350
|
+
case AST_NODE_TYPES11.MemberExpression:
|
|
351
|
+
return ofMember(node, position);
|
|
352
|
+
case AST_NODE_TYPES11.ChainExpression:
|
|
353
|
+
case AST_NODE_TYPES11.TSAsExpression:
|
|
354
|
+
case AST_NODE_TYPES11.TSSatisfiesExpression:
|
|
355
|
+
case AST_NODE_TYPES11.TSNonNullExpression:
|
|
356
|
+
return classify(node.expression, position);
|
|
357
|
+
case AST_NODE_TYPES11.JSXElement:
|
|
358
|
+
case AST_NODE_TYPES11.JSXFragment:
|
|
359
|
+
return PER_RENDER;
|
|
360
|
+
case AST_NODE_TYPES11.Identifier:
|
|
361
|
+
return ofIdentifier(node, position);
|
|
362
|
+
default:
|
|
363
|
+
return NEVER;
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
const ofTagExpression = (node) => {
|
|
367
|
+
if (node.type === AST_NODE_TYPES11.Literal) return typeof node.value === "string" ? ALWAYS : NEVER;
|
|
368
|
+
if (node.type === AST_NODE_TYPES11.Identifier) return ofTagBinding(variableOf(node, node.name));
|
|
369
|
+
if (node.type !== AST_NODE_TYPES11.MemberExpression || node.object.type !== AST_NODE_TYPES11.Identifier)
|
|
370
|
+
return NEVER;
|
|
371
|
+
return isNamespaceImport(variableOf(node.object, node.object.name)) ? ALWAYS : NEVER;
|
|
372
|
+
};
|
|
373
|
+
const resolvingTags = /* @__PURE__ */ new Set();
|
|
374
|
+
const ofTagBinding = (variable) => {
|
|
375
|
+
if (!variable || variable.defs.length === 0 || resolvingTags.has(variable)) return NEVER;
|
|
376
|
+
resolvingTags.add(variable);
|
|
377
|
+
const isModuleLevel = MODULE_SCOPES.has(variable.scope.type);
|
|
378
|
+
const verdict = all(
|
|
379
|
+
variable.defs.map((def) => {
|
|
380
|
+
if (def.type === DefinitionType3.ImportBinding) return ALWAYS;
|
|
381
|
+
if (def.type === DefinitionType3.FunctionName) return isModuleLevel ? ALWAYS : NEVER;
|
|
382
|
+
if (def.type === DefinitionType3.Parameter)
|
|
383
|
+
return isComponentFunction(def.node) ? all([PER_RENDER, ...patternDefaults(def.name).map(ofTagExpression)]) : NEVER;
|
|
384
|
+
if (def.type !== DefinitionType3.Variable) return NEVER;
|
|
385
|
+
return isModuleLevel && def.parent.kind === "const" && isComponentInit(def.node.init) ? ALWAYS : NEVER;
|
|
386
|
+
})
|
|
387
|
+
);
|
|
388
|
+
resolvingTags.delete(variable);
|
|
389
|
+
return verdict;
|
|
390
|
+
};
|
|
391
|
+
const isProvider = (element, name, variable) => {
|
|
392
|
+
const [only, ...rest] = element.attributes;
|
|
393
|
+
const isValueOnly = rest.length === 0 && !element.selfClosing && only?.type === AST_NODE_TYPES11.JSXAttribute && only.name.type === AST_NODE_TYPES11.JSXIdentifier && only.name.name === "value";
|
|
394
|
+
return PROVIDER.test(name) || PROVIDER.test(importedName(variable)) || isValueOnly;
|
|
395
|
+
};
|
|
396
|
+
const classifyTag = (element) => {
|
|
397
|
+
const { name } = element;
|
|
398
|
+
if (name.type === AST_NODE_TYPES11.JSXNamespacedName) return ALWAYS;
|
|
399
|
+
if (name.type === AST_NODE_TYPES11.JSXIdentifier) {
|
|
400
|
+
if (INTRINSIC.test(name.name)) return ALWAYS;
|
|
401
|
+
const variable = variableOf(element, name.name);
|
|
402
|
+
return isProvider(element, name.name, variable) ? NEVER : ofTagBinding(variable);
|
|
403
|
+
}
|
|
404
|
+
return name.object.type === AST_NODE_TYPES11.JSXIdentifier && isNamespaceImport(variableOf(element, name.object.name)) ? ALWAYS : NEVER;
|
|
405
|
+
};
|
|
406
|
+
const positionOf = (element) => element.name.type === AST_NODE_TYPES11.JSXIdentifier && INTRINSIC.test(element.name.name) ? "text" : "prop";
|
|
407
|
+
return { classify, classifyTag, positionOf, variableOf };
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
// isEventHandler.ts
|
|
411
|
+
var isEventHandler = (key) => /^on[A-Z]/u.test(key);
|
|
412
|
+
|
|
413
|
+
// isFunctionAttribute.ts
|
|
414
|
+
var FUNCTION_ATTRIBUTES = /* @__PURE__ */ new Set(["ref", "action", "formAction"]);
|
|
415
|
+
var isFunctionAttribute = (key) => isEventHandler(key) || FUNCTION_ATTRIBUTES.has(key);
|
|
416
|
+
|
|
417
|
+
// needsClient.ts
|
|
418
|
+
var { DefinitionType: DefinitionType4 } = TSESLint4.Scope;
|
|
419
|
+
var HOOK = /^use[A-Z]/u;
|
|
420
|
+
var STYLESHEET = /\.(?:css|scss|sass|less)$/u;
|
|
421
|
+
var CLIENT_CALLS = /* @__PURE__ */ new Set(["use", "createContext", "createPortal", "flushSync"]);
|
|
422
|
+
var CLIENT_MODULES = /* @__PURE__ */ new Set(["next/dynamic", "client-only"]);
|
|
423
|
+
var CLASS_BASES = /* @__PURE__ */ new Set(["Component", "PureComponent"]);
|
|
424
|
+
var PURE_CALLS = /* @__PURE__ */ new Set(["cva", "tv"]);
|
|
425
|
+
var INERT_TYPES = /* @__PURE__ */ new Set([
|
|
426
|
+
AST_NODE_TYPES12.Literal,
|
|
427
|
+
AST_NODE_TYPES12.TemplateLiteral,
|
|
428
|
+
AST_NODE_TYPES12.TemplateElement,
|
|
429
|
+
AST_NODE_TYPES12.Identifier,
|
|
430
|
+
AST_NODE_TYPES12.UnaryExpression,
|
|
431
|
+
AST_NODE_TYPES12.BinaryExpression,
|
|
432
|
+
AST_NODE_TYPES12.LogicalExpression,
|
|
433
|
+
AST_NODE_TYPES12.ConditionalExpression,
|
|
434
|
+
AST_NODE_TYPES12.ArrayExpression,
|
|
435
|
+
AST_NODE_TYPES12.ObjectExpression,
|
|
436
|
+
AST_NODE_TYPES12.Property,
|
|
437
|
+
AST_NODE_TYPES12.SpreadElement,
|
|
438
|
+
AST_NODE_TYPES12.RestElement,
|
|
439
|
+
AST_NODE_TYPES12.ObjectPattern,
|
|
440
|
+
AST_NODE_TYPES12.ArrayPattern,
|
|
441
|
+
AST_NODE_TYPES12.AssignmentPattern,
|
|
442
|
+
AST_NODE_TYPES12.JSXElement,
|
|
443
|
+
AST_NODE_TYPES12.JSXFragment,
|
|
444
|
+
AST_NODE_TYPES12.JSXOpeningElement,
|
|
445
|
+
AST_NODE_TYPES12.JSXClosingElement,
|
|
446
|
+
AST_NODE_TYPES12.JSXOpeningFragment,
|
|
447
|
+
AST_NODE_TYPES12.JSXClosingFragment,
|
|
448
|
+
AST_NODE_TYPES12.JSXAttribute,
|
|
449
|
+
AST_NODE_TYPES12.JSXSpreadAttribute,
|
|
450
|
+
AST_NODE_TYPES12.JSXExpressionContainer,
|
|
451
|
+
AST_NODE_TYPES12.JSXEmptyExpression,
|
|
452
|
+
AST_NODE_TYPES12.JSXText,
|
|
453
|
+
AST_NODE_TYPES12.JSXIdentifier,
|
|
454
|
+
AST_NODE_TYPES12.JSXNamespacedName,
|
|
455
|
+
AST_NODE_TYPES12.TSAsExpression,
|
|
456
|
+
AST_NODE_TYPES12.TSSatisfiesExpression,
|
|
457
|
+
AST_NODE_TYPES12.TSNonNullExpression
|
|
458
|
+
]);
|
|
459
|
+
var TYPE_DECLARATIONS = /* @__PURE__ */ new Set([
|
|
460
|
+
AST_NODE_TYPES12.TSInterfaceDeclaration,
|
|
461
|
+
AST_NODE_TYPES12.TSTypeAliasDeclaration,
|
|
462
|
+
AST_NODE_TYPES12.TSDeclareFunction
|
|
463
|
+
]);
|
|
464
|
+
var BROWSER_GLOBALS = /* @__PURE__ */ new Set([
|
|
465
|
+
...Object.keys(globals.browser).filter(
|
|
466
|
+
(name) => !Object.hasOwn(globals.node, name) && !Object.hasOwn(globals.builtin, name)
|
|
467
|
+
),
|
|
468
|
+
"navigator",
|
|
469
|
+
"localStorage",
|
|
470
|
+
"sessionStorage"
|
|
471
|
+
]);
|
|
472
|
+
var isValueReference = (reference) => reference.isValueReference !== false;
|
|
473
|
+
var verdicts = /* @__PURE__ */ new WeakMap();
|
|
474
|
+
var analyze = (sourceCode) => {
|
|
475
|
+
const { classify, classifyTag, positionOf, variableOf } = createClassifier(sourceCode, false);
|
|
476
|
+
const isData = (node) => classify(node, "prop").data;
|
|
477
|
+
const isComponentBinding = (identifier) => {
|
|
478
|
+
const variable = variableOf(identifier, identifier.name);
|
|
479
|
+
const isNamedLikeComponent = isComponentName(variable?.name);
|
|
480
|
+
return everyDef(
|
|
481
|
+
variable,
|
|
482
|
+
(def) => def.type === DefinitionType4.Type || isNamedLikeComponent && (def.type === DefinitionType4.FunctionName || def.type === DefinitionType4.Variable && isComponentInit(def.node.init))
|
|
483
|
+
);
|
|
484
|
+
};
|
|
485
|
+
const isLocalRead = (node) => {
|
|
486
|
+
let root = node;
|
|
487
|
+
while (root.type === AST_NODE_TYPES12.MemberExpression) {
|
|
488
|
+
if (root.computed && !isInert(root.property)) return false;
|
|
489
|
+
root = root.object;
|
|
490
|
+
}
|
|
491
|
+
const variable = root.type === AST_NODE_TYPES12.Identifier ? variableOf(root, root.name) : null;
|
|
492
|
+
return everyDef(variable, (def) => def.type === DefinitionType4.Variable);
|
|
493
|
+
};
|
|
494
|
+
const isPlainCallee = (callee) => callee.type === AST_NODE_TYPES12.Identifier || callee.type === AST_NODE_TYPES12.MemberExpression && !callee.computed && isPlainCallee(callee.object);
|
|
495
|
+
const isInert = (node) => {
|
|
496
|
+
if (isFunctionNode(node)) return true;
|
|
497
|
+
if (node.type === AST_NODE_TYPES12.UnaryExpression && node.operator === "delete") return false;
|
|
498
|
+
if (node.type === AST_NODE_TYPES12.MemberExpression) return isLocalRead(node);
|
|
499
|
+
if (node.type === AST_NODE_TYPES12.CallExpression)
|
|
500
|
+
return isPlainCallee(node.callee) && (PURE_CALLS.has(calleeName(node.callee)) || isComponentWrapperCall(node)) && node.arguments.every(isInert);
|
|
501
|
+
return INERT_TYPES.has(node.type) && childNodes(node, sourceCode.visitorKeys).every(isInert);
|
|
502
|
+
};
|
|
503
|
+
const exportsOnlyComponents = (declaration) => {
|
|
504
|
+
if (TYPE_DECLARATIONS.has(declaration.type)) return true;
|
|
505
|
+
if (declaration.type === AST_NODE_TYPES12.FunctionDeclaration)
|
|
506
|
+
return isComponentName(declaration.id?.name);
|
|
507
|
+
if (declaration.type !== AST_NODE_TYPES12.VariableDeclaration) return false;
|
|
508
|
+
return declaration.declarations.every(
|
|
509
|
+
(declarator) => declarator.id.type === AST_NODE_TYPES12.Identifier && isComponentName(declarator.id.name) && declarator.init !== null && isComponentInit(declarator.init) && isInert(declarator.init)
|
|
510
|
+
);
|
|
511
|
+
};
|
|
512
|
+
const isServerSafeStatement = (statement) => {
|
|
513
|
+
switch (statement.type) {
|
|
514
|
+
case AST_NODE_TYPES12.ExpressionStatement:
|
|
515
|
+
return statement.directive !== void 0;
|
|
516
|
+
case AST_NODE_TYPES12.ImportDeclaration:
|
|
517
|
+
if (statement.importKind === "type") return true;
|
|
518
|
+
if (statement.specifiers.length === 0) return STYLESHEET.test(statement.source.value);
|
|
519
|
+
return !CLIENT_MODULES.has(statement.source.value);
|
|
520
|
+
case AST_NODE_TYPES12.FunctionDeclaration:
|
|
521
|
+
return true;
|
|
522
|
+
case AST_NODE_TYPES12.TSEnumDeclaration:
|
|
523
|
+
return statement.body.members.every(
|
|
524
|
+
(member) => !member.initializer || isInert(member.initializer)
|
|
525
|
+
);
|
|
526
|
+
case AST_NODE_TYPES12.VariableDeclaration:
|
|
527
|
+
return statement.declarations.every(
|
|
528
|
+
(declarator) => isInert(declarator.id) && (!declarator.init || isInert(declarator.init))
|
|
529
|
+
);
|
|
530
|
+
case AST_NODE_TYPES12.ExportAllDeclaration:
|
|
531
|
+
return statement.exportKind === "type";
|
|
532
|
+
case AST_NODE_TYPES12.ExportNamedDeclaration: {
|
|
533
|
+
if (statement.exportKind === "type") return true;
|
|
534
|
+
const isTypeOnly = statement.specifiers.length > 0 && statement.specifiers.every((specifier) => specifier.exportKind === "type");
|
|
535
|
+
if (statement.source) return isTypeOnly;
|
|
536
|
+
if (statement.declaration) return exportsOnlyComponents(statement.declaration);
|
|
537
|
+
return statement.specifiers.every(
|
|
538
|
+
(specifier) => specifier.exportKind === "type" || specifier.local.type === AST_NODE_TYPES12.Identifier && isComponentBinding(specifier.local)
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
case AST_NODE_TYPES12.ExportDefaultDeclaration: {
|
|
542
|
+
const { declaration } = statement;
|
|
543
|
+
if (declaration.type === AST_NODE_TYPES12.FunctionDeclaration)
|
|
544
|
+
return isComponentName(declaration.id?.name);
|
|
545
|
+
if (declaration.type === AST_NODE_TYPES12.Identifier) return isComponentBinding(declaration);
|
|
546
|
+
return isComponentWrapperCall(declaration) && isComponentInit(declaration) && isInert(declaration);
|
|
547
|
+
}
|
|
548
|
+
default:
|
|
549
|
+
return TYPE_DECLARATIONS.has(statement.type);
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
const callNames = (callee) => {
|
|
553
|
+
if (callee.type !== AST_NODE_TYPES12.Identifier) return [calleeName(callee)];
|
|
554
|
+
return [callee.name, importedName(variableOf(callee, callee.name))];
|
|
555
|
+
};
|
|
556
|
+
const needsNode = (node) => {
|
|
557
|
+
switch (node.type) {
|
|
558
|
+
case AST_NODE_TYPES12.CallExpression:
|
|
559
|
+
return callNames(node.callee).some((name) => HOOK.test(name) || CLIENT_CALLS.has(name));
|
|
560
|
+
case AST_NODE_TYPES12.ClassDeclaration:
|
|
561
|
+
case AST_NODE_TYPES12.ClassExpression:
|
|
562
|
+
return !!node.superClass && CLASS_BASES.has(calleeName(node.superClass));
|
|
563
|
+
case AST_NODE_TYPES12.MemberExpression:
|
|
564
|
+
return node.object.type === AST_NODE_TYPES12.Identifier && node.object.name === "globalThis" && !node.computed && node.property.type === AST_NODE_TYPES12.Identifier && BROWSER_GLOBALS.has(node.property.name) && !variableOf(node.object, "globalThis")?.defs.length;
|
|
565
|
+
case AST_NODE_TYPES12.JSXOpeningElement: {
|
|
566
|
+
if (!classifyTag(node).data) return true;
|
|
567
|
+
const isTag = positionOf(node) === "text";
|
|
568
|
+
return node.attributes.some((attribute) => {
|
|
569
|
+
if (attribute.type === AST_NODE_TYPES12.JSXSpreadAttribute) return !isData(attribute.argument);
|
|
570
|
+
const key = attribute.name.type === AST_NODE_TYPES12.JSXIdentifier ? attribute.name.name : "";
|
|
571
|
+
if (isEventHandler(key)) return true;
|
|
572
|
+
const { value } = attribute;
|
|
573
|
+
if (value?.type !== AST_NODE_TYPES12.JSXExpressionContainer) return false;
|
|
574
|
+
return (isTag ? isFunctionAttribute(key) : key !== "className") && !isData(value.expression);
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
// A child of a component is its `children` prop, which can carry a function; a child of a tag or a fragment renders in place and cannot.
|
|
578
|
+
case AST_NODE_TYPES12.JSXExpressionContainer:
|
|
579
|
+
return node.parent.type === AST_NODE_TYPES12.JSXElement && positionOf(node.parent.openingElement) === "prop" && !isData(node.expression);
|
|
580
|
+
default:
|
|
581
|
+
return false;
|
|
582
|
+
}
|
|
583
|
+
};
|
|
584
|
+
const walk = (node) => needsNode(node) || childNodes(node, sourceCode.visitorKeys).some(walk);
|
|
585
|
+
if (!sourceCode.ast.body.every(isServerSafeStatement) || walk(sourceCode.ast)) return true;
|
|
586
|
+
const scope = sourceCode.getScope(sourceCode.ast);
|
|
587
|
+
return scope.through.some(
|
|
588
|
+
(reference) => isValueReference(reference) && BROWSER_GLOBALS.has(reference.identifier.name)
|
|
589
|
+
) || scope.variables.some(
|
|
590
|
+
(variable) => variable.defs.length === 0 && BROWSER_GLOBALS.has(variable.name) && variable.references.some(isValueReference)
|
|
591
|
+
);
|
|
592
|
+
};
|
|
593
|
+
var needsClient = (sourceCode) => {
|
|
594
|
+
const cached = verdicts.get(sourceCode.ast);
|
|
595
|
+
if (cached !== void 0) return cached;
|
|
596
|
+
const verdict = analyze(sourceCode);
|
|
597
|
+
verdicts.set(sourceCode.ast, verdict);
|
|
598
|
+
return verdict;
|
|
599
|
+
};
|
|
600
|
+
|
|
601
|
+
// needlessUseClient.ts
|
|
602
|
+
var needlessUseClient = ESLintUtils.RuleCreator.withoutDocs({
|
|
603
|
+
meta: {
|
|
604
|
+
type: "problem",
|
|
605
|
+
docs: {
|
|
606
|
+
description: "Disallow a `'use client'` directive in a module that holds only what a server module could: server-safe module-level statements, component exports, renderable tags and data handed to components."
|
|
607
|
+
},
|
|
608
|
+
schema: [],
|
|
609
|
+
messages: {
|
|
610
|
+
needless: "Nothing in this file needs `'use client'`: no hook call, no event handler, no browser global, no class component, no `next/dynamic` or `client-only`, nothing run at module level, no export but components, no tag a server component could not render, and every value it hands to a component is data a server parent could pass. A file without the directive still runs on the client when a client component imports it, so delete the line and let the importer decide. If a dependency without its own directive needs a boundary here, re-export it from this file, or disable this rule on the line with the reason."
|
|
611
|
+
}
|
|
612
|
+
},
|
|
613
|
+
defaultOptions: [],
|
|
614
|
+
create(context) {
|
|
615
|
+
const { sourceCode } = context;
|
|
616
|
+
const directive = findUseClientDirective(sourceCode.ast);
|
|
617
|
+
if (!directive) return {};
|
|
618
|
+
return {
|
|
619
|
+
Program() {
|
|
620
|
+
if (!needsClient(sourceCode)) context.report({ node: directive, messageId: "needless" });
|
|
621
|
+
}
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
});
|
|
625
|
+
|
|
17
626
|
// oneLineComments.ts
|
|
18
627
|
var DIRECTIVE = /^\s*(?:eslint-disable\b|eslint-enable\b|\/\s*<|prettier-ignore|biome-ignore|(?:istanbul|c8|v8)\s+ignore\b|webpack[A-Z])/u;
|
|
19
628
|
var BLOCK_DIRECTIVE = /^\s*(?:eslint\s|eslint-env\b|global\s|globals\s|exported\s)/u;
|
|
@@ -30,16 +639,31 @@ var isBare = (comment) => comment.type === "Line" && comment.value.trim() === ""
|
|
|
30
639
|
var collapse = (text) => text.replace(/\s+/gu, " ").trim();
|
|
31
640
|
var asLine = (body) => body ? `// ${body}` : null;
|
|
32
641
|
var followedOnLine = (comment, after) => !!after?.loc && !!comment.loc && after.loc.start.line === comment.loc.end.line;
|
|
33
|
-
var
|
|
34
|
-
|
|
642
|
+
var blockLines = (comment) => comment.value.replace(/^!/u, "").split(/\r\n|[\r\n\u2028\u2029]/u).map(
|
|
643
|
+
(line) => line.trim().replace(/^\*(?= |$)/u, "").trim()
|
|
35
644
|
);
|
|
645
|
+
var blockBody = (lines) => collapse(lines.filter(Boolean).join(" "));
|
|
646
|
+
var proseParagraphs = (lines, skipTags) => {
|
|
647
|
+
let count = 0;
|
|
648
|
+
let inSegment = false;
|
|
649
|
+
for (const line of lines) {
|
|
650
|
+
if (line === "") {
|
|
651
|
+
inSegment = false;
|
|
652
|
+
continue;
|
|
653
|
+
}
|
|
654
|
+
if (inSegment) continue;
|
|
655
|
+
inSegment = true;
|
|
656
|
+
if (!(skipTags && line.startsWith("@"))) count += 1;
|
|
657
|
+
}
|
|
658
|
+
return count;
|
|
659
|
+
};
|
|
36
660
|
var opener = (comment) => {
|
|
37
661
|
if (isDocShaped(comment)) return "/**";
|
|
38
662
|
return isBanner(comment) ? "/*!" : "/*";
|
|
39
663
|
};
|
|
40
|
-
var collapsedBlock = (comment) => {
|
|
664
|
+
var collapsedBlock = (comment, lines) => {
|
|
41
665
|
const open = opener(comment);
|
|
42
|
-
const body = blockBody(
|
|
666
|
+
const body = blockBody(lines);
|
|
43
667
|
const text = body ? `${open} ${body} */` : `${open} */`;
|
|
44
668
|
return text.indexOf("*/") === text.length - 2 ? text : null;
|
|
45
669
|
};
|
|
@@ -58,7 +682,7 @@ var oneLineComments = {
|
|
|
58
682
|
schema: [],
|
|
59
683
|
messages: {
|
|
60
684
|
adjacent: "Two comment lines are touching. A comment is one line, however long it runs: join them, or write one line per fact beside the code it describes.",
|
|
61
|
-
paragraphs: "A bare `//` between comment lines
|
|
685
|
+
paragraphs: "A paragraph break inside a comment, a bare `//` between comment lines or an empty line inside a block, makes it a set of comments rather than one wrapped line, and joining it would bury every fact but the first. Write one line per fact beside the code it describes, or move the rationale to the README or the project MANIFEST.",
|
|
62
686
|
block: "This block comment spans lines. A comment is one line, however long it runs: collapse it to one `//` line, or to one `/** \u2026 */` line when it is JSDoc, or move the rationale to the README or the project MANIFEST.",
|
|
63
687
|
notDoc: "A block comment is for JSDoc and JSX only. Write this one as `//`, on its own line or at the end of one, or delete it if it is empty.",
|
|
64
688
|
orphanDoc: "`/**` is JSDoc, and JSDoc sits directly above the code it documents. Move it there, join it with the doc under it, or write it as `//`."
|
|
@@ -93,13 +717,17 @@ var oneLineComments = {
|
|
|
93
717
|
const before = sourceCode.getTokenBefore(comment, { includeComments: true });
|
|
94
718
|
return !!before?.loc && before.loc.end.line === comment.loc?.start.line && followedOnLine(comment, after);
|
|
95
719
|
};
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
720
|
+
const blockVerdict = (comment, after) => {
|
|
721
|
+
const lines = blockLines(comment);
|
|
722
|
+
const keepBlock = insideJsx(comment) || isDocShaped(comment) || isBanner(comment);
|
|
723
|
+
const messageId = spansLines(comment) ? "block" : "notDoc";
|
|
724
|
+
if (!insideJsx(comment) && !isBanner(comment) && proseParagraphs(lines, isDocShaped(comment)) > 1) {
|
|
725
|
+
return { messageId: "paragraphs", text: null };
|
|
99
726
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
727
|
+
if (keepBlock) return { messageId, text: collapsedBlock(comment, lines) };
|
|
728
|
+
const body = blockBody(lines);
|
|
729
|
+
const text = followedOnLine(comment, after) || isDirectiveText(body) ? null : asLine(body);
|
|
730
|
+
return { messageId, text };
|
|
103
731
|
};
|
|
104
732
|
return {
|
|
105
733
|
"Program:exit"() {
|
|
@@ -108,10 +736,10 @@ var oneLineComments = {
|
|
|
108
736
|
if (comment.type !== "Block" || isDirective(comment)) continue;
|
|
109
737
|
const after = sourceCode.getTokenAfter(comment, { includeComments: true });
|
|
110
738
|
if (spansLines(comment)) {
|
|
111
|
-
const text2 =
|
|
739
|
+
const { messageId: messageId2, text: text2 } = blockVerdict(comment, after);
|
|
112
740
|
context.report({
|
|
113
741
|
loc: comment.loc,
|
|
114
|
-
messageId:
|
|
742
|
+
messageId: messageId2,
|
|
115
743
|
fix: text2 && !breaksSemicolonInsertion(comment, after) ? (fixer) => fixer.replaceText(comment, text2) : null
|
|
116
744
|
});
|
|
117
745
|
continue;
|
|
@@ -122,10 +750,10 @@ var oneLineComments = {
|
|
|
122
750
|
else context.report({ loc: comment.loc, messageId: "orphanDoc" });
|
|
123
751
|
continue;
|
|
124
752
|
}
|
|
125
|
-
const text =
|
|
753
|
+
const { messageId, text } = blockVerdict(comment, after);
|
|
126
754
|
context.report({
|
|
127
755
|
loc: comment.loc,
|
|
128
|
-
messageId
|
|
756
|
+
messageId,
|
|
129
757
|
fix: text ? (fixer) => fixer.replaceText(comment, text) : null
|
|
130
758
|
});
|
|
131
759
|
}
|
|
@@ -141,18 +769,9 @@ var oneLineComments = {
|
|
|
141
769
|
const last = current[current.length - 1];
|
|
142
770
|
if (!first?.loc || !last?.loc) return;
|
|
143
771
|
const block = current.some((comment) => comment.type === "Block");
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
if (isBare(comment)) inText = false;
|
|
148
|
-
else if (!inText) {
|
|
149
|
-
inText = true;
|
|
150
|
-
segments += 1;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
const paragraphs = segments > 1;
|
|
154
|
-
const body = collapse(current.map((comment) => comment.value.trim()).join(" "));
|
|
155
|
-
const text = asLine(body);
|
|
772
|
+
const texts = current.map((comment) => isBare(comment) ? "" : comment.value.trim() || " ");
|
|
773
|
+
const paragraphs = proseParagraphs(texts, false) > 1;
|
|
774
|
+
const text = asLine(collapse(texts.join(" ")));
|
|
156
775
|
context.report({
|
|
157
776
|
loc: { start: first.loc.start, end: last.loc.end },
|
|
158
777
|
messageId: paragraphs ? "paragraphs" : "adjacent",
|
|
@@ -170,11 +789,140 @@ var oneLineComments = {
|
|
|
170
789
|
};
|
|
171
790
|
}
|
|
172
791
|
};
|
|
173
|
-
|
|
792
|
+
|
|
793
|
+
// staticJsxInClient.ts
|
|
794
|
+
import { AST_NODE_TYPES as AST_NODE_TYPES13, AST_TOKEN_TYPES, ESLintUtils as ESLintUtils2 } from "@typescript-eslint/utils";
|
|
795
|
+
var DISABLE_KEYWORDS = ["eslint-disable-next-line", "eslint-disable-line", "eslint-disable"];
|
|
796
|
+
var isJsx = (node) => node.type === AST_NODE_TYPES13.JSXElement || node.type === AST_NODE_TYPES13.JSXFragment;
|
|
797
|
+
var isRenderedByComponent = (node) => {
|
|
798
|
+
let current = node.parent;
|
|
799
|
+
while (current && !isFunctionNode(current)) current = current.parent;
|
|
800
|
+
return !current || isComponentFunction(current);
|
|
801
|
+
};
|
|
802
|
+
var staticJsxInClient = ESLintUtils2.RuleCreator.withoutDocs({
|
|
803
|
+
meta: {
|
|
804
|
+
type: "suggestion",
|
|
805
|
+
docs: {
|
|
806
|
+
description: "Disallow a subtree of static JSX inside a client module, where it ships to the browser and hydrates for nothing."
|
|
807
|
+
},
|
|
808
|
+
schema: [
|
|
809
|
+
{
|
|
810
|
+
type: "object",
|
|
811
|
+
properties: { minElements: { type: "integer", minimum: 1 } },
|
|
812
|
+
additionalProperties: false
|
|
813
|
+
}
|
|
814
|
+
],
|
|
815
|
+
messages: {
|
|
816
|
+
static: "Static markup in a client file: {{count}} elements that read only literals, imports and module constants still ship to the browser and hydrate. Render them in the server parent and pass them in as `children` or a named ReactNode prop. Moving them to a file without the directive does not help while this file imports it."
|
|
817
|
+
}
|
|
818
|
+
},
|
|
819
|
+
defaultOptions: [{ minElements: 3 }],
|
|
820
|
+
create(context, [{ minElements }]) {
|
|
821
|
+
const { sourceCode } = context;
|
|
822
|
+
const directive = findUseClientDirective(sourceCode.ast);
|
|
823
|
+
if (!directive) return {};
|
|
824
|
+
const isDirectiveKept = () => sourceCode.getAllComments().some((comment) => {
|
|
825
|
+
const text = comment.value.trim();
|
|
826
|
+
const keyword = DISABLE_KEYWORDS.find(
|
|
827
|
+
(candidate) => text === candidate || text.startsWith(`${candidate} `)
|
|
828
|
+
);
|
|
829
|
+
if (!keyword) return false;
|
|
830
|
+
const list = text.slice(keyword.length);
|
|
831
|
+
const rules = list.split("--")[0]?.split(/[\s,]+/u).filter(Boolean) ?? [];
|
|
832
|
+
const covers = rules.length === 0 || rules.some((rule) => rule.endsWith("no-needless-use-client"));
|
|
833
|
+
const line = directive.loc.start.line;
|
|
834
|
+
if (keyword === "eslint-disable-line") return covers && comment.loc.start.line === line;
|
|
835
|
+
if (keyword === "eslint-disable-next-line") return covers && comment.loc.end.line === line - 1;
|
|
836
|
+
return covers && comment.type === AST_TOKEN_TYPES.Block && comment.range[1] <= directive.range[0];
|
|
837
|
+
});
|
|
838
|
+
if (!needsClient(sourceCode) && !isDirectiveKept()) return {};
|
|
839
|
+
const { classify, classifyTag, positionOf } = createClassifier(sourceCode, true);
|
|
840
|
+
const isStatic = (node, position) => {
|
|
841
|
+
const verdict = classify(node, position);
|
|
842
|
+
return verdict.data && verdict.stable;
|
|
843
|
+
};
|
|
844
|
+
const isStaticAttribute = (attribute, position) => {
|
|
845
|
+
if (attribute.type !== AST_NODE_TYPES13.JSXAttribute) return false;
|
|
846
|
+
const key = attribute.name.type === AST_NODE_TYPES13.JSXIdentifier ? attribute.name.name : "";
|
|
847
|
+
if (isFunctionAttribute(key)) return false;
|
|
848
|
+
const { value } = attribute;
|
|
849
|
+
if (!value || value.type === AST_NODE_TYPES13.Literal) return true;
|
|
850
|
+
return value.type === AST_NODE_TYPES13.JSXExpressionContainer && isStatic(value.expression, position);
|
|
851
|
+
};
|
|
852
|
+
const childPosition = (node) => node.type === AST_NODE_TYPES13.JSXElement ? positionOf(node.openingElement) : "text";
|
|
853
|
+
const verdicts2 = /* @__PURE__ */ new Map();
|
|
854
|
+
const judge = (node) => {
|
|
855
|
+
const cached = verdicts2.get(node);
|
|
856
|
+
if (cached) return cached;
|
|
857
|
+
const position = childPosition(node);
|
|
858
|
+
let isStaticNode = true;
|
|
859
|
+
let count = 0;
|
|
860
|
+
if (node.type === AST_NODE_TYPES13.JSXElement) {
|
|
861
|
+
const { openingElement } = node;
|
|
862
|
+
const tag = classifyTag(openingElement);
|
|
863
|
+
isStaticNode = tag.data && tag.stable && openingElement.attributes.every((attribute) => isStaticAttribute(attribute, position));
|
|
864
|
+
count = 1;
|
|
865
|
+
}
|
|
866
|
+
for (const child of node.children) {
|
|
867
|
+
if (isJsx(child)) {
|
|
868
|
+
const verdict2 = judge(child);
|
|
869
|
+
isStaticNode &&= verdict2.isStatic;
|
|
870
|
+
count += verdict2.count;
|
|
871
|
+
} else if (child.type === AST_NODE_TYPES13.JSXExpressionContainer)
|
|
872
|
+
isStaticNode &&= isStatic(child.expression, position);
|
|
873
|
+
else if (child.type !== AST_NODE_TYPES13.JSXText) isStaticNode = false;
|
|
874
|
+
}
|
|
875
|
+
const verdict = { isStatic: isStaticNode, count };
|
|
876
|
+
verdicts2.set(node, verdict);
|
|
877
|
+
return verdict;
|
|
878
|
+
};
|
|
879
|
+
const report = (loc, count) => context.report({ loc, messageId: "static", data: { count: String(count) } });
|
|
880
|
+
const visit = (node) => {
|
|
881
|
+
const { isStatic: isStaticNode, count } = judge(node);
|
|
882
|
+
if (isStaticNode) {
|
|
883
|
+
if (count >= minElements) report(node.loc, count);
|
|
884
|
+
return;
|
|
885
|
+
}
|
|
886
|
+
const position = childPosition(node);
|
|
887
|
+
let run = [];
|
|
888
|
+
const flush = () => {
|
|
889
|
+
const [first] = run;
|
|
890
|
+
const last = run.at(-1);
|
|
891
|
+
const total = run.reduce((sum, member) => sum + judge(member).count, 0);
|
|
892
|
+
if (first && last && run.length > 1 && total >= minElements)
|
|
893
|
+
report({ start: first.loc.start, end: last.loc.end }, total);
|
|
894
|
+
else for (const member of run) visit(member);
|
|
895
|
+
run = [];
|
|
896
|
+
};
|
|
897
|
+
for (const child of node.children) {
|
|
898
|
+
if (isJsx(child) && judge(child).isStatic) run.push(child);
|
|
899
|
+
else if (isJsx(child)) {
|
|
900
|
+
flush();
|
|
901
|
+
visit(child);
|
|
902
|
+
} else if (child.type === AST_NODE_TYPES13.JSXExpressionContainer && !isStatic(child.expression, position))
|
|
903
|
+
flush();
|
|
904
|
+
else if (child.type === AST_NODE_TYPES13.JSXSpreadChild) flush();
|
|
905
|
+
}
|
|
906
|
+
flush();
|
|
907
|
+
};
|
|
908
|
+
const visitRoot = (node) => {
|
|
909
|
+
if (!isJsx(node.parent) && isRenderedByComponent(node)) visit(node);
|
|
910
|
+
};
|
|
911
|
+
return { JSXElement: visitRoot, JSXFragment: visitRoot };
|
|
912
|
+
}
|
|
913
|
+
});
|
|
914
|
+
|
|
915
|
+
// plugin.ts
|
|
916
|
+
var plugin = {
|
|
174
917
|
// No `version`: it feeds ESLint's cache key, and a hardcoded one drifts from package.json and serves stale cached results after a behaviour change.
|
|
175
918
|
meta: { name: "@soujvnunes/eslint-config" },
|
|
176
|
-
rules: {
|
|
919
|
+
rules: {
|
|
920
|
+
"one-line-comments": oneLineComments,
|
|
921
|
+
"no-needless-use-client": needlessUseClient,
|
|
922
|
+
"no-static-jsx-in-client": staticJsxInClient
|
|
923
|
+
}
|
|
177
924
|
};
|
|
925
|
+
var soujvnunesPlugin = plugin;
|
|
178
926
|
|
|
179
927
|
// index.ts
|
|
180
928
|
var DEFAULT_IGNORES = [
|
|
@@ -351,7 +1099,7 @@ var buildConfig = ({
|
|
|
351
1099
|
...restrictedSyntaxRule,
|
|
352
1100
|
...importOrderRule(importGroups)
|
|
353
1101
|
};
|
|
354
|
-
const languageGlobals = { ...
|
|
1102
|
+
const languageGlobals = { ...globals2.node, ...globals2.es2021 };
|
|
355
1103
|
const settings = {};
|
|
356
1104
|
if (next) {
|
|
357
1105
|
Object.assign(plugins, {
|
|
@@ -361,7 +1109,7 @@ var buildConfig = ({
|
|
|
361
1109
|
"jsx-a11y": jsxA11y
|
|
362
1110
|
});
|
|
363
1111
|
Object.assign(rules, reactRules, nextRules);
|
|
364
|
-
Object.assign(languageGlobals,
|
|
1112
|
+
Object.assign(languageGlobals, globals2.browser, {
|
|
365
1113
|
React: "readonly",
|
|
366
1114
|
JSX: "readonly",
|
|
367
1115
|
NodeJS: "readonly"
|
|
@@ -390,12 +1138,22 @@ var buildConfig = ({
|
|
|
390
1138
|
};
|
|
391
1139
|
const oneLineCommentsOverride = {
|
|
392
1140
|
files: ["**/*.{js,jsx,mjs,cjs,ts,tsx,mts,cts}"],
|
|
393
|
-
plugins: { soujvnunes:
|
|
1141
|
+
plugins: { soujvnunes: soujvnunesPlugin },
|
|
394
1142
|
rules: { "soujvnunes/one-line-comments": "error" }
|
|
395
1143
|
};
|
|
1144
|
+
const clientBoundaryOverride = {
|
|
1145
|
+
files: ["**/*.{jsx,tsx}"],
|
|
1146
|
+
// Next requires these two conventions to be client components and hands them functions, so the directive is never needless and no server parent exists to take their markup.
|
|
1147
|
+
ignores: ["**/{error,global-error}.{jsx,tsx}"],
|
|
1148
|
+
plugins: { soujvnunes: soujvnunesPlugin },
|
|
1149
|
+
rules: {
|
|
1150
|
+
"soujvnunes/no-needless-use-client": "error",
|
|
1151
|
+
"soujvnunes/no-static-jsx-in-client": "error"
|
|
1152
|
+
}
|
|
1153
|
+
};
|
|
396
1154
|
const scriptsOverride = {
|
|
397
1155
|
files: ["scripts/**/*.mjs"],
|
|
398
|
-
languageOptions: { globals: { ...
|
|
1156
|
+
languageOptions: { globals: { ...globals2.node } },
|
|
399
1157
|
rules: { "no-console": "off" }
|
|
400
1158
|
};
|
|
401
1159
|
return [
|
|
@@ -423,7 +1181,7 @@ var buildConfig = ({
|
|
|
423
1181
|
prettier,
|
|
424
1182
|
oneLineCommentsOverride,
|
|
425
1183
|
rootConfigOverride,
|
|
426
|
-
...next ? [nextFileConventionsOverride] : [],
|
|
1184
|
+
...next ? [nextFileConventionsOverride, clientBoundaryOverride] : [],
|
|
427
1185
|
scriptsOverride,
|
|
428
1186
|
...extend
|
|
429
1187
|
];
|
|
@@ -434,5 +1192,5 @@ export {
|
|
|
434
1192
|
createBaseConfig,
|
|
435
1193
|
createNextConfig,
|
|
436
1194
|
oneLineComments,
|
|
437
|
-
|
|
1195
|
+
soujvnunesPlugin
|
|
438
1196
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soujvnunes/eslint-config",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Shared flat ESLint config factories (base + Next.js).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "soujvnunes",
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@eslint/js": "^9.39.4",
|
|
40
40
|
"@next/eslint-plugin-next": "^16.2.2",
|
|
41
|
+
"@typescript-eslint/utils": "^8.58.0",
|
|
41
42
|
"eslint-config-prettier": "^10.1.8",
|
|
42
43
|
"eslint-import-resolver-typescript": "^4.4.5",
|
|
43
44
|
"eslint-plugin-better-tailwindcss": "^4.7.0",
|