Skip to content

Commit c1a0474

Browse files
authored
Refactor: Update isThisExpression to isThisExpressionLoose in mul… (#1401)
1 parent 842df5e commit c1a0474

10 files changed

Lines changed: 312 additions & 39 deletions

File tree

packages/core/src/component/component-collector-legacy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export function isThisSetState(node: TSESTree.CallExpression) {
7777
const { callee } = node;
7878
return (
7979
callee.type === T.MemberExpression
80-
&& AST.isThisExpression(callee.object)
80+
&& AST.isThisExpressionLoose(callee.object)
8181
&& callee.property.type === T.Identifier
8282
&& callee.property.name === "setState"
8383
);
@@ -91,6 +91,6 @@ export function isThisSetState(node: TSESTree.CallExpression) {
9191
export function isAssignmentToThisState(node: TSESTree.AssignmentExpression) {
9292
const { left } = node;
9393
return left.type === T.MemberExpression
94-
&& AST.isThisExpression(left.object)
94+
&& AST.isThisExpressionLoose(left.object)
9595
&& AST.getPropertyName(left.property) === "state";
9696
}

packages/plugins/eslint-plugin-react-x/src/rules/no-access-state-in-setstate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
9393
// Main logic for detecting `this.state` access
9494
MemberExpression(node) {
9595
// Check for `this` expressions
96-
if (!AST.isThisExpression(node.object)) {
96+
if (!AST.isThisExpressionLoose(node.object)) {
9797
return;
9898
}
9999
// Ensure we are inside a React class component
@@ -151,7 +151,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
151151
return;
152152
}
153153
// Check for destructuring from `this`
154-
if (node.init == null || !AST.isThisExpression(node.init) || node.id.type !== T.ObjectPattern) {
154+
if (node.init == null || !AST.isThisExpressionLoose(node.init) || node.id.type !== T.ObjectPattern) {
155155
return;
156156
}
157157
// Check if `state` is one of the destructured properties

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,25 @@ export type MessageID = CamelCase<typeof RULE_NAME>;
1919

2020
const REACT_CHILDREN_METHOD = ["forEach", "map"] as const;
2121

22+
const arrayIndexParamPosition = new Map<string, number>([
23+
["every", 1],
24+
["filter", 1],
25+
["find", 1],
26+
["findIndex", 1],
27+
["findLast", 1],
28+
["findLastIndex", 1],
29+
["flatMap", 1],
30+
["forEach", 1],
31+
["map", 1],
32+
["reduce", 2],
33+
["reduceRight", 2],
34+
["some", 1],
35+
]);
36+
37+
export function getArrayIndexParamPosition(methodName: string) {
38+
return arrayIndexParamPosition.get(methodName) ?? -1;
39+
}
40+
2241
// Checks if a method name is 'forEach' or 'map'
2342
function isReactChildrenMethod(name: string): name is typeof REACT_CHILDREN_METHOD[number] {
2443
return REACT_CHILDREN_METHOD.includes(name as never);
@@ -56,7 +75,7 @@ function getMapIndexParamName(context: RuleContext, node: TSESTree.CallExpressio
5675
}
5776
const { name } = callee.property;
5877
// Determines the position of the index parameter for array methods like 'map', 'forEach', etc
59-
const indexPosition = AST.getArrayMethodCallbackIndexParamPosition(name);
78+
const indexPosition = getArrayIndexParamPosition(name);
6079
if (indexPosition === -1) {
6180
return unit;
6281
}

0 commit comments

Comments
 (0)