Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

prefer-for-of rule false positive #1931

Closed
plantain-00 opened this issue Dec 24, 2016 · 5 comments · Fixed by #1986
Closed

prefer-for-of rule false positive #1931

plantain-00 opened this issue Dec 24, 2016 · 5 comments · Fixed by #1986

Comments

@plantain-00
Copy link

Bug Report

  • TSLint version: 4.2.0
  • TypeScript version: 2.1.4
  • Running TSLint via: (please choose one) CLI

TypeScript code being linted

const a = [1, 2];
const b = [2, 3];
for (let i = 0; i < a.length; i++) {
    console.log(a[i] + b[i]);
}

with tslint.json configuration:

{
    "extends": "tslint:latest",
    "rules": {
        "forin": false,
        "interface-name": [
            false
        ],
        "max-line-length": [
            false
        ],
        "no-var-requires": false,
        "no-console": [
            false
        ],
        "no-string-literal": false,
        "no-reference": false,
        "ordered-imports": [
            false
        ],
        "object-literal-sort-keys": false,
        "variable-name": [
            true,
            "ban-keywords"
        ],
        "no-bitwise": false,
        "member-access": false,
        "arrow-parens": false,
        "array-type": [true, "array"],
        "max-classes-per-file": [false]
    }
}

Actual behavior

Expected a 'for-of' loop instead of a 'for' loop with this simple iteration

Expected behavior

No error.
Because the code cannot be transformed to for...of.

@plantain-00 plantain-00 changed the title [ prefer-for-of rule]some for loop code can not be transformed to for...of [ prefer-for-of]some for loop code can not be transformed to for...of Dec 24, 2016
@adidahiya adidahiya changed the title [ prefer-for-of]some for loop code can not be transformed to for...of prefer-for-of rule false positive Dec 25, 2016
@adidahiya
Copy link
Contributor

if you have support for Array.prototype.entries() in your environment, this could work for you:

for (const [i, elem] of a.entries()) {
    console.log(elem + b[i]);
}

but you're right, this looks like a bug in the rule implementation; it probably shouldn't flag that code as a failure.

@nchen63
Copy link
Contributor

nchen63 commented Jan 4, 2017

I can't reproduce this issue. Can anyone else?

@plantain-00
Copy link
Author

@nchen63
Another example closer to my real code:

declare const value1: any;
declare const value2: any;
for (let i = 0; i < (value1 as number[]).length; i++) {
    if ((value1 as number[])[i] === (value2 as number[])[i]) {
    }
}

@nchen63
Copy link
Contributor

nchen63 commented Jan 5, 2017

@plantain-00 That one reproduces the issue

@plantain-00
Copy link
Author

Sorry for initial wrong code, I have no clue how it happened then.
Thanks for fixing this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants