array-element
[subtotal, shipping, tax] [subtotal, tax] This mutator removes one element from an array literal, as if the code left out
a value: [subtotal, shipping, tax] becomes [subtotal, tax]. An empty array
gets an undefined element instead: [] becomes [undefined].
Tests catch these mutants when they compare the whole array, with inputs where each element makes a difference, such as a cart that already holds items.
Each element gets its own mutant, a spread such as ...items too:
| Code | Mutant |
|---|---|
[subtotal, |
[subtotal, tax] |
[ |
[gift] |
[...items |
[...items] |
[ |
[] |
In recommended, Mootant doesn’t remove an element that has a mutant inside.
For example, in [subtotal * rate, tax], it removes tax but not
subtotal * rate, because arithmetic-operator
changes it.
Two kinds of mutants do not prevent removal:
- optional-chaining:
[user?.coupon, code]still becomes[code]. - string-literal replacing a whole string element
with
'':['card', 'paypal']still becomes['paypal'].
Other mutants inside the element still prevent removal. An empty string ''
also stays in the array because string-literal fills it.
With coarseMutants, on in strict,
Mootant removes such elements too: [ → subtotal * rate, tax][tax].
An empty array literal gets an undefined element:
| Code | Mutant |
|---|---|
let coupons: string[] = [] |
let coupons: string[] = [undefined] |
setItems([]) |
setItems([undefined]) |
Mootant skips this change for the empty dependency list of a React hook, as in
useEffect(load, []); see Skipped changes.
Check the whole result, with inputs where each element makes a difference. Take this function:
export function addGift(items: string[], gift: string): string[] {
return [...items, gift]
return [gift]
}A test that starts from an empty cart does not catch [ →
...items, gift][gift], because both versions return only the gift. Put items in the cart and
compare the whole result:
it('keeps the items and adds the gift', () => {
expect(addGift(['mug', 'tea'], 'tote bag')).toEqual([
'mug',
'tea',
'tote bag',
])
})With the mutant, addGift(['mug', 'tea'], 'tote bag') returns ['tote bag'].
The same test catches [...items → , gift][...items], which returns
['mug', 'tea'].
Other cases need these checks:
[]→[undefined]: check the length, or the whole array withtoEqual.[subtotal,→shipping,tax][subtotal, tax], when the code sums the array: pass a shipping cost other than 0.[→cartId][]inuseEffect: check that the effect runs again for a newcartId.
Turn the mutator off in the configuration:
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'array-element': 'off',
},
})To ignore the mutants of this mutator on one line, put an ignore comment above it:
// mootant-ignore-next-line array-element, statement-removal: speed only
preload([heroImage, logoImage])Without statement-removal in the list, the line
gets → preload([heroImage, logoImage]);. Turning this mutator off brings
back that mutant too.
This mutator has no options.
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 |
|---|---|
[subtotal * rate, tax]→[tax] | Removes code that has a finer mutant insidesubsumedsolitary |
[]→[undefined] in useEffect(load, []) | A React hook behaves the same with [undefined]equivalenthook-dependencies |
[]→[undefined] in let parts = [] | join('') writes undefined as an empty stringequivalentjoined-array |
[...box, item]→[...box] | May keep a loop running forevertimeoutloop-advance |
[sku, sku]→[sku] | Same code as another mutantduplicateduplicate |
['sku']→[] in defineProps() | Read by the Vue or Svelte compiler as writteninvalidcompiler-macro |
['a', 'b', …]→['b', …] in a Svelte class | An element in a Svelte template or in an array with a hole needs a copy of the array, and the array has more than eightinvalidliteral-copies |
The coarseMutants setting brings back
solitary, and timeouts brings back
loop-advance; strict turns on both. solitary also comes back when the
mutant inside doesn’t run, for example because its mutator is off. The other
changes stay skipped.