regex-flag
/^save\d+$/i /^save\d+$/ This mutator removes a flag of a regular expression, as if it had been
forgotten: /^save\d+$/i becomes /^save\d+$/, which no longer matches
'SAVE10'.
Tests catch these mutants with text for which the flag changes the
result, such as 'SAVE10' for this i, or text with two matches for g.
Mootant changes the flags of regular expression literals and of RegExp calls,
such as new RegExp('^promo-', 'i'). A call counts only when its pattern and
any flags are written in it without ${}. In mootant list, a mutant shows the
flags alone, such as → gmm.
| Code | Mutant | Without the flag |
|---|---|---|
/^save\d+$/ |
/^save\d+$/ |
Letters match only in the case written |
/^SKU-\d+$/ |
/^SKU-\d+$/ |
^ and $ match only at the start and the end of the text |
/<note>.*<\/note>/ |
/<note>.*<\/note>/ |
. doesn’t match a line break |
name.replace(/\s+/ |
name.replace(/\s+/, '-') |
Only the first match is replaced |
text.match(/#\w+/ |
text.match(/#\w+/) |
Only the first match is returned, with its groups |
- Mootant removes
gonly from a regular expression written as the first argument of.replace()or.match(). One in a variable keeps itsg. - The
u,v,yanddflags stay as they are. - A flag that changes nothing, such as
iin/^\d{5}$/i, stays; see Skipped changes. - With the
additionsoption, on instrict, Mootant also addsiandg.
Test with text for which the flag changes the result. Take this function:
export function isCouponCode(code: string): boolean {
return /^save\d+$/i.test(code)
return /^save\d+$/.test(code)
}A test with the coupon code 'save10' does not catch /^save\d+$/ →
i/^save\d+$/, because both versions accept it. Test a coupon code in capitals:
it('accepts a coupon code in capitals', () => {
expect(isCouponCode('SAVE10')).toBe(true)
})With the mutant, isCouponCode('SAVE10') returns false.
Text that tells the other changes apart:
mremoved: the match on a middle line, such as'Mug\nSKU-123\nCup'.sremoved: a line break that.must match.gremoved: two or more matches, and an assertion on the whole result. With one match at most, as for/^#+/gin.match(), the whole result still differs, so assert withtoEqual.iadded: text in another letter case that the code must reject, such as'sale-10'for/^SALE-\d+$/.gadded: two or more matches, of which the code must replace only the first.
If the code lowercases the text before /^save\d+$/i, the i changes nothing,
and no test can catch its removal: remove the i, or ignore the mutant as
Configuration shows.
Turn the mutator off in the configuration:
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'regex-flag': 'off',
},
})With the mutator off, string-literal empties the
flags of a RegExp call instead: ' → i''' in new RegExp('^promo-', 'i').
To ignore the mutants of this mutator on one line, put an ignore comment above it:
export function isCouponCode(input: string): boolean {
let code = input.trim().toLowerCase()
// mootant-ignore-next-line regex-flag: the code is lowercased above
return /^save\d+$/i.test(code)
}With strict, 'regex-flag': 'on' turns additions back off; see
Mutators.
false by default, true in strict. With it, Mootant also adds i to a
regular expression where letter case can matter, and g to one in the first
argument of .replace(): /^SALE-\d+$/ → /^SALE-\d+$/i.
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'regex-flag': ['on', { additions: true }],
},
})Mootant leaves out these changes. npx mootant list src --skipped shows them
with the category and the rule; Skipped changes
explains the categories.
| Change | Reason |
|---|---|
/^\d{5}$/i→/^\d{5}$/ | The flag changes nothing the code reads, such as i with no letters or g where only the first match of a known string is readequivalentunused-flag |
/\s+$/g→/\s+$/ in .replace() | ^ or $ lets the pattern match only once, with or without gequivalentanchored-global |
/^sale-/i→/^sale-/ in prefix = /^sale-/i | May keep a loop running forevertimeoutloop-advance |
The timeouts setting, on in strict, brings
back loop-advance. The other changes stay skipped.
The pattern belongs to regex-pattern.