question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`prefer-spread` incompatible with DOM APIs (and non-iterables)

See original GitHub issue

Quoting https://github.com/sindresorhus/eslint-plugin-unicorn/issues/120#issuecomment-615220593:

unicorn/prefer-spread is enabled by default as an error when extending from unicorn:recommended. I found a quite annoying edge-case, due to:

Array.from works on Array-likes while ... only works on iterables.

It is true that:

Array-like is IMHO just a legacy quirk. Iterables is a better way to define things that can be iterated.

However, all DOM APIs (to my knowledge) don’t implement the [Symbol.iterator] protocol.

// This works
for (const element of Array.from(document.querySelectorAll('.foo'))) {
  console.log(element);
}

// This doesn't
for (const element of [...document.querySelectorAll('.foo')]) {
  console.log(element);
}

I’ve enabled ESLint auto-fixing in my editor and more often than I’d like to admit I trolled myself with this pretty hard.

I’ll probably just demote the rule to be a warning, because I don’t want to monkey-patch the [Symbol.iterator] protocol into the DOM API, even though that’d be even better, since then you could just do:

for (const element of document.querySelectorAll('.foo')) {
  console.log(element);
}

I would recommend to make this rule a warning in unicorn:recommended as well, to avoid this foot-gun.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
sindresorhuscommented, Jan 11, 2021

[...document.body.children] works in all modern browsers now, including the latest version of Edge.

https://stackoverflow.com/a/31574921/64949


Closing as this plugin only cares about modern browsers. If you need support for older browser you can simply disable the rule.

PR welcome if anyone cares enough to add a note to the rule docs about older browsers.

2reactions
fiskercommented, Apr 17, 2020

We can keep this open, because people still use something like HTMLCollection which can’t fix

Read more comments on GitHub >

github_iconTop Results From Across the Web

Document Object Model (DOM) - Web APIs - MDN Web Docs
The Document Object Model (DOM) connects web pages to scripts or programming languages by representing the structure of a document—such as ...
Read more >
What is the Document Object Model? - W3C
The Document Object Model (DOM) is an application programming interface (API) for HTML and XML documents. It defines the logical structure of documents...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found