Skip to content

Commit 34fa486

Browse files
committed
refactor(ast): reorganize array method utilities
1 parent b0dbc87 commit 34fa486

File tree

8 files changed

+37
-36
lines changed

8 files changed

+37
-36
lines changed

packages/core/src/component/component-definition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function isValidComponentDefinition(context: RuleContext, node: AST.TSEST
3939
if (hint & ComponentDetectionHint.SkipClassProperty && isFunctionOfClassProperty(node.parent)) {
4040
return false;
4141
}
42-
if (hint & ComponentDetectionHint.SkipArrayMapArgument && AST.isArrayMapCallLoose(node.parent)) {
42+
if (hint & ComponentDetectionHint.SkipArrayMapArgument && AST.isArrayMapCall(node.parent)) {
4343
return false;
4444
}
4545
const boundaryNode = AST.findParentNode(

packages/plugins/eslint-plugin-react-x/src/rules/no-array-index-key.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ function getMapIndexParamName(context: RuleContext, node: TSESTree.CallExpressio
5151
return _;
5252
}
5353
const { name } = callee.property;
54-
if (!AST.ITERATOR_FUNCTION_INDEX_PARAM_POSITION.has(name)) {
54+
const indexPosition = AST.getArrayMethodIndexParamPosition(name);
55+
if (indexPosition === -1) {
5556
return _;
5657
}
5758
const callbackArg = node.arguments[isUsingReactChildren(context, node) ? 1 : 0];
@@ -62,14 +63,10 @@ function getMapIndexParamName(context: RuleContext, node: TSESTree.CallExpressio
6263
return _;
6364
}
6465
const { params } = callbackArg;
65-
const indexParamPosition = AST.ITERATOR_FUNCTION_INDEX_PARAM_POSITION.get(name);
66-
if (indexParamPosition == null) {
66+
if (params.length < indexPosition + 1) {
6767
return _;
6868
}
69-
if (params.length < indexParamPosition + 1) {
70-
return _;
71-
}
72-
const param = params.at(indexParamPosition);
69+
const param = params.at(indexPosition);
7370

7471
return param != null && "name" in param
7572
? param.name

packages/plugins/eslint-plugin-react-x/src/rules/no-duplicate-key.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
6969
break;
7070
}
7171
default: {
72-
const call = AST.findParentNode(jsxElement, AST.isArrayMapCallLoose);
72+
const call = AST.findParentNode(jsxElement, AST.isArrayMapCall);
7373
const iter = AST.findParentNode(jsxElement, (n) => n === call || AST.isFunction(n));
7474
if (!AST.isFunction(iter)) return;
7575
const arg0 = call?.arguments[0];

packages/plugins/eslint-plugin-react-x/src/rules/no-missing-key.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
115115
if (state.isWithinChildrenToArray) {
116116
return;
117117
}
118-
const isMapCall = AST.isArrayMapCallLoose(node);
118+
const isMapCall = AST.isArrayMapCall(node);
119119
if (!isMapCall && !isArrayFromCall(node)) {
120120
return;
121121
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { TSESTree } from "@typescript-eslint/types";
2+
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
3+
4+
const indexParamPosition = new Map<string, number>([
5+
["every", 1],
6+
["filter", 1],
7+
["find", 1],
8+
["findIndex", 1],
9+
["findLast", 1],
10+
["findLastIndex", 1],
11+
["flatMap", 1],
12+
["forEach", 1],
13+
["map", 1],
14+
["reduce", 2],
15+
["reduceRight", 2],
16+
["some", 1],
17+
]);
18+
19+
export function getArrayMethodIndexParamPosition(methodName: string) {
20+
return indexParamPosition.get(methodName) ?? -1;
21+
}
22+
23+
export function isArrayMapCall(node: TSESTree.Node): node is TSESTree.CallExpression {
24+
if (node.type !== T.CallExpression) return false;
25+
if (node.callee.type !== T.MemberExpression) return false;
26+
if (node.callee.property.type !== T.Identifier) return false;
27+
const { name } = node.callee.property;
28+
return name === "map" || name.endsWith("Map");
29+
}

packages/utilities/ast/src/constants.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

packages/utilities/ast/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export * from "./constants";
1+
export * from "./array-methods";
22
export * from "./find-parent-node";
33
export * from "./function-init-path";
44
export * from "./get-class-identifier";
@@ -11,7 +11,6 @@ export * from "./get-nested-return-statements";
1111
export * from "./get-property-name";
1212
export * from "./get-top-level-identifier";
1313
export * from "./is";
14-
export * from "./is-array-map-call";
1514
export * from "./is-empty-function";
1615
export * from "./is-line-break";
1716
export * from "./is-literal";

packages/utilities/ast/src/is-array-map-call.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)