sindresorhus/eslint-plugin-unicorn

Rule proposal: Prefer `String#matchAll()`

Closed

#574 opened on Mar 3, 2020

 (6 comments) (5 reactions) (0 assignees)JavaScript (468 forks)user submission
help wantednew rule

Repository metrics

Stars
 (5,022 stars)
PR merge metrics
 (Avg merge 1d 16h) (399 merged PRs in 30d)

Description

Not offering a PR here, but just thought such a rule could possibly fit your project...

Namely, preferring String.prototype.matchAll to while loop exec assignments.

As per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll#Examples , one can replace this:

let match;
while ((match = regexp.exec(str)) !== null) {
}

..with the likes of:

for (const match of str.matchAll(regexp)) {
}

I find it quite a bit more elegant and semantic, and it allows avoiding defining a variable in the parent block.

If not, no worries, just thought I'd see if it might pique the interest of others.

Contributor guide