Skip to content

Commit

Permalink
fix mutation of Array constructor call for TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher J. Brody committed Nov 29, 2019
1 parent fb063cc commit 3b4f9e5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export default function ArrayDeclarationMutatorSpec(name: string, expectMutation
it('should mutate filled Array constructor calls as empty arrays', () => {
expectMutation('new Array(a, 1 + 1)', 'new Array()');
expectMutation("new Array('val')", 'new Array()');
// XXX known issue on TypeScript mutator:
expectMutation("Array('val')", 'Array()');
expectMutation('Array(a, 1 + 1)', 'Array()');
});
Expand All @@ -32,7 +31,6 @@ export default function ArrayDeclarationMutatorSpec(name: string, expectMutation

it('should mutate empty array constructor call as a filled array', () => {
expectMutation('new Array()', 'new Array([])');
// XXX known issue on TypeScript mutator:
expectMutation('Array()', 'Array([])');
});
});
Expand Down
18 changes: 14 additions & 4 deletions packages/typescript/src/mutator/ArrayDeclarationMutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,30 @@ import * as ts from 'typescript';

import NodeMutator, { NodeReplacement } from './NodeMutator';

export default class ArrayDeclarationMutator extends NodeMutator<ts.ArrayLiteralExpression | ts.NewExpression> {
export default class ArrayDeclarationMutator extends NodeMutator<ts.ArrayLiteralExpression | ts.CallExpression | ts.NewExpression> {
public name = 'ArrayDeclaration';

public guard(node: ts.Node): node is ts.ArrayLiteralExpression | ts.NewExpression {
return node.kind === ts.SyntaxKind.ArrayLiteralExpression || node.kind === ts.SyntaxKind.NewExpression;
public guard(node: ts.Node): node is ts.ArrayLiteralExpression | ts.CallExpression | ts.NewExpression {
return node.kind === ts.SyntaxKind.ArrayLiteralExpression || node.kind === ts.SyntaxKind.CallExpression || node.kind === ts.SyntaxKind.NewExpression;
}

protected identifyReplacements(node: ts.ArrayLiteralExpression | ts.NewExpression, sourceFile: ts.SourceFile): NodeReplacement[] {
protected identifyReplacements(node: ts.ArrayLiteralExpression | ts.CallExpression | ts.NewExpression, sourceFile: ts.SourceFile): NodeReplacement[] {
if (node.kind === ts.SyntaxKind.ArrayLiteralExpression) {
if (node.elements.length) {
return [{ node, replacement: '[]' }];
} else {
return [{ node, replacement: '["Stryker was here"]' }];
}
} else if (node.kind === ts.SyntaxKind.CallExpression) {
if (node.expression.kind === ts.SyntaxKind.Identifier && node.expression.getFullText(sourceFile).trim() === 'Array') {
if (node.arguments && node.arguments.length) {
return [{ node, replacement: 'Array()' }];
} else {
return [{ node, replacement: 'Array([])' }];
}
} else {
return [];
}
} else {
if (node.expression.getFullText(sourceFile).trim() === 'Array') {
if (node.arguments && node.arguments.length) {
Expand Down

0 comments on commit 3b4f9e5

Please sign in to comment.