logical-operator
inStock && hasAddress hasAddress This mutator drops one side of &&, || or ??, as if a check or a default
were missing: inStock && hasAddress becomes hasAddress. It also swaps ||
and ?? and turns a whole condition into true or false.
Tests catch a drop with a case where the dropped side decides the result, such as an item that is out of stock.
&&, || and ?? become one of their two sides:
| Code | Mutant |
|---|---|
|
hasAddress |
inStock |
inStock |
|
'Untitled' |
title |
title |
|
0 |
discount |
discount |
In a chain such as isSignedIn && hasItems && isPaid, Mootant drops each part
once.
Mootant drops a null or undefined default, as in gift ?? null, only with
the nullishNormalization option, on in strict.
|| becomes ??, and ?? becomes ||, as if the code used the wrong one:
|| takes the default for every falsy value, such as '' or 0, and ?? only
for null and undefined.
| Code | Mutant |
|---|---|
name |
name ?? 'Guest' |
quantity |
quantity || 1 |
Mootant swaps only where the swap may change the result. It decides by the type of the left side, as far as the code shows it, and by the default:
| Left side | Default | recommended |
'always' |
|---|---|---|---|
number |
0 |
Skipped | Swap |
string, bigint or boolean |
'', 0n or false |
Skipped | Skipped |
string, number, bigint or boolean |
Any other | Swap | Swap |
| Object, array or function | Any | Skipped | Skipped |
| Unknown type | A number, true, or a string or BigInt other than '' and 0n |
Swap | Swap |
| Unknown type | Object, array or function | Skipped | Skipped |
| Unknown type | Any other, such as '', false, null or rates.standard |
Skipped | Swap |
|| becomes ?? only where its left side may be null or undefined and its
result is a value, not a condition or a boolean. Where it doesn’t, Mootant
drops the right side instead: title → || 'Untitled'title for a string
title.
A swap replaces the drop of one side, since a test that catches the swap also
catches that drop: name || 'Guest' gets no name mutant, and quantity ?? 1
no 1.
A whole && becomes false, and a whole || becomes true, as if the
condition never or always held:
| Code | Mutant |
|---|---|
|
false |
|
true |
Mootant makes this change once, for the outermost && or ||, in these places:
| Place | Example |
|---|---|
A condition, such as if (…), ! or a filter callback |
if (isMember || hasCoupon) |
Both sides are booleans, or a boolean is expected |
let free: boolean = isMember || hasCoupon |
| Another returned value, unless a side is a literal or known not to be a boolean | return user && user.name in plain JavaScript |
Where a side is a comparison, as in quantity > 0 && inStock, some drops and
constants are left to comparison-operator; see
Skipped changes.
Test a case where the result changes without the dropped side. Take this function:
export function canShip(inStock: boolean, hasAddress: boolean): boolean {
return inStock && hasAddress
return hasAddress
}A test that ships an item in stock to a known address does not catch
→ inStock && hasAddresshasAddress: both versions return true. Ship an
item that is out of stock:
it('does not ship an item that is out of stock', () => {
expect(canShip(false, true)).toBe(false)
})With the mutant, canShip(false, true) returns true.
Cases that tell the other changes apart:
inStock→&& hasAddressinStock: an item in stock without an address.discount→?? 0discount: no discount; →0: a discount other than 0.name→||'Guest'name ?? 'Guest': an empty name.quantity→??1quantity || 1: a quantity of 0.- A constant
trueorfalse: a case where the condition has the other value.
Turn the mutator off in the configuration:
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'logical-operator': 'off',
},
})To ignore the mutants of this mutator on one line, put an ignore comment above it:
// mootant-ignore-next-line logical-operator: retry delay
let delay = options.retryDelay ?? 1000Your setting for this mutator replaces the one from strict: with strict,
['on', { nullishSwap: 'always' }] turns nullishNormalization back off.
'distinguishing' by default, 'always' in strict. The 'always' column of
the table in Swap || and ?? shows the swaps it adds. For a
number with the default 0, such as discount → ?? 0discount || 0, only
NaN and -0 tell the swap apart from the code.
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'logical-operator': ['on', { nullishSwap: 'always' }],
},
})false by default, true in strict. With true, Mootant also drops a
null, undefined or void 0 default of ??: gift → ?? nullgift gives
undefined where the code gives null.
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'logical-operator': ['on', { nullishNormalization: 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 |
|---|---|
quantity > 0 && inStock→inStock | Another mutant inside gives this result on some inputssubsumedR2 |
name || 'Guest'→name | This mutator swaps || and ?? insteadsubsumedswap-subsumes-drop |
cart && cart.items→cart.items for cart: Cart | Type of the left side decides the resultequivalenttyped-constant |
name ?? 'Untitled'→name for name: string | Type hints say the left side is never null or undefinedequivalentnever-nullish |
note || ''→note for note: string | 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 |
discount ?? 0→discount || 0 for a number | ?? and || likely give the same resultnoisenullish-swap |
coupon?.active ?? false→coupon?.active in if (…) | Falsy default where only truth countsequivalentfalsy-default |
typeof sku === 'string' && sku.length > 0→sku.length > 0 | A side is always true or always falseequivalentfixed-operand |
gift ?? null→gift | Only turns null into undefined, or backnoisenullish-normalization |
finished || failed→failed in done = finished || failed | May keep a loop running forevertimeoutloop-advance |
hasMore || retrying→true in while (…) | Makes a loop condition always truetimeoutloop-running |
showBadge ?? true→true in if (…) | Same change as a condition or boolean-literal mutantownedO15 |
price?.toFixed(2) ?? 'free'→price?.toFixed(2) || 'free' | Left side is falsy only when null or undefinedequivalentnever-falsy |
a.featured && !b.featured→false in a sort callback | method-call removes the sort() call insteadownedO12 |
isEmpty || failed→true in if (…) break | Ends the loop at once, as condition doesownedO13 |
isAdmin || isAdmin→isAdmin | Same code as another mutantduplicateduplicate |
Changes that come back:
loop-advanceandloop-running, with thetimeoutssetting.nullish-normalizationand part ofnullish-swap, with the options.R2, when the mutant inside doesn’t run, for example with comparison-operator off.
strict turns on the setting and the options. The other changes stay skipped.
Logical assignments, such as note ||= '', belong to
assignment-operator.