-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Conditional expression mutator): Mutate conditional operators (#…
…1253) The `ConditionalExpressionMutator` now also mutates conditional expressions in the wild. Not only in `for`, `while` and `if` statements. For example: `const foo = bar < baz` will be mutated to `const foo = bar <= baz`. Loops are still not mutated to obvious infinite loops (for example `while (i < 10) { i++ }` **isn't** mutated to `while (true) { i++ }`)
- Loading branch information
Showing
4 changed files
with
131 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 40 additions & 6 deletions
46
packages/stryker-javascript-mutator/src/mutators/ConditionalExpressionMutator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,55 @@ | ||
import * as types from '@babel/types'; | ||
import NodeMutator from './NodeMutator'; | ||
import NodeGenerator from '../helpers/NodeGenerator'; | ||
import { NodeWithParent } from '../helpers/ParentNode'; | ||
|
||
/** | ||
* Represents a mutator which can remove the conditional clause from statements. | ||
*/ | ||
export default class ConditionalExpressionMutator implements NodeMutator { | ||
private readonly validOperators: string[] = [ | ||
'!=', | ||
'!==', | ||
'&&', | ||
'<', | ||
'<=', | ||
'==', | ||
'===', | ||
'>', | ||
'>=', | ||
'||', | ||
]; | ||
|
||
public name = 'ConditionalExpression'; | ||
|
||
constructor() { } | ||
constructor() {} | ||
|
||
private hasValidParent(node: NodeWithParent): boolean { | ||
return ( | ||
!node.parent || | ||
!( | ||
types.isForStatement(node.parent) || | ||
types.isWhileStatement(node.parent) || | ||
types.isIfStatement(node.parent) || | ||
types.isDoWhileStatement(node.parent) | ||
) | ||
); | ||
} | ||
|
||
private isValidOperator(operator: string): boolean { | ||
return this.validOperators.indexOf(operator) !== -1; | ||
} | ||
|
||
public mutate(node: types.Node): types.Node[] | void { | ||
if (types.isConditionalExpression(node)) { | ||
return [ | ||
NodeGenerator.createBooleanLiteralNode(node.test, false), | ||
NodeGenerator.createBooleanLiteralNode(node.test, true) | ||
]; | ||
if ( | ||
(types.isBinaryExpression(node) || types.isLogicalExpression(node)) && | ||
this.hasValidParent(node) && | ||
this.isValidOperator(node.operator) | ||
) { | ||
return [ | ||
NodeGenerator.createBooleanLiteralNode(node, false), | ||
NodeGenerator.createBooleanLiteralNode(node, true), | ||
]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 36 additions & 6 deletions
42
packages/stryker-typescript/src/mutator/ConditionalExpressionMutator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters