Skip to content

Commit 1e367bb

Browse files
authored
no-for-loop: Remove invalid fix for TypeScript (#2426)
1 parent 5c92a9a commit 1e367bb

File tree

3 files changed

+6
-34
lines changed

3 files changed

+6
-34
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"@babel/eslint-parser": "^7.24.5",
7575
"@eslint/eslintrc": "^3.1.0",
7676
"@lubien/fixture-beta-package": "^1.0.0-beta.1",
77-
"@typescript-eslint/parser": "^8.0.0-alpha.12",
77+
"@typescript-eslint/parser": "^8.1.0",
7878
"ava": "^6.1.3",
7979
"c8": "^9.1.0",
8080
"chalk": "^5.3.0",

rules/no-for-loop.js

+5-19
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ const getReferencesInChildScopes = (scope, name) =>
263263
/** @param {import('eslint').Rule.RuleContext} context */
264264
const 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
}

test/no-for-loop.mjs

-14
Original file line numberDiff line numberDiff line change
@@ -766,11 +766,6 @@ test.typescript({
766766
let selectionRange = allProviderRanges[i];
767767
}
768768
`,
769-
output: outdent`
770-
for (let [i, last]: [number, vscode.Position | vscode.Range] of positions.entries()) {
771-
let selectionRange = allProviderRanges[i];
772-
}
773-
`,
774769
errors: 1,
775770
},
776771
{
@@ -780,11 +775,6 @@ test.typescript({
780775
console.log(i);
781776
}
782777
`,
783-
output: outdent`
784-
for (const [i, last /* comment */]: [number, /* comment */ Position] of positions.entries()) {
785-
console.log(i);
786-
}
787-
`,
788778
errors: 1,
789779
},
790780
{
@@ -793,10 +783,6 @@ test.typescript({
793783
let last: vscode.Position | vscode.Range = positions[i];
794784
}
795785
`,
796-
output: outdent`
797-
for (let last: vscode.Position | vscode.Range of positions) {
798-
}
799-
`,
800786
errors: 1,
801787
},
802788
],

0 commit comments

Comments
 (0)