@@ -263,7 +263,7 @@ const getReferencesInChildScopes = (scope, name) =>
263
263
/** @param {import('eslint').Rule.RuleContext } context */
264
264
const create = context => {
265
265
const { sourceCode} = context ;
266
- const { scopeManager, text : sourceCodeText } = sourceCode ;
266
+ const { scopeManager} = sourceCode ;
267
267
268
268
return {
269
269
ForStatement ( node ) {
@@ -339,12 +339,12 @@ const create = context => {
339
339
const elementIdentifierName = elementNode ?. id . name ;
340
340
const elementVariable = elementIdentifierName && resolveIdentifierName ( elementIdentifierName , bodyScope ) ;
341
341
342
- const shouldFix = ! someVariablesLeakOutOfTheLoop ( node , [ indexVariable , elementVariable ] . filter ( Boolean ) , forScope ) ;
342
+ const shouldFix = ! someVariablesLeakOutOfTheLoop ( node , [ indexVariable , elementVariable ] . filter ( Boolean ) , forScope )
343
+ && ! elementNode ?. id . typeAnnotation ;
343
344
344
345
if ( shouldFix ) {
345
346
problem . fix = function * ( fixer ) {
346
347
const shouldGenerateIndex = isIndexVariableUsedElsewhereInTheLoopBody ( indexVariable , bodyScope , arrayIdentifierName ) ;
347
-
348
348
const index = indexIdentifierName ;
349
349
const element = elementIdentifierName
350
350
|| avoidCapture ( singular ( arrayIdentifierName ) || defaultElementName , getScopes ( bodyScope ) ) ;
@@ -353,7 +353,6 @@ const create = context => {
353
353
let declarationElement = element ;
354
354
let declarationType = 'const' ;
355
355
let removeDeclaration = true ;
356
- let typeAnnotation ;
357
356
358
357
if ( elementNode ) {
359
358
if ( elementNode . id . type === 'ObjectPattern' || elementNode . id . type === 'ArrayPattern' ) {
@@ -362,26 +361,13 @@ const create = context => {
362
361
363
362
if ( removeDeclaration ) {
364
363
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 ) ;
374
365
}
375
366
}
376
367
377
368
const parts = [ declarationType ] ;
378
369
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()` ) ;
385
371
} else {
386
372
parts . push ( ` ${ declarationElement } of ${ array } ` ) ;
387
373
}
0 commit comments