Skip to content

False positive on prefer-array-at on NodeListOf (no .at() method) #45

@johanrd

Description

@johanrd

The rule suggests .at(-1) for array-like DOM objects that don't have this method, causing runtime errors.

  // Before (works)                                                                                                                                                                                                                                                                                     
  const items = document.querySelectorAll('div');                                                                                                                                                                                                                                                       
  const last = items[items.length - 1];                                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                                                        
  // After applying fix (crashes)                                                                                                                                                                                                                                                                       
  const last = items.at(-1);                                                                                                                                                                                                                                                                            
  // Uncaught TypeError: document.querySelectorAll(...).at is not a function                                                                                                                                                                                                                            

NodeList, HTMLCollection, and similar DOM types are array-like but don't extend Array, so they lack .at().

Affected types:

  • NodeList / NodeListOf
  • HTMLCollection / HTMLCollectionOf
  • NamedNodeMap
  • DOMTokenList
  • FileList
  • TouchList
  • etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions