Skip to content

Commit 8361a35

Browse files
committed
Replace isArrayMapCall and isArrayFromCall with loose variants; update related rules
1 parent 7524c57 commit 8361a35

7 files changed

Lines changed: 24 additions & 25 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function shouldExcludeBasedOnHint(node: AST.TSESTreeFunction, hint: bigint): boo
7777
return true;
7878
}
7979

80-
if ((hint & ComponentDetectionHint.SkipArrayMapArgument) && AST.isArrayMapCall(node.parent)) {
80+
if ((hint & ComponentDetectionHint.SkipArrayMapArgument) && AST.isArrayMapCallLoose(node.parent)) {
8181
return true;
8282
}
8383

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
@@ -78,7 +78,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
7878
}
7979
// Case 2: Elements created by an array's .map() call
8080
default: {
81-
const call = AST.findParentNode(jsxElement, AST.isArrayMapCall);
81+
const call = AST.findParentNode(jsxElement, AST.isArrayMapCallLoose);
8282
const iter = AST.findParentNode(jsxElement, (n) => n === call || AST.isFunction(n));
8383
if (!AST.isFunction(iter)) return;
8484
const arg0 = call?.arguments[0];

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
128128
if (state.isWithinChildrenToArray) return;
129129
// Get the callback function from array methods like `map` or `from`
130130
const callback = match(node)
131-
.when(AST.isArrayMapCall, (n) => n.arguments[0])
132-
.when(AST.isArrayFromCall, (n) => n.arguments[1])
131+
.when(AST.isArrayMapCallLoose, (n) => n.arguments[0])
132+
.when(AST.isArrayFromCallLoose, (n) => n.arguments[1])
133133
.otherwise(() => null);
134134
if (!AST.isFunction(callback)) return;
135135
const body = callback.body;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
103103
}
104104

105105
function isArrayMethodCallback(node: TSESTree.Node) {
106-
if (node.parent?.type !== T.CallExpression) return false;
107-
if (!AST.isArrayMapCall(node.parent) && !AST.isArrayFromCall(node.parent)) return false;
106+
const parent = node.parent;
107+
if (parent?.type !== T.CallExpression) return false;
108+
if (!AST.isArrayMapCallLoose(parent) && !AST.isArrayFromCallLoose(parent)) return false;
108109
return AST.isOneOf([T.ArrowFunctionExpression, T.FunctionExpression])(AST.getUnderlyingExpression(node));
109110
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { TSESTree } from "@typescript-eslint/types";
2+
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
3+
4+
function isMethodCall(name: string, looseMatcher?: (name: string) => boolean) {
5+
// eslint-disable-next-line function/function-return-boolean
6+
return (node: TSESTree.Node): node is TSESTree.CallExpression => {
7+
if (node.type !== T.CallExpression) return false;
8+
if (node.callee.type !== T.MemberExpression) return false;
9+
if (node.callee.property.type !== T.Identifier) return false;
10+
return node.callee.property.name === name || looseMatcher != null && looseMatcher(name);
11+
};
12+
}
13+
14+
export const isArrayMapCallLoose = isMethodCall("map", (name) => name.endsWith("Map"));
15+
16+
export const isArrayFromCallLoose = isMethodCall("from", (name) => name.startsWith("from"));

packages/utilities/ast/src/array-method.ts

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

packages/utilities/ast/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export * from "./array-method";
1+
export * from "./array-method-call";
22
export * from "./class-id";
33
export * from "./equal";
44
export * from "./expression-base";

0 commit comments

Comments
 (0)