Skip to content

Docs: [prefer-for-of] mention non-array loops that cannot be converted #8984

Open
@yohny

Description

@yohny

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.4.5&fileType=.ts&code=MYewdgzgLgBADgQwE4QKZJgXhmVB3GAEQHkBZABWTSQAoBKAbgFgAoUSWRFVAEy3iroAdFzQAxJCAC2AZShIAlmADmNAOQRpqGAA8pAGzUAaGGqiodUAPR7DjVu2gwkqCAFd9UAHIger-qK8QsqoUACi%2BqhSqGBQEABCAJ4AKgjKXgjR6prRAPpgvqhq9iysVlYwAAJQiXCuwIpwUAC0rvpK1nAuAGbozd0gSM0g3TCgUnD6CEoQMFAAFgiwAEao%2BiAE6yBwMBDzIB58q86okwjAvDDLiTADQyOsdzA0kbAK-AAMDDDvADwn7k8Pj8ECEkRUC2%2BCgA1NC6DAAN6sGAwcq7KBubrdVgAX1YZQqyzcsAWSxgPBA-gKsFQADcYmNpHAFJErqhgAg3GgYAAJZKkAAyAGEQPpIsAoApwMRRhSqSBYIt6TAANoyRJSZaioQKcxIJaDAC6j0Gz0csCQYBgIwBHm8hQg8KRLBRaOgmOxLDxpRYQA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Y6RAM0Wlq4D2fAV3RRe0IZHBgAviFlA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eFYDArugDTg2SrIARqwDavGlHQkATAAZp0yDwm0AJgkjiwAXV4BfEHqA&tokens=false

Repro Code

const parser = new DOMParser();
const parsed = parser.parseFromString('some xml', 'text/xml');
const resultNodes = parsed.getElementsByTagName('some_node');

// @typescript-eslint/prefer-for-of complains that below loop should be replaced by for-of
for (let i = 0; i < resultNodes.length; i++) {
  // stuff
}

// but that does not even compile because HTMLCollectionOf does not have [Symbol.iterator]
for (const rn of resultNodes) {
  // stuff
}

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
     "@typescript-eslint/prefer-for-of": "error"
  },
};

tsconfig

{
  "compilerOptions": {
    "strict": true,
    "lib": [
      "es2022",
      "dom"
    ]
  }
}

Expected Result

The rule suggesting the conversion to for-of loop should not trigger in this case

Actual Result

Rule triggers, suggesting uncompilable conversion

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuedocumentationDocumentation ("docs") that needs adding/updatingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions