oxc/bad-match-all-arg

Require the g flag on a regular expression passed to matchAll()

matchAll() throws a TypeError when its regular expression doesn't have the g flag. The code parses and only fails when that line runs.

The rule checks a regular expression literal, new RegExp() and RegExp(), written in the call or kept in a variable. If the flags aren't a plain string, it can't tell and reports nothing. A string argument is fine, because matchAll() turns it into a global regular expression.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"oxc/bad-match-all-arg" = "error" # or "warn"

Examples#

example.js2 problems
const words = [...text.matchAll(/\w+/)];

const tag = new RegExp("#\\w+", "i");
for (const match of text.matchAll(tag)) {
  tags.push(match[0]);
}
example.jsNo problems
const words = [...text.matchAll(/\w+/g)];

const tag = new RegExp("#\\w+", "gi");
for (const match of text.matchAll(tag)) {
  tags.push(match[0]);
}

const commas = [...text.matchAll(",")];