Skip to content

Commit

Permalink
no-for-loop: Improve output when using the TypeScript parser (#883)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Oct 21, 2020
1 parent ce6500a commit 52edd3b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
7 changes: 5 additions & 2 deletions rules/no-for-loop.js
Expand Up @@ -371,8 +371,11 @@ const create = context => {
if (removeDeclaration) {
declarationType = element.type === 'VariableDeclarator' ? elementNode.kind : elementNode.parent.kind;
if (elementNode.id.typeAnnotation && shouldGenerateIndex) {
declarationElement = sourceCode.text.slice(elementNode.id.range[0], elementNode.id.typeAnnotation.range[0]);
typeAnnotation = sourceCode.getText(elementNode.id.typeAnnotation, -1).trim();
declarationElement = sourceCode.text.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);
}
Expand Down
14 changes: 14 additions & 0 deletions test/no-for-loop.js
Expand Up @@ -743,6 +743,20 @@ runTest.typescript({
`,
errors: 1
},
{
code: outdent`
for (let i = 0; i < positions.length; i++) {
const last /* comment */ : /* comment */ Position = positions[i];
console.log(i);
}
`,
output: outdent`
for (const [i, last /* comment */]: [number, /* comment */ Position] of positions.entries()) {
console.log(i);
}
`,
errors: 1
},
{
code: outdent`
for (let i = 0; i < positions.length; i++) {
Expand Down

0 comments on commit 52edd3b

Please sign in to comment.