Skip to content

Commit

Permalink
no-for-loop: Remove invalid fix for TypeScript (#2426)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Aug 20, 2024
1 parent 5c92a9a commit 1e367bb
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"@babel/eslint-parser": "^7.24.5",
"@eslint/eslintrc": "^3.1.0",
"@lubien/fixture-beta-package": "^1.0.0-beta.1",
"@typescript-eslint/parser": "^8.0.0-alpha.12",
"@typescript-eslint/parser": "^8.1.0",
"ava": "^6.1.3",
"c8": "^9.1.0",
"chalk": "^5.3.0",
Expand Down
24 changes: 5 additions & 19 deletions rules/no-for-loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ const getReferencesInChildScopes = (scope, name) =>
/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
const {sourceCode} = context;
const {scopeManager, text: sourceCodeText} = sourceCode;
const {scopeManager} = sourceCode;

return {
ForStatement(node) {
Expand Down Expand Up @@ -339,12 +339,12 @@ const create = context => {
const elementIdentifierName = elementNode?.id.name;
const elementVariable = elementIdentifierName && resolveIdentifierName(elementIdentifierName, bodyScope);

const shouldFix = !someVariablesLeakOutOfTheLoop(node, [indexVariable, elementVariable].filter(Boolean), forScope);
const shouldFix = !someVariablesLeakOutOfTheLoop(node, [indexVariable, elementVariable].filter(Boolean), forScope)
&& !elementNode?.id.typeAnnotation;

if (shouldFix) {
problem.fix = function * (fixer) {
const shouldGenerateIndex = isIndexVariableUsedElsewhereInTheLoopBody(indexVariable, bodyScope, arrayIdentifierName);

const index = indexIdentifierName;
const element = elementIdentifierName
|| avoidCapture(singular(arrayIdentifierName) || defaultElementName, getScopes(bodyScope));
Expand All @@ -353,7 +353,6 @@ const create = context => {
let declarationElement = element;
let declarationType = 'const';
let removeDeclaration = true;
let typeAnnotation;

if (elementNode) {
if (elementNode.id.type === 'ObjectPattern' || elementNode.id.type === 'ArrayPattern') {
Expand All @@ -362,26 +361,13 @@ const create = context => {

if (removeDeclaration) {
declarationType = element.type === 'VariableDeclarator' ? elementNode.kind : elementNode.parent.kind;
if (elementNode.id.typeAnnotation && shouldGenerateIndex) {
declarationElement = sourceCodeText.slice(elementNode.id.range[0], elementNode.id.typeAnnotation.range[0]).trim();
typeAnnotation = sourceCode.getText(
elementNode.id.typeAnnotation,
-1, // Skip leading `:`
).trim();
} else {
declarationElement = sourceCode.getText(elementNode.id);
}
declarationElement = sourceCode.getText(elementNode.id);
}
}

const parts = [declarationType];
if (shouldGenerateIndex) {
parts.push(` [${index}, ${declarationElement}]`);
if (typeAnnotation) {
parts.push(`: [number, ${typeAnnotation}]`);
}

parts.push(` of ${array}.entries()`);
parts.push(` [${index}, ${declarationElement}] of ${array}.entries()`);
} else {
parts.push(` ${declarationElement} of ${array}`);
}
Expand Down
14 changes: 0 additions & 14 deletions test/no-for-loop.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -766,11 +766,6 @@ test.typescript({
let selectionRange = allProviderRanges[i];
}
`,
output: outdent`
for (let [i, last]: [number, vscode.Position | vscode.Range] of positions.entries()) {
let selectionRange = allProviderRanges[i];
}
`,
errors: 1,
},
{
Expand All @@ -780,11 +775,6 @@ test.typescript({
console.log(i);
}
`,
output: outdent`
for (const [i, last /* comment */]: [number, /* comment */ Position] of positions.entries()) {
console.log(i);
}
`,
errors: 1,
},
{
Expand All @@ -793,10 +783,6 @@ test.typescript({
let last: vscode.Position | vscode.Range = positions[i];
}
`,
output: outdent`
for (let last: vscode.Position | vscode.Range of positions) {
}
`,
errors: 1,
},
],
Expand Down

0 comments on commit 1e367bb

Please sign in to comment.