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

prefer-for-of rule recommended on a node list #2927

Closed
mattferderer opened this issue Jun 13, 2017 · 4 comments
Closed

prefer-for-of rule recommended on a node list #2927

mattferderer opened this issue Jun 13, 2017 · 4 comments

Comments

@mattferderer
Copy link

Bug Report

  • TSLint version: 5.4.3
  • TypeScript version: 2.3.4
  • Running TSLint via: VSCode eg2.tslint

TypeScript code being linted

const items = document.getElementsByClassName("items")
for(let i =0; i < items.length; i++) {
  
}

with tslint.json configuration:

{
    "defaultSeverity": "error",
    "extends": [
        "tslint:recommended"
    ],
    "jsRules": {},
    "rules": {
      "semicolon": [true, "never"],
      "no-unnecessary-type-assertion": true
    },
    "rulesDirectory": []
}

Actual behavior

Receiving the error "Expected a 'for-of' loop instead of a 'for' loop with this simple iteration (prefer-for-of)

Expected behavior

A for of loop to my knowledge shouldn't be used on a NodeList as it's not supported widely & is also not part of any standard. TypeScript will complain if you do this. The same with a forEach loop.

@Jessidhia
Copy link

It is specified as iterable<Node> (https://dom.spec.whatwg.org/#interface-nodelist). If TypeScript does not allow you to iterate on it, then it's TypeScript that needs fixing.

Also note that core-js can polyfill iterators for most DOM collections, including NodeList.

If you need to be "safe" and avoid iterating on them directly, you could also wrap the NodeList-resulting call with Array.from.

@mattferderer
Copy link
Author

mattferderer commented Jun 14, 2017

@Kovensky do you run a specific tsconfig that allows TypeScript to do this or are you suggesting to request it as an issue for them?

My comment about it not being supported was based off of the MDN Specifications that state "forEach on NodeList is NOT a part of any standard". Maybe I'm misunderstanding that though.

@NN---
Copy link
Contributor

NN--- commented Jul 8, 2017

@Kovensky It is iterable with ES6 support or with --downlevelIterators flag in TypeScript.
If you use ES5 mode , then it is not iterable using for..of.

I had to remove this rule due to this false positive.

imhoffd added a commit to ionic-team/tslint-ionic-rules that referenced this issue Jan 18, 2018
@brandyscarney says:

> here are the ones I ran into issues with in the framework:
>
> * `no-unnecessary-type-assertion` is throwing errors when we cast as an HTMLElement - which is necessary because otherwise it queries as an `Element` and throws errors that the properties don’t exist, this is a bug in TS: palantir/tslint#3540
> * `prefer-for-of` is throwing errors on node lists which is also a bug: palantir/tslint#2927
> * `no-conditional-assignment` is used in DOM controller
@JoshuaKGoldberg
Copy link
Contributor

Duplicate of #2021.

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

No branches or pull requests

4 participants