optional-chaining
coupon?.code coupon.code This mutator replaces ?. with ., as if the value before it could never be
null or undefined: coupon?.code becomes coupon.code.
Tests catch these
mutants by running the code with null or undefined before the ?., such as
a cart without a coupon. The mutant then throws a TypeError.
Mootant replaces ?. with . before a property name and removes it before [
or (:
| Code | Mutant |
|---|---|
coupon |
coupon.code |
product |
product['sku'] |
onChange |
onChange(0) |
A chain gets one mutant per ?., each with the other ?. kept.
cart?.items?.[0]?.title gets three:
cart.items?.[0]?.titlecart?.items[0]?.titlecart?.items?.[0].title
Whether a ?. gets a mutant depends on type hints: what Mootant can tell about
a type from the same file, such as an annotation.
Type of the value before ?. |
Mutant |
|---|---|
May be null or undefined, such as Coupon | undefined |
Yes |
Can’t be, such as string[] |
Only with the nonNullable option |
| Imported from another file, or unknown | Yes |
Run the code with null or undefined before the ?., and check the result.
Take this function:
interface Cart {
coupon?: { code: string; percent: number }
}
export function discountPercent(cart: Cart): number {
return cart.coupon?.percent ?? 0
return cart.coupon.percent ?? 0
}A test with a coupon does not catch cart.coupon →
?.percentcart.coupon.percent, because both versions return the percent of the coupon.
Test a cart without a coupon:
it('gives no discount without a coupon', () => {
expect(discountPercent({})).toBe(0)
})With the mutant, discountPercent({}) throws a TypeError instead of
returning 0.
The other changes need these tests:
- A call, such as
onChange?.(0): run the code without the function. - A chain: a test for each
?.withnullorundefinedright before it, such as no cart, noitemsand an emptyitemslist.
If the value before ?. can never be null or undefined, no test can catch
the mutant: remove the ?., or ignore the mutant as
Configuration shows.
Turn the mutator off in the configuration:
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'optional-chaining': 'off',
},
})To ignore the mutants of this mutator on one line, put an ignore comment above it:
// mootant-ignore-next-line optional-chaining: browsers have no unref
setTimeout(releaseReservation, 15 * 60_000).unref?.()With strict, 'optional-chaining': 'on' turns nonNullable back off; see
Mutators.
false by default, true in strict. With it, Mootant replaces ?. whatever
the type hints say, since an annotation can be wrong at runtime: items
→ ?.lengthitems.length with items: string[].
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'optional-chaining': ['on', { nonNullable: 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 |
|---|---|
items?.length→items.length | Type hints say it can’t be null or undefinedequivalentnon-nullable |
page?.next→page.next in try { cursor = page?.next } catch {} | May keep a loop running forevertimeoutloop-advance |
match?.groups?.size→match?.groups.size | The regex shows the match has its named groupsequivalentnamed-groups |
cart?.coupon→cart.coupon in delete cart?.coupon | Mootant can’t switch a mutant on hereinvalidinvalid-target |
The nonNullable option brings back non-nullable, and the
timeouts setting brings back loop-advance;
strict turns on both. The other changes stay skipped.