Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/src/component/component-collector-legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function isThisSetState(node: TSESTree.CallExpression) {
const { callee } = node;
return (
callee.type === T.MemberExpression
&& AST.isThisExpression(callee.object)
&& AST.isThisExpressionLoose(callee.object)
&& callee.property.type === T.Identifier
&& callee.property.name === "setState"
);
Expand All @@ -91,6 +91,6 @@ export function isThisSetState(node: TSESTree.CallExpression) {
export function isAssignmentToThisState(node: TSESTree.AssignmentExpression) {
const { left } = node;
return left.type === T.MemberExpression
&& AST.isThisExpression(left.object)
&& AST.isThisExpressionLoose(left.object)
&& AST.getPropertyName(left.property) === "state";
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
// Main logic for detecting `this.state` access
MemberExpression(node) {
// Check for `this` expressions
if (!AST.isThisExpression(node.object)) {
if (!AST.isThisExpressionLoose(node.object)) {
return;
}
// Ensure we are inside a React class component
Expand Down Expand Up @@ -151,7 +151,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
return;
}
// Check for destructuring from `this`
if (node.init == null || !AST.isThisExpression(node.init) || node.id.type !== T.ObjectPattern) {
if (node.init == null || !AST.isThisExpressionLoose(node.init) || node.id.type !== T.ObjectPattern) {
return;
}
// Check if `state` is one of the destructured properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ export type MessageID = CamelCase<typeof RULE_NAME>;

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

const arrayIndexParamPosition = new Map<string, number>([
["every", 1],
["filter", 1],
["find", 1],
["findIndex", 1],
["findLast", 1],
["findLastIndex", 1],
["flatMap", 1],
["forEach", 1],
["map", 1],
["reduce", 2],
["reduceRight", 2],
["some", 1],
]);

export function getArrayIndexParamPosition(methodName: string) {
return arrayIndexParamPosition.get(methodName) ?? -1;
}

// Checks if a method name is 'forEach' or 'map'
function isReactChildrenMethod(name: string): name is typeof REACT_CHILDREN_METHOD[number] {
return REACT_CHILDREN_METHOD.includes(name as never);
Expand Down Expand Up @@ -56,7 +75,7 @@ function getMapIndexParamName(context: RuleContext, node: TSESTree.CallExpressio
}
const { name } = callee.property;
// Determines the position of the index parameter for array methods like 'map', 'forEach', etc
const indexPosition = AST.getArrayMethodCallbackIndexParamPosition(name);
const indexPosition = getArrayIndexParamPosition(name);
if (indexPosition === -1) {
return unit;
}
Expand Down
Loading
Loading