eslint-plugin-react-x 5.30.0-beta.5 → 5.30.0-beta.7
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/dist/index.js +180 -113
- package/package.json +7 -7
package/dist/index.js
CHANGED
|
@@ -2,13 +2,13 @@ import { DEFAULT_ESLINT_REACT_SETTINGS, getSettingsFromContext, toRegExp } from
|
|
|
2
2
|
import * as ESLintUtils from "@typescript-eslint/utils/eslint-utils";
|
|
3
3
|
import { Analyze, Check, Collect, Compare, Extract, Traverse } from "@eslint-react/ast";
|
|
4
4
|
import * as core from "@eslint-react/core";
|
|
5
|
-
import { getConstrainedTypeAtLocation, getText, merge, report } from "@eslint-react/eslint";
|
|
5
|
+
import { complete, getConstrainedTypeAtLocation, getText, merge, report } from "@eslint-react/eslint";
|
|
6
6
|
import { AST_NODE_TYPES } from "@typescript-eslint/types";
|
|
7
7
|
import { P, isMatching, match } from "ts-pattern";
|
|
8
|
-
import { isAssignmentTargetEqual, isInitializedFromReact,
|
|
8
|
+
import { isAssignmentTargetEqual, isInitializedFromReact, resolveEnclosingAssignmentTarget, resolveObjectType, resolveOrigin, resolveOriginChain } from "@eslint-react/var";
|
|
9
9
|
import { DefinitionType, ScopeType } from "@typescript-eslint/scope-manager";
|
|
10
10
|
import { findVariable, getStaticValue } from "@typescript-eslint/utils/ast-utils";
|
|
11
|
-
import { findParentAttribute,
|
|
11
|
+
import { findParentAttribute, getElementFullType, getElementSelfType, hasAttribute, isInsideCreateElementProps, isPlainAttribute, parseCreateElementCall } from "@eslint-react/jsx";
|
|
12
12
|
import { compare } from "compare-versions";
|
|
13
13
|
import { unionConstituents } from "ts-api-utils";
|
|
14
14
|
import "typescript";
|
|
@@ -130,7 +130,7 @@ const rules$6 = {
|
|
|
130
130
|
};
|
|
131
131
|
// package.json
|
|
132
132
|
var name$6 = "eslint-plugin-react-x";
|
|
133
|
-
var version = "5.30.0-beta.
|
|
133
|
+
var version = "5.30.0-beta.7";
|
|
134
134
|
// src/utils/create-rule.ts
|
|
135
135
|
function getDocsUrl(ruleName) {
|
|
136
136
|
return `https://eslint-react.xyz/docs/rules/${ruleName}`;
|
|
@@ -1160,14 +1160,13 @@ function resolveGlobalOrigin(context, node, seen = /* @__PURE__ */ new Set()) {
|
|
|
1160
1160
|
if (initializer.type !== AST_NODE_TYPES.Identifier && initializer.type !== AST_NODE_TYPES.MemberExpression) return null;
|
|
1161
1161
|
return resolveGlobalOrigin(context, initializer, seen);
|
|
1162
1162
|
}
|
|
1163
|
-
function resolveToFunction(context, node
|
|
1163
|
+
function resolveToFunction(context, node) {
|
|
1164
1164
|
const expression = Extract.unwrap(node);
|
|
1165
1165
|
if (Check.isFunction(expression)) return expression;
|
|
1166
|
-
if (!Check.isIdentifier(expression)
|
|
1167
|
-
|
|
1168
|
-
const
|
|
1169
|
-
|
|
1170
|
-
return resolveToFunction(context, resolved, seen);
|
|
1166
|
+
if (!Check.isIdentifier(expression)) return null;
|
|
1167
|
+
const origin = resolveOriginChain(context, expression);
|
|
1168
|
+
const unwrapped = origin == null ? null : Extract.unwrap(origin);
|
|
1169
|
+
return unwrapped != null && Check.isFunction(unwrapped) ? unwrapped : null;
|
|
1171
1170
|
}
|
|
1172
1171
|
// src/rules/globals/effects.ts
|
|
1173
1172
|
function inferGlobalMutations(context, facts) {
|
|
@@ -1408,13 +1407,13 @@ function resolveVariableOrigin(context, variable, mode = "alias", seen = /* @__P
|
|
|
1408
1407
|
const source = findVariable(context.sourceCode.getScope(init), init);
|
|
1409
1408
|
return source == null ? variable : resolveVariableOrigin(context, source, mode, seen);
|
|
1410
1409
|
}
|
|
1411
|
-
function resolveToFunctionNode(context, node
|
|
1410
|
+
function resolveToFunctionNode(context, node) {
|
|
1412
1411
|
const expr = Extract.unwrap(node);
|
|
1413
1412
|
if (Check.isFunction(expr)) return expr;
|
|
1414
|
-
if (!Check.isIdentifier(expr)
|
|
1415
|
-
|
|
1416
|
-
const
|
|
1417
|
-
return
|
|
1413
|
+
if (!Check.isIdentifier(expr)) return null;
|
|
1414
|
+
const origin = resolveOriginChain(context, expr);
|
|
1415
|
+
const unwrapped = origin == null ? null : Extract.unwrap(origin);
|
|
1416
|
+
return unwrapped != null && Check.isFunction(unwrapped) ? unwrapped : null;
|
|
1418
1417
|
}
|
|
1419
1418
|
function isComponentPropsDefinition(def, components) {
|
|
1420
1419
|
if (def.type !== DefinitionType.Parameter) return false;
|
|
@@ -1727,7 +1726,7 @@ function isArrayIndexReference(context, node) {
|
|
|
1727
1726
|
if (call.arguments[callbackPosition] !== argument) return false;
|
|
1728
1727
|
const param = callback.params[indexPosition];
|
|
1729
1728
|
if (param == null) return false;
|
|
1730
|
-
return
|
|
1729
|
+
return (param.type === AST_NODE_TYPES.AssignmentPattern ? param.left : param) === def.name;
|
|
1731
1730
|
}
|
|
1732
1731
|
function getIdentifiersFromBinaryExpression(side) {
|
|
1733
1732
|
if (Check.isIdentifier(side)) return [side];
|
|
@@ -1749,9 +1748,10 @@ function create$46(context) {
|
|
|
1749
1748
|
return node != null && Check.isIdentifier(node) && isArrayIndexReference(context, node);
|
|
1750
1749
|
}
|
|
1751
1750
|
function getPropsObject(node) {
|
|
1752
|
-
const
|
|
1753
|
-
if (
|
|
1751
|
+
const call = parseCreateElementCall(context, node);
|
|
1752
|
+
if (call != null) return call.propsObject;
|
|
1754
1753
|
if (!core.isCloneElementCall(context, node)) return null;
|
|
1754
|
+
const props = node.arguments[1];
|
|
1755
1755
|
return props?.type === AST_NODE_TYPES.ObjectExpression ? props : null;
|
|
1756
1756
|
}
|
|
1757
1757
|
function visitKeyExpression(node) {
|
|
@@ -1815,6 +1815,7 @@ var no_children_count_default = createRule({
|
|
|
1815
1815
|
create: create$45
|
|
1816
1816
|
});
|
|
1817
1817
|
function create$45(context) {
|
|
1818
|
+
if (!context.sourceCode.text.includes("Children")) return {};
|
|
1818
1819
|
return { MemberExpression(node) {
|
|
1819
1820
|
if (core.isChildrenCount(context, node)) context.report({
|
|
1820
1821
|
messageId: "default",
|
|
@@ -1833,6 +1834,7 @@ var no_children_for_each_default = createRule({
|
|
|
1833
1834
|
create: create$44
|
|
1834
1835
|
});
|
|
1835
1836
|
function create$44(context) {
|
|
1837
|
+
if (!context.sourceCode.text.includes("Children")) return {};
|
|
1836
1838
|
return { MemberExpression(node) {
|
|
1837
1839
|
if (core.isChildrenForEach(context, node)) context.report({
|
|
1838
1840
|
messageId: "default",
|
|
@@ -1851,6 +1853,7 @@ var no_children_map_default = createRule({
|
|
|
1851
1853
|
create: create$43
|
|
1852
1854
|
});
|
|
1853
1855
|
function create$43(context) {
|
|
1856
|
+
if (!context.sourceCode.text.includes("Children")) return {};
|
|
1854
1857
|
return { MemberExpression(node) {
|
|
1855
1858
|
if (core.isChildrenMap(context, node)) context.report({
|
|
1856
1859
|
messageId: "default",
|
|
@@ -1869,6 +1872,7 @@ var no_children_only_default = createRule({
|
|
|
1869
1872
|
create: create$42
|
|
1870
1873
|
});
|
|
1871
1874
|
function create$42(context) {
|
|
1875
|
+
if (!context.sourceCode.text.includes("Children")) return {};
|
|
1872
1876
|
return { MemberExpression(node) {
|
|
1873
1877
|
if (core.isChildrenOnly(context, node)) context.report({
|
|
1874
1878
|
messageId: "default",
|
|
@@ -1887,6 +1891,7 @@ var no_children_to_array_default = createRule({
|
|
|
1887
1891
|
create: create$41
|
|
1888
1892
|
});
|
|
1889
1893
|
function create$41(context) {
|
|
1894
|
+
if (!context.sourceCode.text.includes("Children")) return {};
|
|
1890
1895
|
return { MemberExpression(node) {
|
|
1891
1896
|
if (core.isChildrenToArray(context, node)) context.report({
|
|
1892
1897
|
messageId: "default",
|
|
@@ -2070,6 +2075,7 @@ var no_clone_element_default = createRule({
|
|
|
2070
2075
|
create: create$39
|
|
2071
2076
|
});
|
|
2072
2077
|
function create$39(context) {
|
|
2078
|
+
if (!context.sourceCode.text.includes("cloneElement")) return {};
|
|
2073
2079
|
return { CallExpression(node) {
|
|
2074
2080
|
if (core.isCloneElementCall(context, node)) context.report({
|
|
2075
2081
|
messageId: "default",
|
|
@@ -2247,6 +2253,20 @@ var no_direct_mutation_state_default = createRule({
|
|
|
2247
2253
|
name: "no-direct-mutation-state",
|
|
2248
2254
|
create: create$33
|
|
2249
2255
|
});
|
|
2256
|
+
function create$33(context) {
|
|
2257
|
+
if (!context.sourceCode.text.includes("Component")) return {};
|
|
2258
|
+
return { AssignmentExpression(node) {
|
|
2259
|
+
if (!isAssignmentToThisState(node)) return;
|
|
2260
|
+
const fact = Traverse.findParent(node, Check.isClass);
|
|
2261
|
+
if (fact == null || !core.isClassComponent(fact)) return;
|
|
2262
|
+
const constructor = Traverse.findParent(node, isConstructorFunction);
|
|
2263
|
+
if (Traverse.findParent(node, Check.isFunctionOrClass) === constructor) return;
|
|
2264
|
+
context.report({
|
|
2265
|
+
messageId: "default",
|
|
2266
|
+
node
|
|
2267
|
+
});
|
|
2268
|
+
} };
|
|
2269
|
+
}
|
|
2250
2270
|
function isConstructorFunction(node) {
|
|
2251
2271
|
return Check.isOneOf([AST_NODE_TYPES.FunctionDeclaration, AST_NODE_TYPES.FunctionExpression])(node) && Check.isPropertyOrMethod(node.parent) && Check.isIdentifier(node.parent.key, "constructor");
|
|
2252
2272
|
}
|
|
@@ -2261,16 +2281,23 @@ function isAssignmentToThisState(node) {
|
|
|
2261
2281
|
}
|
|
2262
2282
|
return false;
|
|
2263
2283
|
}
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2284
|
+
// src/utils/jsx-iterator.ts
|
|
2285
|
+
const KNOWN_CALLBACK_POSITIONS = /* @__PURE__ */ new Map([
|
|
2286
|
+
["flatMap", 0],
|
|
2287
|
+
["from", 1],
|
|
2288
|
+
["map", 0]
|
|
2289
|
+
]);
|
|
2290
|
+
function getIteratorCallback(node) {
|
|
2291
|
+
const callee = Extract.unwrap(node.callee);
|
|
2292
|
+
if (callee.type !== AST_NODE_TYPES.MemberExpression) return null;
|
|
2293
|
+
if (!Check.isIdentifier(callee.property)) return null;
|
|
2294
|
+
const position = KNOWN_CALLBACK_POSITIONS.get(callee.property.name);
|
|
2295
|
+
if (position == null) return null;
|
|
2296
|
+
const argument = node.arguments[position];
|
|
2297
|
+
if (argument == null) return null;
|
|
2298
|
+
const callback = Extract.unwrap(argument);
|
|
2299
|
+
if (!Check.isFunction(callback)) return null;
|
|
2300
|
+
return callback;
|
|
2274
2301
|
}
|
|
2275
2302
|
var no_duplicate_key_default = createRule({
|
|
2276
2303
|
meta: {
|
|
@@ -2283,59 +2310,72 @@ var no_duplicate_key_default = createRule({
|
|
|
2283
2310
|
create: create$32
|
|
2284
2311
|
});
|
|
2285
2312
|
function create$32(context) {
|
|
2286
|
-
if (
|
|
2287
|
-
|
|
2313
|
+
if (!/\bkey\b/u.test(context.sourceCode.text)) return {};
|
|
2314
|
+
let childrenRekeyingDepth = 0;
|
|
2315
|
+
const duplicateKeys = /* @__PURE__ */ new Set();
|
|
2316
|
+
const siblingKeys = /* @__PURE__ */ new Map();
|
|
2317
|
+
function getKeyValue(node) {
|
|
2318
|
+
const { value } = node;
|
|
2319
|
+
if (value == null) return null;
|
|
2320
|
+
return value.type === AST_NODE_TYPES.JSXExpressionContainer ? Extract.unwrap(value.expression) : value;
|
|
2321
|
+
}
|
|
2288
2322
|
function isKeyValueEqual(a, b) {
|
|
2289
|
-
const aValue = a
|
|
2290
|
-
const bValue = b
|
|
2291
|
-
if (aValue == null || bValue == null) return
|
|
2323
|
+
const aValue = getKeyValue(a);
|
|
2324
|
+
const bValue = getKeyValue(b);
|
|
2325
|
+
if (aValue == null || bValue == null) return aValue === bValue;
|
|
2292
2326
|
return Compare.isEqual(aValue, bValue);
|
|
2293
2327
|
}
|
|
2328
|
+
function isStaticKey(node) {
|
|
2329
|
+
const value = getKeyValue(node);
|
|
2330
|
+
if (value == null) return true;
|
|
2331
|
+
if (value.type === AST_NODE_TYPES.Literal) return typeof value.value === "string" || typeof value.value === "number";
|
|
2332
|
+
return value.type === AST_NODE_TYPES.TemplateLiteral && value.expressions.length === 0;
|
|
2333
|
+
}
|
|
2334
|
+
function isChildrenRekeyingCall(node) {
|
|
2335
|
+
return core.isChildrenToArrayCall(context, node) || core.isChildrenMapCall(context, node);
|
|
2336
|
+
}
|
|
2294
2337
|
return {
|
|
2295
2338
|
"JSXAttribute[name.name='key']"(node) {
|
|
2339
|
+
if (childrenRekeyingDepth > 0) return;
|
|
2296
2340
|
const jsxElement = node.parent.parent;
|
|
2297
2341
|
switch (jsxElement.parent.type) {
|
|
2298
2342
|
case AST_NODE_TYPES.ArrayExpression:
|
|
2299
2343
|
case AST_NODE_TYPES.JSXElement:
|
|
2300
2344
|
case AST_NODE_TYPES.JSXFragment: {
|
|
2301
2345
|
const root = jsxElement.parent;
|
|
2302
|
-
const prevKeys =
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2346
|
+
const prevKeys = siblingKeys.get(root) ?? [];
|
|
2347
|
+
for (const prevKey of prevKeys) {
|
|
2348
|
+
if (!isKeyValueEqual(prevKey, node)) continue;
|
|
2349
|
+
duplicateKeys.add(prevKey);
|
|
2350
|
+
duplicateKeys.add(node);
|
|
2351
|
+
break;
|
|
2352
|
+
}
|
|
2353
|
+
siblingKeys.set(root, [...prevKeys, node]);
|
|
2308
2354
|
break;
|
|
2309
2355
|
}
|
|
2310
2356
|
default: {
|
|
2311
|
-
const call = Traverse.findParent(jsxElement, (n) =>
|
|
2312
|
-
if (n.type !== AST_NODE_TYPES.CallExpression) return false;
|
|
2313
|
-
const callee = Extract.unwrap(n.callee);
|
|
2314
|
-
if (callee.type !== AST_NODE_TYPES.MemberExpression) return false;
|
|
2315
|
-
return Check.isIdentifier(callee.property, "map");
|
|
2316
|
-
});
|
|
2357
|
+
const call = Traverse.findParent(jsxElement, (n) => n.type === AST_NODE_TYPES.CallExpression && getIteratorCallback(n) != null);
|
|
2317
2358
|
if (call == null) return;
|
|
2359
|
+
const callback = getIteratorCallback(call);
|
|
2360
|
+
if (callback == null) return;
|
|
2318
2361
|
const iter = Traverse.findParent(jsxElement, Check.isFunction, (n) => n === call);
|
|
2319
2362
|
if (iter == null) return;
|
|
2320
|
-
|
|
2321
|
-
if (
|
|
2322
|
-
if (Extract.unwrap(arg0) !== iter) return;
|
|
2323
|
-
keyedEntries.set(call, {
|
|
2324
|
-
hasDuplicate: node.value?.type === AST_NODE_TYPES.Literal,
|
|
2325
|
-
keys: [node],
|
|
2326
|
-
root: call
|
|
2327
|
-
});
|
|
2363
|
+
if (callback !== iter) return;
|
|
2364
|
+
if (isStaticKey(node)) duplicateKeys.add(node);
|
|
2328
2365
|
}
|
|
2329
2366
|
}
|
|
2330
2367
|
},
|
|
2368
|
+
CallExpression(node) {
|
|
2369
|
+
if (isChildrenRekeyingCall(node)) childrenRekeyingDepth += 1;
|
|
2370
|
+
},
|
|
2371
|
+
"CallExpression:exit"(node) {
|
|
2372
|
+
if (isChildrenRekeyingCall(node)) childrenRekeyingDepth -= 1;
|
|
2373
|
+
},
|
|
2331
2374
|
"Program:exit"() {
|
|
2332
|
-
for (const
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
node: key
|
|
2337
|
-
});
|
|
2338
|
-
}
|
|
2375
|
+
for (const key of duplicateKeys) context.report({
|
|
2376
|
+
messageId: "default",
|
|
2377
|
+
node: key
|
|
2378
|
+
});
|
|
2339
2379
|
}
|
|
2340
2380
|
};
|
|
2341
2381
|
}
|
|
@@ -2466,6 +2506,7 @@ var no_implicit_children_default = createRule({
|
|
|
2466
2506
|
create: create$30
|
|
2467
2507
|
});
|
|
2468
2508
|
function create$30(context) {
|
|
2509
|
+
if (!context.sourceCode.text.includes("...")) return {};
|
|
2469
2510
|
return createImplicitPropListener(context, {
|
|
2470
2511
|
name: "children",
|
|
2471
2512
|
isAllowedProp: (fqn) => fqn.endsWith("attributes.children") || fqn.endsWith("propswithchildren.children"),
|
|
@@ -2489,6 +2530,7 @@ var no_implicit_key_default = createRule({
|
|
|
2489
2530
|
create: create$29
|
|
2490
2531
|
});
|
|
2491
2532
|
function create$29(context) {
|
|
2533
|
+
if (!context.sourceCode.text.includes("...")) return {};
|
|
2492
2534
|
return createImplicitPropListener(context, {
|
|
2493
2535
|
name: "key",
|
|
2494
2536
|
isAllowedProp: (fqn) => fqn.endsWith("react.attributes.key"),
|
|
@@ -2515,6 +2557,7 @@ var no_implicit_ref_default = createRule({
|
|
|
2515
2557
|
create: create$28
|
|
2516
2558
|
});
|
|
2517
2559
|
function create$28(context) {
|
|
2560
|
+
if (!context.sourceCode.text.includes("...")) return {};
|
|
2518
2561
|
return createImplicitPropListener(context, {
|
|
2519
2562
|
name: "ref",
|
|
2520
2563
|
isAllowedProp: (fqn) => fqn.endsWith("attributes.ref"),
|
|
@@ -2659,7 +2702,19 @@ function create$25(context) {
|
|
|
2659
2702
|
})) context.report({
|
|
2660
2703
|
fix(fixer) {
|
|
2661
2704
|
if (id.type !== AST_NODE_TYPES.Identifier || id.parent !== call.parent) return [];
|
|
2662
|
-
|
|
2705
|
+
const declaration = id.parent.parent;
|
|
2706
|
+
if (declaration == null || declaration.type !== AST_NODE_TYPES.VariableDeclaration) return [];
|
|
2707
|
+
switch (declaration.parent.type) {
|
|
2708
|
+
case AST_NODE_TYPES.BlockStatement:
|
|
2709
|
+
case AST_NODE_TYPES.Program:
|
|
2710
|
+
case AST_NODE_TYPES.StaticBlock:
|
|
2711
|
+
case AST_NODE_TYPES.SwitchCase:
|
|
2712
|
+
case AST_NODE_TYPES.TSModuleBlock: break;
|
|
2713
|
+
default: return [];
|
|
2714
|
+
}
|
|
2715
|
+
const lastToken = context.sourceCode.getLastToken(declaration);
|
|
2716
|
+
if (lastToken == null) return [];
|
|
2717
|
+
return fixer.insertTextAfter(lastToken, [
|
|
2663
2718
|
"\n",
|
|
2664
2719
|
id.name,
|
|
2665
2720
|
".",
|
|
@@ -2678,12 +2733,6 @@ function create$25(context) {
|
|
|
2678
2733
|
}
|
|
2679
2734
|
};
|
|
2680
2735
|
}
|
|
2681
|
-
// src/rules/no-missing-key/lib.ts
|
|
2682
|
-
const KNOWN_CALLBACK_POSITIONS = /* @__PURE__ */ new Map([
|
|
2683
|
-
["flatMap", 0],
|
|
2684
|
-
["from", 1],
|
|
2685
|
-
["map", 0]
|
|
2686
|
-
]);
|
|
2687
2736
|
var no_missing_key_default = createRule({
|
|
2688
2737
|
meta: {
|
|
2689
2738
|
type: "problem",
|
|
@@ -2697,16 +2746,6 @@ var no_missing_key_default = createRule({
|
|
|
2697
2746
|
name: "no-missing-key",
|
|
2698
2747
|
create: create$24
|
|
2699
2748
|
});
|
|
2700
|
-
function getIteratorCallback(node) {
|
|
2701
|
-
const callee = Extract.unwrap(node.callee);
|
|
2702
|
-
if (callee.type !== AST_NODE_TYPES.MemberExpression) return null;
|
|
2703
|
-
if (!Check.isIdentifier(callee.property)) return null;
|
|
2704
|
-
const position = KNOWN_CALLBACK_POSITIONS.get(callee.property.name);
|
|
2705
|
-
if (position == null) return null;
|
|
2706
|
-
const callback = node.arguments[position];
|
|
2707
|
-
if (callback == null || !Check.isFunction(callback)) return null;
|
|
2708
|
-
return callback;
|
|
2709
|
-
}
|
|
2710
2749
|
function create$24(context) {
|
|
2711
2750
|
let childrenToArrayDepth = 0;
|
|
2712
2751
|
function visitItemExpression(node) {
|
|
@@ -2717,10 +2756,13 @@ function create$24(context) {
|
|
|
2717
2756
|
case AST_NODE_TYPES.TSNonNullExpression:
|
|
2718
2757
|
case AST_NODE_TYPES.TSSatisfiesExpression:
|
|
2719
2758
|
case AST_NODE_TYPES.TSInstantiationExpression: return visitItemExpression(node.expression);
|
|
2720
|
-
case AST_NODE_TYPES.JSXElement:
|
|
2721
|
-
|
|
2722
|
-
node
|
|
2723
|
-
|
|
2759
|
+
case AST_NODE_TYPES.JSXElement:
|
|
2760
|
+
if (hasAttribute(node, "key")) return [];
|
|
2761
|
+
if (node.openingElement.attributes.some((a) => a.type === AST_NODE_TYPES.JSXSpreadAttribute)) return [];
|
|
2762
|
+
return [{
|
|
2763
|
+
messageId: "default",
|
|
2764
|
+
node
|
|
2765
|
+
}];
|
|
2724
2766
|
case AST_NODE_TYPES.JSXFragment: return [{
|
|
2725
2767
|
messageId: "unexpected-fragment-syntax",
|
|
2726
2768
|
node
|
|
@@ -2962,6 +3004,7 @@ var no_nested_lazy_component_declarations_default = createRule({
|
|
|
2962
3004
|
create: create$21
|
|
2963
3005
|
});
|
|
2964
3006
|
function create$21(context) {
|
|
3007
|
+
if (!context.sourceCode.text.includes("lazy")) return {};
|
|
2965
3008
|
const fc = core.getFunctionComponentCollector(context);
|
|
2966
3009
|
const cc = core.getClassComponentCollector(context);
|
|
2967
3010
|
const hc = core.getHookCollector(context);
|
|
@@ -3206,14 +3249,15 @@ function create$13(context) {
|
|
|
3206
3249
|
const constructions = /* @__PURE__ */ new WeakMap();
|
|
3207
3250
|
return merge(visitor, {
|
|
3208
3251
|
JSXOpeningElement(node) {
|
|
3209
|
-
const selfName =
|
|
3210
|
-
if (selfName
|
|
3252
|
+
const selfName = getElementSelfType(node.parent);
|
|
3253
|
+
if (selfName === "") return;
|
|
3211
3254
|
if (!isContextName(selfName, isReact18OrBelow)) return;
|
|
3212
3255
|
const enclosingFunction = Traverse.findParent(node, Check.isFunction);
|
|
3213
3256
|
if (enclosingFunction == null) return;
|
|
3214
3257
|
if (compilationMode === "annotation" && core.isFunctionHasDirective(enclosingFunction, "use memo")) return;
|
|
3215
|
-
const attribute = node.attributes.findLast(
|
|
3216
|
-
if (attribute?.
|
|
3258
|
+
const attribute = node.attributes.findLast(isPlainAttribute("value"));
|
|
3259
|
+
if (attribute?.type !== AST_NODE_TYPES.JSXAttribute) return;
|
|
3260
|
+
if (attribute.value?.type !== AST_NODE_TYPES.JSXExpressionContainer) return;
|
|
3217
3261
|
const valueExpression = attribute.value.expression;
|
|
3218
3262
|
const construction = resolveObjectType(context, valueExpression);
|
|
3219
3263
|
if (construction == null) return;
|
|
@@ -3261,6 +3305,7 @@ const schema$1 = [{
|
|
|
3261
3305
|
additionalProperties: false,
|
|
3262
3306
|
properties: { safeDefaultProps: {
|
|
3263
3307
|
type: "array",
|
|
3308
|
+
description: "Property names or patterns that are safe to use as default props.",
|
|
3264
3309
|
items: { type: "string" }
|
|
3265
3310
|
} }
|
|
3266
3311
|
}];
|
|
@@ -3275,13 +3320,13 @@ var no_unstable_default_props_default = createRule({
|
|
|
3275
3320
|
name: RULE_NAME$12,
|
|
3276
3321
|
create: create$12
|
|
3277
3322
|
});
|
|
3278
|
-
function create$12(context
|
|
3323
|
+
function create$12(context) {
|
|
3324
|
+
const { safeDefaultProps } = complete(context.options[0], defaultOptions$1[0]);
|
|
3279
3325
|
const { compilationMode } = getSettingsFromContext(context);
|
|
3280
3326
|
if (compilationMode === "infer" || compilationMode === "all") return {};
|
|
3281
3327
|
if (compilationMode === "annotation" && context.sourceCode.ast.body.some((stmt) => Check.isDirective(stmt, "use memo"))) return {};
|
|
3282
3328
|
const { api, visitor } = core.getFunctionComponentCollector(context);
|
|
3283
3329
|
const declarators = /* @__PURE__ */ new WeakMap();
|
|
3284
|
-
const { safeDefaultProps = [] } = options;
|
|
3285
3330
|
const safePatterns = safeDefaultProps.map((s) => toRegExp(s));
|
|
3286
3331
|
return merge(visitor, {
|
|
3287
3332
|
[SEL_OBJECT_DESTRUCTURING_VARIABLE_DECLARATOR](node) {
|
|
@@ -3355,8 +3400,7 @@ function isExportedClass(node) {
|
|
|
3355
3400
|
default: return false;
|
|
3356
3401
|
}
|
|
3357
3402
|
}
|
|
3358
|
-
function isPublicMethod(
|
|
3359
|
-
const member = def.parent;
|
|
3403
|
+
function isPublicMethod(member) {
|
|
3360
3404
|
switch (member.type) {
|
|
3361
3405
|
case AST_NODE_TYPES.MethodDefinition: return member.accessibility == null || member.accessibility === "public";
|
|
3362
3406
|
case AST_NODE_TYPES.PropertyDefinition: return (member.accessibility == null || member.accessibility === "public") && (member.value?.type === AST_NODE_TYPES.ArrowFunctionExpression || member.value?.type === AST_NODE_TYPES.FunctionExpression);
|
|
@@ -3374,8 +3418,17 @@ var no_unused_class_component_members_default = createRule({
|
|
|
3374
3418
|
create: create$11
|
|
3375
3419
|
});
|
|
3376
3420
|
function create$11(context) {
|
|
3421
|
+
if (!context.sourceCode.text.includes("Component")) return {};
|
|
3377
3422
|
const propertyDefs = /* @__PURE__ */ new WeakMap();
|
|
3423
|
+
const publicMethods = /* @__PURE__ */ new WeakMap();
|
|
3378
3424
|
const propertyUsages = /* @__PURE__ */ new WeakMap();
|
|
3425
|
+
function getStaticName(key, computed) {
|
|
3426
|
+
const unwrapped = Extract.unwrap(key);
|
|
3427
|
+
if (!computed && Check.isIdentifier(unwrapped)) return unwrapped.name;
|
|
3428
|
+
if (unwrapped.type === AST_NODE_TYPES.Literal && typeof unwrapped.value === "string") return unwrapped.value;
|
|
3429
|
+
if (unwrapped.type === AST_NODE_TYPES.TemplateLiteral && unwrapped.expressions.length === 0) return unwrapped.quasis[0]?.value.cooked ?? unwrapped.quasis[0]?.value.raw ?? null;
|
|
3430
|
+
return null;
|
|
3431
|
+
}
|
|
3379
3432
|
function classExit(node) {
|
|
3380
3433
|
if (!core.isClassComponent(node)) return;
|
|
3381
3434
|
const id = core.getClassId(node);
|
|
@@ -3383,21 +3436,20 @@ function create$11(context) {
|
|
|
3383
3436
|
if (defs == null) return;
|
|
3384
3437
|
const usages = propertyUsages.get(node);
|
|
3385
3438
|
const exported = isExportedClass(node);
|
|
3386
|
-
|
|
3387
|
-
|
|
3439
|
+
const publicMembers = publicMethods.get(node);
|
|
3440
|
+
for (const [methodName, defNode] of defs) {
|
|
3388
3441
|
if (usages?.has(methodName) ?? false) continue;
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
}
|
|
3442
|
+
const isPureComponentShouldUpdate = methodName === "shouldComponentUpdate" && core.isPureComponent(node);
|
|
3443
|
+
if (LIFECYCLE_METHODS.has(methodName) && !isPureComponentShouldUpdate) continue;
|
|
3392
3444
|
if (KNOWN_HOST_CONVENTION_METHODS.has(methodName)) continue;
|
|
3393
|
-
if (exported &&
|
|
3445
|
+
if (exported && (publicMembers?.has(methodName) ?? false)) continue;
|
|
3394
3446
|
context.report({
|
|
3395
3447
|
data: {
|
|
3396
3448
|
className: id != null ? getText(context, id) : "Component",
|
|
3397
3449
|
methodName
|
|
3398
3450
|
},
|
|
3399
3451
|
messageId: "default",
|
|
3400
|
-
node:
|
|
3452
|
+
node: defNode
|
|
3401
3453
|
});
|
|
3402
3454
|
}
|
|
3403
3455
|
}
|
|
@@ -3405,7 +3457,11 @@ function create$11(context) {
|
|
|
3405
3457
|
const currentClass = Traverse.findParent(node, Check.isClass);
|
|
3406
3458
|
if (currentClass == null || !core.isClassComponent(currentClass)) return;
|
|
3407
3459
|
if (node.static) return;
|
|
3408
|
-
if (
|
|
3460
|
+
if (node.computed || !Check.isIdentifier(node.key)) return;
|
|
3461
|
+
const methodName = node.key.name;
|
|
3462
|
+
const defs = getOrInsertComputed(propertyDefs, currentClass, () => /* @__PURE__ */ new Map());
|
|
3463
|
+
if (!defs.has(methodName)) defs.set(methodName, node.key);
|
|
3464
|
+
if (isPublicMethod(node)) getOrInsertComputed(publicMethods, currentClass, () => /* @__PURE__ */ new Set()).add(methodName);
|
|
3409
3465
|
}
|
|
3410
3466
|
return {
|
|
3411
3467
|
"ClassDeclaration:exit": classExit,
|
|
@@ -3415,12 +3471,17 @@ function create$11(context) {
|
|
|
3415
3471
|
const currentMethod = Traverse.findParent(node, Check.isPropertyOrMethod);
|
|
3416
3472
|
if (currentClass == null || currentMethod == null) return;
|
|
3417
3473
|
if (!core.isClassComponent(currentClass) || currentMethod.static) return;
|
|
3418
|
-
if (node.object.type !== AST_NODE_TYPES.ThisExpression
|
|
3474
|
+
if (node.object.type !== AST_NODE_TYPES.ThisExpression) return;
|
|
3419
3475
|
if (node.parent.type === AST_NODE_TYPES.AssignmentExpression && node.parent.left === node) {
|
|
3420
|
-
|
|
3476
|
+
if (!node.computed && Check.isIdentifier(node.property)) {
|
|
3477
|
+
const defs = getOrInsertComputed(propertyDefs, currentClass, () => /* @__PURE__ */ new Map());
|
|
3478
|
+
if (!defs.has(node.property.name)) defs.set(node.property.name, node.property);
|
|
3479
|
+
}
|
|
3421
3480
|
return;
|
|
3422
3481
|
}
|
|
3423
|
-
|
|
3482
|
+
const propertyName = getStaticName(node.property, node.computed);
|
|
3483
|
+
if (propertyName == null) return;
|
|
3484
|
+
getOrInsertComputed(propertyUsages, currentClass, () => /* @__PURE__ */ new Set()).add(propertyName);
|
|
3424
3485
|
},
|
|
3425
3486
|
MethodDefinition: methodEnter,
|
|
3426
3487
|
PropertyDefinition: methodEnter,
|
|
@@ -3429,8 +3490,11 @@ function create$11(context) {
|
|
|
3429
3490
|
const currentMethod = Traverse.findParent(node, Check.isPropertyOrMethod);
|
|
3430
3491
|
if (currentClass == null || currentMethod == null) return;
|
|
3431
3492
|
if (!core.isClassComponent(currentClass) || currentMethod.static) return;
|
|
3432
|
-
if (node.init != null && node.init.type === AST_NODE_TYPES.ThisExpression && node.id.type === AST_NODE_TYPES.ObjectPattern) {
|
|
3433
|
-
|
|
3493
|
+
if (node.init != null && node.init.type === AST_NODE_TYPES.ThisExpression && node.id.type === AST_NODE_TYPES.ObjectPattern) for (const prop of node.id.properties) {
|
|
3494
|
+
if (prop.type !== AST_NODE_TYPES.Property) continue;
|
|
3495
|
+
const name = getStaticName(prop.key, prop.computed);
|
|
3496
|
+
if (name == null) continue;
|
|
3497
|
+
getOrInsertComputed(propertyUsages, currentClass, () => /* @__PURE__ */ new Set()).add(name);
|
|
3434
3498
|
}
|
|
3435
3499
|
}
|
|
3436
3500
|
};
|
|
@@ -3440,7 +3504,7 @@ function collectUsedPropKeysOfParameter(context, usedPropKeys, parameter) {
|
|
|
3440
3504
|
switch (parameter.type) {
|
|
3441
3505
|
case AST_NODE_TYPES.Identifier: return collectUsedPropKeysOfIdentifier(context, usedPropKeys, parameter);
|
|
3442
3506
|
case AST_NODE_TYPES.ObjectPattern: return collectUsedPropKeysOfObjectPattern(context, usedPropKeys, parameter);
|
|
3443
|
-
case AST_NODE_TYPES.AssignmentPattern: return collectUsedPropKeysOfParameter(context, usedPropKeys,
|
|
3507
|
+
case AST_NODE_TYPES.AssignmentPattern: return collectUsedPropKeysOfParameter(context, usedPropKeys, parameter.left);
|
|
3444
3508
|
default: return false;
|
|
3445
3509
|
}
|
|
3446
3510
|
}
|
|
@@ -4021,7 +4085,7 @@ function hasUseClientDirective(program) {
|
|
|
4021
4085
|
function getDestructuredPropertyName(pattern, name) {
|
|
4022
4086
|
for (const property of pattern.properties) {
|
|
4023
4087
|
if (property.type !== AST_NODE_TYPES.Property) continue;
|
|
4024
|
-
if (
|
|
4088
|
+
if ((property.value.type === AST_NODE_TYPES.AssignmentPattern ? property.value.left : property.value) !== name) continue;
|
|
4025
4089
|
return Extract.getPropertyName(property, "max");
|
|
4026
4090
|
}
|
|
4027
4091
|
return null;
|
|
@@ -6401,7 +6465,7 @@ function isRefLikeName(name) {
|
|
|
6401
6465
|
return name === "ref" || name.endsWith("Ref");
|
|
6402
6466
|
}
|
|
6403
6467
|
function getParameterId(param) {
|
|
6404
|
-
const target =
|
|
6468
|
+
const target = param.type === AST_NODE_TYPES.AssignmentPattern ? param.left : param;
|
|
6405
6469
|
return target.type === AST_NODE_TYPES.Identifier ? target : null;
|
|
6406
6470
|
}
|
|
6407
6471
|
function getRefSourceName(node) {
|
|
@@ -7187,7 +7251,7 @@ function isConfiguredStableHookValue(variable, configs) {
|
|
|
7187
7251
|
if (id.type !== AST_NODE_TYPES.ArrayPattern) return false;
|
|
7188
7252
|
const index = id.elements.findIndex((element) => {
|
|
7189
7253
|
if (element == null) return false;
|
|
7190
|
-
const binding =
|
|
7254
|
+
const binding = element.type === AST_NODE_TYPES.AssignmentPattern ? element.left : element;
|
|
7191
7255
|
return variable.identifiers.some((identifier) => identifier === binding);
|
|
7192
7256
|
});
|
|
7193
7257
|
return index !== -1 && stableResult.has(index);
|
|
@@ -7351,8 +7415,10 @@ const rule = {
|
|
|
7351
7415
|
} else if (name === "useState" || name === "useReducer" || name === "useActionState") {
|
|
7352
7416
|
const isActionState = name === "useActionState";
|
|
7353
7417
|
if (id.type === "ArrayPattern" && (isActionState ? id.elements.length >= 2 : id.elements.length === 2) && isArray(resolved.identifiers)) {
|
|
7354
|
-
const
|
|
7355
|
-
const
|
|
7418
|
+
const firstNode = id.elements[0] ?? null;
|
|
7419
|
+
const secondNode = id.elements[1] ?? null;
|
|
7420
|
+
const firstElement = firstNode?.type === "AssignmentPattern" ? firstNode.left : firstNode;
|
|
7421
|
+
const secondElement = secondNode?.type === "AssignmentPattern" ? secondNode.left : secondNode;
|
|
7356
7422
|
if (resolved.identifiers.some((identifier) => identifier === secondElement)) {
|
|
7357
7423
|
if (name === "useState") {
|
|
7358
7424
|
const references = resolved.references;
|
|
@@ -7372,7 +7438,8 @@ const rule = {
|
|
|
7372
7438
|
}
|
|
7373
7439
|
} else if (name === "useTransition") {
|
|
7374
7440
|
if (id.type === "ArrayPattern" && id.elements.length === 2 && Array.isArray(resolved.identifiers)) {
|
|
7375
|
-
const
|
|
7441
|
+
const secondNode = id.elements[1] ?? null;
|
|
7442
|
+
const secondElement = secondNode?.type === "AssignmentPattern" ? secondNode.left : secondNode;
|
|
7376
7443
|
if (resolved.identifiers.some((identifier) => identifier === secondElement)) return true;
|
|
7377
7444
|
}
|
|
7378
7445
|
}
|
|
@@ -8351,15 +8418,15 @@ const schema = [{
|
|
|
8351
8418
|
properties: {
|
|
8352
8419
|
enforceAssignment: {
|
|
8353
8420
|
type: "boolean",
|
|
8354
|
-
|
|
8421
|
+
description: "Require the useState result to be assigned to a destructured value/setter pair."
|
|
8355
8422
|
},
|
|
8356
8423
|
enforceLazyInitialization: {
|
|
8357
8424
|
type: "boolean",
|
|
8358
|
-
|
|
8425
|
+
description: "Require expensive initializers to be wrapped in a lazy initializer function."
|
|
8359
8426
|
},
|
|
8360
8427
|
enforceSetterName: {
|
|
8361
8428
|
type: "boolean",
|
|
8362
|
-
|
|
8429
|
+
description: "Require the setter name to match the state value name."
|
|
8363
8430
|
}
|
|
8364
8431
|
}
|
|
8365
8432
|
}];
|
|
@@ -8379,7 +8446,7 @@ var use_state_default = createRule({
|
|
|
8379
8446
|
create
|
|
8380
8447
|
});
|
|
8381
8448
|
function create(context) {
|
|
8382
|
-
const { enforceAssignment
|
|
8449
|
+
const { enforceAssignment, enforceLazyInitialization, enforceSetterName } = complete(context.options[0], defaultOptions[0]);
|
|
8383
8450
|
const { additionalStateHooks } = getSettingsFromContext(context);
|
|
8384
8451
|
return { CallExpression(node) {
|
|
8385
8452
|
if (!core.isUseStateLikeCall(node, additionalStateHooks)) return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-react-x",
|
|
3
|
-
"version": "5.30.0-beta.
|
|
3
|
+
"version": "5.30.0-beta.7",
|
|
4
4
|
"description": "A set of composable ESLint rules for libraries and frameworks that use React as a UI runtime.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -36,12 +36,12 @@
|
|
|
36
36
|
"dist"
|
|
37
37
|
],
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@eslint-react/ast": "5.30.0-beta.
|
|
40
|
-
"@eslint-react/core": "5.30.0-beta.
|
|
41
|
-
"@eslint-react/eslint": "5.30.0-beta.
|
|
42
|
-
"@eslint-react/jsx": "5.30.0-beta.
|
|
43
|
-
"@eslint-react/shared": "5.30.0-beta.
|
|
44
|
-
"@eslint-react/var": "5.30.0-beta.
|
|
39
|
+
"@eslint-react/ast": "5.30.0-beta.7",
|
|
40
|
+
"@eslint-react/core": "5.30.0-beta.7",
|
|
41
|
+
"@eslint-react/eslint": "5.30.0-beta.7",
|
|
42
|
+
"@eslint-react/jsx": "5.30.0-beta.7",
|
|
43
|
+
"@eslint-react/shared": "5.30.0-beta.7",
|
|
44
|
+
"@eslint-react/var": "5.30.0-beta.7",
|
|
45
45
|
"@typescript-eslint/scope-manager": "^8.72.1",
|
|
46
46
|
"@typescript-eslint/types": "^8.72.1",
|
|
47
47
|
"@typescript-eslint/utils": "^8.72.1",
|