A shared regular expression with the g or y flag is used with test or exec, which resume where the last match ended
What it finds
A regular expression with the g or y flag remembers in lastIndex where its last match ended, and test and exec start the next search from there. Shared between calls, as a constant, the same expression gives true and then false for the same string.
Reported are test calls on a const initialized with a regular expression literal with the g or y flag, declared at the top level of the file or of another function than the call. Loops that call exec until it returns null use lastIndex on purpose and are left alone.
From the rule's source, stateful_global_regex_test.rs.
const WORD = /\w+/g;export function hasWord(s: string) { return WORD.test(s);tx0 reports: this regular expression keeps lastIndex between calls} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/ts:stateful-global-regex-test" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/ts:stateful-global-regex-test] reason="..."tx0 explain tx0/ts:stateful-global-regex-test shows the same in the terminal. Found a false positive? Report it.