From 7ca6365322007fd39175dae249e40cadabd432ea Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Fri, 3 Jan 2025 14:34:58 +0000 Subject: [PATCH] chore(eslint-plugin): [no-for-in-array] use `getConstraintInfo` Uses `getConstraintInfo` for determining if the right hand side of the `for..in` is constrained or not. --- packages/eslint-plugin/src/rules/no-for-in-array.ts | 12 ++++++++---- .../tests/rules/no-for-in-array.test.ts | 7 +++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-for-in-array.ts b/packages/eslint-plugin/src/rules/no-for-in-array.ts index ec8ebe9125fb..26c6a839456f 100644 --- a/packages/eslint-plugin/src/rules/no-for-in-array.ts +++ b/packages/eslint-plugin/src/rules/no-for-in-array.ts @@ -2,7 +2,7 @@ import * as ts from 'typescript'; import { createRule, - getConstrainedTypeAtLocation, + getConstraintInfo, getParserServices, isTypeArrayTypeOrUnionOfArrayTypes, } from '../util'; @@ -30,11 +30,15 @@ export default createRule({ const services = getParserServices(context); const checker = services.program.getTypeChecker(); - const type = getConstrainedTypeAtLocation(services, node.right); + const { constraintType } = getConstraintInfo( + checker, + services.getTypeAtLocation(node.right), + ); if ( - isTypeArrayTypeOrUnionOfArrayTypes(type, checker) || - (type.flags & ts.TypeFlags.StringLike) !== 0 + constraintType != null && + (isTypeArrayTypeOrUnionOfArrayTypes(constraintType, checker) || + (constraintType.flags & ts.TypeFlags.StringLike) !== 0) ) { context.report({ loc: getForStatementHeadLoc(context.sourceCode, node), diff --git a/packages/eslint-plugin/tests/rules/no-for-in-array.test.ts b/packages/eslint-plugin/tests/rules/no-for-in-array.test.ts index 47b3d1ab3ea2..78c1a6c0439a 100644 --- a/packages/eslint-plugin/tests/rules/no-for-in-array.test.ts +++ b/packages/eslint-plugin/tests/rules/no-for-in-array.test.ts @@ -23,6 +23,13 @@ for (const x of [3, 4, 5]) { ` for (const x in { a: 1, b: 2, c: 3 }) { console.log(x); +} + `, + ` +function f(input: T) { + for (const x in input) { + console.log(x); + } } `, ],