-
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(mutators): add mutators to instrumenter package (#2266)
Add our existing mutators: * ArithmeticOperator * ArrayDeclaration * ArrowFunction * BlockStatement * BooleanLiteral * ConditionalExpression * EqualityOperator * LogicalOperator * ObjectLiteral * StringLiteral * UnaryOperator * UpdateOperator
- Loading branch information
Showing
29 changed files
with
808 additions
and
102 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
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
24 changes: 24 additions & 0 deletions
24
packages/instrumenter/src/mutators/array-declaration-mutator.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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { NodePath, types } from '@babel/core'; | ||
|
||
import { NodeMutation } from '../mutant'; | ||
|
||
import { NodeMutator } from './node-mutator'; | ||
|
||
export class ArrayDeclarationMutator implements NodeMutator { | ||
public name = 'ArrayDeclaration'; | ||
|
||
public mutate(path: NodePath): NodeMutation[] { | ||
if (path.isArrayExpression()) { | ||
const replacement = path.node.elements.length ? types.arrayExpression() : types.arrayExpression([types.stringLiteral('Stryker was here')]); | ||
return [{ original: path.node, replacement }]; | ||
} else if ((path.isCallExpression() || path.isNewExpression()) && types.isIdentifier(path.node.callee) && path.node.callee.name === 'Array') { | ||
const mutatedCallArgs = path.node.arguments && path.node.arguments.length ? [] : [types.arrayExpression()]; | ||
const replacement = types.isNewExpression(path) | ||
? types.newExpression(path.node.callee, mutatedCallArgs) | ||
: types.callExpression(path.node.callee, mutatedCallArgs); | ||
return [{ original: path.node, replacement }]; | ||
} else { | ||
return []; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/instrumenter/src/mutators/arrow-function-mutator.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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as types from '@babel/types'; | ||
import { NodePath } from '@babel/core'; | ||
|
||
import { NodeMutation } from '../mutant'; | ||
|
||
import { NodeMutator } from '.'; | ||
|
||
export class ArrowFunctionMutator implements NodeMutator { | ||
public name = 'ArrowFunction'; | ||
|
||
public mutate(path: NodePath): NodeMutation[] { | ||
return path.isArrowFunctionExpression() && !types.isBlockStatement(path.node.body) | ||
? [{ original: path.node, replacement: types.arrowFunctionExpression([], types.identifier('undefined')) }] | ||
: []; | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
packages/instrumenter/src/mutators/boolean-literal-mutator.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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as types from '@babel/types'; | ||
import { NodePath } from '@babel/core'; | ||
|
||
import { NodeMutation } from '../mutant'; | ||
|
||
import { NodeMutator } from '.'; | ||
|
||
export class BooleanLiteralMutator implements NodeMutator { | ||
public name = 'BooleanLiteral'; | ||
|
||
private readonly unaryBooleanPrefix = '!'; | ||
|
||
public mutate(path: NodePath): NodeMutation[] { | ||
if (path.isBooleanLiteral()) { | ||
return [{ original: path.node, replacement: types.booleanLiteral(!path.node.value) }]; | ||
} else if (path.isUnaryExpression() && path.node.operator === this.unaryBooleanPrefix && path.node.prefix) { | ||
return [{ original: path.node, replacement: types.cloneNode(path.node.argument, false) }]; | ||
} | ||
|
||
return []; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
packages/instrumenter/src/mutators/equality-operator-mutator.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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { types, NodePath } from '@babel/core'; | ||
|
||
import { NodeMutation } from '../mutant'; | ||
|
||
import { NodeMutator } from './node-mutator'; | ||
|
||
export class EqualityOperatorMutator implements NodeMutator { | ||
private readonly operators: { [targetedOperator: string]: BinaryOperator[] } = { | ||
'<': ['<=', '>='], | ||
'<=': ['<', '>'], | ||
'>': ['>=', '<='], | ||
'>=': ['>', '<'], | ||
'==': ['!='], | ||
'!=': ['=='], | ||
'===': ['!=='], | ||
'!==': ['==='], | ||
}; | ||
|
||
public name = 'EqualityOperator'; | ||
|
||
public mutate(path: NodePath): NodeMutation[] { | ||
if (path.isBinaryExpression()) { | ||
let mutatedOperators = this.operators[path.node.operator]; | ||
if (mutatedOperators) { | ||
return mutatedOperators.map((mutatedOperator) => { | ||
const replacement = types.cloneNode(path.node, false) as types.BinaryExpression; | ||
replacement.operator = mutatedOperator; | ||
|
||
return { | ||
original: path.node, | ||
replacement, | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
packages/instrumenter/src/mutators/logical-operator-mutator.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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as types from '@babel/types'; | ||
import { NodePath } from '@babel/core'; | ||
|
||
import { NodeMutation } from '../mutant'; | ||
|
||
import { NodeMutator } from '.'; | ||
|
||
export class LogicalOperatorMutator implements NodeMutator { | ||
public name = 'LogicalOperator'; | ||
|
||
private readonly operators: { [targetedOperator: string]: '||' | '&&' } = { | ||
'&&': '||', | ||
'||': '&&', | ||
}; | ||
|
||
public mutate(path: NodePath): NodeMutation[] { | ||
if (path.isLogicalExpression()) { | ||
const mutatedOperator = this.operators[path.node.operator]; | ||
if (mutatedOperator) { | ||
const replacement = types.cloneNode(path.node, false); | ||
replacement.operator = mutatedOperator; | ||
return [{ original: path.node, replacement }]; | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/instrumenter/src/mutators/object-literal-mutator.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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import * as types from '@babel/types'; | ||
import { NodePath } from '@babel/core'; | ||
|
||
import { NodeMutation } from '../mutant'; | ||
|
||
import { NodeMutator } from '.'; | ||
|
||
export class ObjectLiteralMutator implements NodeMutator { | ||
public name = 'ObjectLiteral'; | ||
|
||
public mutate(path: NodePath): NodeMutation[] { | ||
return path.isObjectExpression() && path.node.properties.length > 0 ? [{ original: path.node, replacement: types.objectExpression([]) }] : []; | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
packages/instrumenter/src/mutators/unary-operator-mutator.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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as types from '@babel/types'; | ||
import { NodePath } from '@babel/core'; | ||
|
||
import { NodeMutation } from '../mutant'; | ||
|
||
import { NodeMutator } from '.'; | ||
|
||
export class UnaryOperatorMutator implements NodeMutator { | ||
public name = 'UnaryOperator'; | ||
|
||
private readonly operators: { [targetedOperator: string]: string } = { | ||
'+': '-', | ||
'-': '+', | ||
'~': '', | ||
}; | ||
|
||
public mutate(path: NodePath): NodeMutation[] { | ||
if (path.isUnaryExpression() && this.operators[path.node.operator] !== undefined && path.node.prefix) { | ||
return this.operators[path.node.operator].length > 0 | ||
? [ | ||
{ | ||
original: path.node, | ||
replacement: types.unaryExpression(this.operators[path.node.operator] as any, path.node.argument), | ||
}, | ||
] | ||
: [ | ||
{ | ||
original: path.node, | ||
replacement: types.cloneNode(path.node.argument, false), | ||
}, | ||
]; | ||
} | ||
|
||
return []; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/instrumenter/src/mutators/update-operator-mutator.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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import * as types from '@babel/types'; | ||
import { NodePath } from '@babel/core'; | ||
|
||
import { NodeMutation } from '../mutant'; | ||
|
||
import { NodeMutator } from '.'; | ||
|
||
export class UpdateOperatorMutator implements NodeMutator { | ||
public name = 'UpdateOperator'; | ||
|
||
private readonly operators: { [targetedOperator: string]: '--' | '++' } = { | ||
'++': '--', | ||
'--': '++', | ||
}; | ||
|
||
public mutate(path: NodePath): NodeMutation[] { | ||
return path.isUpdateExpression() && this.operators[path.node.operator] !== undefined | ||
? [ | ||
{ | ||
original: path.node, | ||
replacement: types.updateExpression(this.operators[path.node.operator], path.node.argument, path.node.prefix), | ||
}, | ||
] | ||
: []; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
type BinaryOperator = | ||
| '+' | ||
| '-' | ||
| '/' | ||
| '%' | ||
| '*' | ||
| '**' | ||
| '&' | ||
| '|' | ||
| '>>' | ||
| '>>>' | ||
| '<<' | ||
| '^' | ||
| '==' | ||
| '===' | ||
| '!=' | ||
| '!==' | ||
| 'in' | ||
| 'instanceof' | ||
| '>' | ||
| '<' | ||
| '>=' | ||
| '<='; |
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
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
Oops, something went wrong.