-
Notifications
You must be signed in to change notification settings - Fork 1
/
FunctionChainMutator.ts
35 lines (28 loc) · 1.23 KB
/
FunctionChainMutator.ts
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
import * as ts from 'typescript';
import NodeMutator, {NodeReplacement} from './NodeMutator';
export default class FunctionChainMutator extends NodeMutator<ts.CallExpression> {
public name = 'FunctionChain';
public guard(node: ts.Node): node is ts.CallExpression {
return node.kind === ts.SyntaxKind.CallExpression;
}
protected identifyReplacements(fn: ts.CallExpression, sourceFile: any): NodeReplacement[] {
if (fn.parent.kind === ts.SyntaxKind.PropertyAccessExpression
&& fn.parent.parent.kind === ts.SyntaxKind.CallExpression
|| fn.expression.kind != ts.SyntaxKind.PropertyAccessExpression)
return [];
const mutants = [];
const removeCall = (node: ts.CallExpression) => {
if (node.expression.kind === ts.SyntaxKind.PropertyAccessExpression) {
let propertyAccess = node.expression as ts.PropertyAccessExpression;
mutants.push({
node: fn,
replacement: propertyAccess.expression.getFullText() + sourceFile.text.substring(node.getEnd(), fn.getEnd())
});
if (propertyAccess.expression.kind === ts.SyntaxKind.CallExpression)
removeCall(propertyAccess.expression as ts.CallExpression);
}
}
removeCall(fn);
return mutants;
}
}