@@ -263,7 +263,7 @@ const getReferencesInChildScopes = (scope, name) =>
263263/** @param {import('eslint').Rule.RuleContext } context */
264264const create = context => {
265265 const { sourceCode} = context ;
266- const { scopeManager, text : sourceCodeText } = sourceCode ;
266+ const { scopeManager} = sourceCode ;
267267
268268 return {
269269 ForStatement ( node ) {
@@ -339,12 +339,12 @@ const create = context => {
339339 const elementIdentifierName = elementNode ?. id . name ;
340340 const elementVariable = elementIdentifierName && resolveIdentifierName ( elementIdentifierName , bodyScope ) ;
341341
342- const shouldFix = ! someVariablesLeakOutOfTheLoop ( node , [ indexVariable , elementVariable ] . filter ( Boolean ) , forScope ) ;
342+ const shouldFix = ! someVariablesLeakOutOfTheLoop ( node , [ indexVariable , elementVariable ] . filter ( Boolean ) , forScope )
343+ && ! elementNode ?. id . typeAnnotation ;
343344
344345 if ( shouldFix ) {
345346 problem . fix = function * ( fixer ) {
346347 const shouldGenerateIndex = isIndexVariableUsedElsewhereInTheLoopBody ( indexVariable , bodyScope , arrayIdentifierName ) ;
347-
348348 const index = indexIdentifierName ;
349349 const element = elementIdentifierName
350350 || avoidCapture ( singular ( arrayIdentifierName ) || defaultElementName , getScopes ( bodyScope ) ) ;
@@ -353,7 +353,6 @@ const create = context => {
353353 let declarationElement = element ;
354354 let declarationType = 'const' ;
355355 let removeDeclaration = true ;
356- let typeAnnotation ;
357356
358357 if ( elementNode ) {
359358 if ( elementNode . id . type === 'ObjectPattern' || elementNode . id . type === 'ArrayPattern' ) {
@@ -362,26 +361,13 @@ const create = context => {
362361
363362 if ( removeDeclaration ) {
364363 declarationType = element . type === 'VariableDeclarator' ? elementNode . kind : elementNode . parent . kind ;
365- if ( elementNode . id . typeAnnotation && shouldGenerateIndex ) {
366- declarationElement = sourceCodeText . slice ( elementNode . id . range [ 0 ] , elementNode . id . typeAnnotation . range [ 0 ] ) . trim ( ) ;
367- typeAnnotation = sourceCode . getText (
368- elementNode . id . typeAnnotation ,
369- - 1 , // Skip leading `:`
370- ) . trim ( ) ;
371- } else {
372- declarationElement = sourceCode . getText ( elementNode . id ) ;
373- }
364+ declarationElement = sourceCode . getText ( elementNode . id ) ;
374365 }
375366 }
376367
377368 const parts = [ declarationType ] ;
378369 if ( shouldGenerateIndex ) {
379- parts . push ( ` [${ index } , ${ declarationElement } ]` ) ;
380- if ( typeAnnotation ) {
381- parts . push ( `: [number, ${ typeAnnotation } ]` ) ;
382- }
383-
384- parts . push ( ` of ${ array } .entries()` ) ;
370+ parts . push ( ` [${ index } , ${ declarationElement } ] of ${ array } .entries()` ) ;
385371 } else {
386372 parts . push ( ` ${ declarationElement } of ${ array } ` ) ;
387373 }
0 commit comments