1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//! Mutation operators and their stable kebab-case names.
use serde::Serialize;
/// One mutation operator.
///
/// The default set ([`Operator::DEFAULT`]) comes from selective-mutation
/// research: it keeps the operators that give most of the test-strength
/// signal and drops redundant ones. The other operators of [`Operator::ALL`]
/// are opt-in: they make more mutants, many of them equivalent or killed by
/// any test, so they run only when a run names them.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum Operator {
/// `+` and `-` swap, `*` and `/` swap, `%` becomes `*`. The compound
/// assignments `+=` and `-=`, `*=` and `/=`, `<<=` and `>>=`, `&=` and
/// `|=` swap, and `%=` becomes `*=`.
ArithmeticOperator,
/// The non-redundant relational subset (Kaminski), and the constant
/// result of the comparison.
RelationalOperator,
/// `&&` and `||` swap, `??` becomes `&&`. `&&=` and `||=` swap, `??=`
/// becomes `&&=`.
LogicalOperator,
/// A branch or loop test becomes `true` and `false`, and a boolean
/// operand of `&&` or `||` becomes the constant that does not decide
/// the whole expression.
NegateCondition,
/// `!x`, `-x` and `~x` become `x`. `-x` and `+x` swap.
UnaryOperator,
/// `++` and `--` swap.
UpdateOperator,
/// A call statement becomes a no-op.
RemovedCall,
/// `return expr` and the concise body of `x => expr` become `undefined`.
ReturnValue,
/// `true` and `false` swap.
BooleanLiteral,
/// A non-empty string or template becomes empty. An empty one becomes
/// `"fallow"` or `` `fallow` ``.
StringLiteral,
/// `a?.b` becomes `a.b`, `a?.()` becomes `a()`.
OptionalChaining,
/// One token of a regular expression literal changes: an anchor or a
/// quantifier is removed, a `{n,m}` range becomes `{n}`, a character
/// class is negated, or a class escape gets the opposite case.
RegexLiteral,
/// One call link of a fluent chain in a module-level `const`
/// initializer is removed: `r.m(args)` becomes `r`, when `r` is a call.
RemovedChainLink,
/// Opt-in. The statements of a non-empty block or function body are
/// removed.
BlockStatement,
/// Opt-in. `[x]` becomes `[]`, and `[]` becomes `["fallow"]`.
ArrayLiteral,
/// Opt-in. A non-empty object literal becomes `{}`.
ObjectLiteral,
/// Opt-in. A string, array or date method call becomes its pair
/// (`startsWith` and `endsWith`), or the call is removed (`s.trim()`
/// becomes `s`).
MethodExpression,
/// Opt-in. The statements of a `case` or `default` clause are removed,
/// so the clause falls through.
SwitchCase,
/// Opt-in. A call in an expression position becomes `void 0`.
CallExpression,
/// Opt-in. A `throw new X(...)` statement is removed.
ThrowStatement,
/// Opt-in. The `regex-literal` and `regex-assertion` changes, applied
/// to the string pattern of `new RegExp("...")` or `RegExp("...")`.
RegexConstructor,
/// Opt-in. A lookaround of a regular expression literal is negated
/// (`(?=` and `(?!` swap, `(?<=` and `(?<!` swap), or `\b` and `\B`
/// swap.
RegexAssertion,
/// Opt-in. `a ?? b` becomes `a`: the fallback of a nullish coalescing
/// expression is removed.
NullishFallback,
}
impl Operator {
/// The operators of a run that names no operator.
pub const DEFAULT: [Self; 13] = [
Self::ArithmeticOperator,
Self::RelationalOperator,
Self::LogicalOperator,
Self::NegateCondition,
Self::UnaryOperator,
Self::UpdateOperator,
Self::RemovedCall,
Self::ReturnValue,
Self::BooleanLiteral,
Self::StringLiteral,
Self::OptionalChaining,
Self::RegexLiteral,
Self::RemovedChainLink,
];
/// The operators that run only when a run names them.
pub const OPT_IN: [Self; 10] = [
Self::BlockStatement,
Self::ArrayLiteral,
Self::ObjectLiteral,
Self::MethodExpression,
Self::SwitchCase,
Self::CallExpression,
Self::ThrowStatement,
Self::RegexConstructor,
Self::RegexAssertion,
Self::NullishFallback,
];
/// Every operator, in a stable order: the default set, then the opt-in
/// set.
pub const ALL: [Self; 23] = [
Self::ArithmeticOperator,
Self::RelationalOperator,
Self::LogicalOperator,
Self::NegateCondition,
Self::UnaryOperator,
Self::UpdateOperator,
Self::RemovedCall,
Self::ReturnValue,
Self::BooleanLiteral,
Self::StringLiteral,
Self::OptionalChaining,
Self::RegexLiteral,
Self::RemovedChainLink,
Self::BlockStatement,
Self::ArrayLiteral,
Self::ObjectLiteral,
Self::MethodExpression,
Self::SwitchCase,
Self::CallExpression,
Self::ThrowStatement,
Self::RegexConstructor,
Self::RegexAssertion,
Self::NullishFallback,
];
/// True for an operator of [`Operator::DEFAULT`].
pub fn is_default(self) -> bool {
Self::DEFAULT.contains(&self)
}
/// The stable kebab-case name of the operator.
pub const fn as_str(self) -> &'static str {
match self {
Self::ArithmeticOperator => "arithmetic-operator",
Self::RelationalOperator => "relational-operator",
Self::LogicalOperator => "logical-operator",
Self::NegateCondition => "negate-condition",
Self::UnaryOperator => "unary-operator",
Self::UpdateOperator => "update-operator",
Self::RemovedCall => "removed-call",
Self::ReturnValue => "return-value",
Self::BooleanLiteral => "boolean-literal",
Self::StringLiteral => "string-literal",
Self::OptionalChaining => "optional-chaining",
Self::RegexLiteral => "regex-literal",
Self::RemovedChainLink => "removed-chain-link",
Self::BlockStatement => "block-statement",
Self::ArrayLiteral => "array-literal",
Self::ObjectLiteral => "object-literal",
Self::MethodExpression => "method-expression",
Self::SwitchCase => "switch-case",
Self::CallExpression => "call-expression",
Self::ThrowStatement => "throw-statement",
Self::RegexConstructor => "regex-constructor",
Self::RegexAssertion => "regex-assertion",
Self::NullishFallback => "nullish-fallback",
}
}
/// Parses a kebab-case operator name.
pub fn from_name(name: &str) -> Option<Self> {
Self::ALL
.into_iter()
.find(|operator| operator.as_str() == name)
}
}
impl std::fmt::Display for Operator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}