assignment-operator
total += shipping total -= shipping This mutator swaps the operator of a compound assignment: total += shipping
becomes total -= shipping. In &&=, ||= and ??=, it changes when the
assignment happens.
Tests catch these mutants by checking the value after the assignment, with inputs for which the two versions differ, such as a shipping cost other than 0.
Each operator becomes one other operator:
| Code | Mutant |
|---|---|
total |
total -= shipping |
total |
total += discount |
price |
price /= quantity |
amount |
amount *= 100 |
cents |
cents *= 100 |
rate |
rate *= years |
bits |
bits >>= 2 |
bits |
bits <<= 2 |
hash |
hash >>= 0 |
options |
options |= allowed |
options |
options &= GIFT_WRAP |
options |
options |= EXPRESS |
Mootant swaps += only when one side is known to be a number, such as a
number parameter, a numeric literal or a variable that starts at 0, and the
other side is not known to be anything else. Any other += may join strings,
and Mootant skips its swap.
&&=, ||= and ??= can get up to two of these mutants each:
| Change | Code | Mutant |
|---|---|---|
| Always assign | note |
note = 'Thank you!' |
| Never assign | coupon |
coupon |
Swap ??= and ||= |
shipping |
shipping ||= 5 |
??= and ||= differ only for a falsy target other than null and
undefined, such as 0 or ''. Mootant swaps them for these targets and
defaults:
| Target | Default |
|---|---|
| A string, number, BigInt or boolean | Not the falsy value of its type, such as 5 for a number |
| Of unknown type, as in plain JavaScript | A number, or a string, BigInt or boolean other than '', 0n and false |
||= becomes ??= only where the target may also be null or undefined. The
nullishSwap option adds two more cases.
Mootant also skips changes to an assignment that moves a loop, such as
packed += perBox in while (packed < items). See
Skipped changes.
Check the value after the assignment with inputs for which the two versions differ. Take this function:
export function giftNote(note: string | undefined): string {
note ||= 'Enjoy your gift!'
note ??= 'Enjoy your gift!'
return note
}Tests that pass undefined or a non-empty note do not catch
note → ||= 'Enjoy your gift!'note ??= 'Enjoy your gift!': both versions
replace undefined and keep a non-empty note. Pass an empty note:
it('replaces an empty note', () => {
expect(giftNote('')).toBe('Enjoy your gift!')
})With the mutant, giftNote('') returns an empty string instead of
'Enjoy your gift!'. A falsy target other than null, undefined and the
default, such as 0 or '', also catches → ??=||=.
Values that tell the other changes apart:
- Arithmetic operators: a target such as 10 and a right side such as 3.
- Shifts: a target such as -8 and a shift such as 1.
&=,|=and^=: a target that shares a bit with the right side but differs from it, such as 6 for 2.- Always assign: a target that the original keeps, but
=would change. - Never assign: a target that the original changes.
Turn the mutator off in the configuration:
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'assignment-operator': 'off',
},
})To ignore the mutants of this mutator on one line, put an ignore comment above it:
// mootant-ignore-next-line assignment-operator: timing only
stats.checkoutMs += Date.now() - startedAtWhen this mutator is off, or its mutants on a line are ignored,
statement-removal removes an assignment such as
total += shipping that no other mutant changes.
['on', options] merges the options over the
defaults of the mutator, not over the options
of the preset: with strict, 'assignment-operator': 'on' turns nullishSwap
back to 'distinguishing'.
'distinguishing' by default, 'always' in strict. With 'always', Mootant
also swaps ??= and ||= for these targets:
- A number with the default
0:fee→??=0fee ||= 0, which differ only forNaNand-0. - A target of unknown type, unless the default is an object, an array or a function.
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'assignment-operator': ['on', { nullishSwap: 'always' }],
},
})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 |
|---|---|
name += size→name -= size | May join strings, where -= gives NaNnoisenon-numeric-plus |
packed += perBox→packed -= perBox | May keep a loop running forevertimeoutloop-advance |
shipping ??= 5→shipping = 5 | This mutator swaps ??= and ||= insteadsubsumedswap-subsumes-drop |
fee ??= 0→fee ||= 0 for a number | ??= and ||= likely give the same resultnoisenullish-swap |
options ??= {}→options | Type hints say the target is never null or undefinedequivalentnever-nullish |
items ||= []→items | Type hints make the target always truthyequivalenttyped-constant |
coupon ||= ''→coupon | Default is the type’s only falsy value; a number needs a lower limit, such as 0 for a length, to rule out NaNequivalentcanonical-falsy-default |
total += 0→total -= 0 | 0 on the right of a number gives the same resultequivalentnumeric-identity |
amount *= -1→amount /= -1 | 1 or -1 on the right gives the same resultequivalentidentity-operand |
The timeouts setting brings back
loop-advance, and nullishSwap set to 'always' brings back
some nullish-swap changes, such as fee → ??= 0fee ||= 0; strict does
both. The other changes stay skipped.
A plain = gets no mutant from this mutator: in total = total + price,
arithmetic-operator changes total + price.
update-operator changes count++ and count--.