Skip to content

Commit

Permalink
feat(typescript): do not mutate interfaces (#1662)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartosz Leoniak authored and nicojs committed Sep 18, 2019
1 parent 97f0f7e commit 86b2ffe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/typescript/src/TypescriptMutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const MUTATORS_TOKEN = 'mutators';
export class TypescriptMutator {

public static inject = tokens(commonTokens.options, MUTATORS_TOKEN);
constructor(private readonly options: StrykerOptions, public mutators: ReadonlyArray<NodeMutator>) { }
constructor(private readonly options: StrykerOptions, public readonly mutators: ReadonlyArray<NodeMutator>) { }

public mutate(inputFiles: File[]): Mutant[] {
const tsConfig = getTSConfig(this.options);
Expand All @@ -37,13 +37,14 @@ export class TypescriptMutator {
const mutants = flatMap(targetMutators, mutator => mutator.mutate(node, sourceFile));
node.forEachChild(child => {
// It is important that forEachChild does not return a true, otherwise node visiting is halted!
mutants.push(... this.mutateForNode(child, sourceFile));
mutants.push(...this.mutateForNode(child, sourceFile));
});
return mutants;
}
}
}

const shouldNodeBeSkipped = (node: ts.Node): boolean => {
return node.modifiers !== undefined && node.modifiers.some(modifier => modifier.kind === ts.SyntaxKind.DeclareKeyword);
return node.kind === ts.SyntaxKind.InterfaceDeclaration ||
node.modifiers !== undefined && node.modifiers.some(modifier => modifier.kind === ts.SyntaxKind.DeclareKeyword);
};
25 changes: 25 additions & 0 deletions packages/typescript/test/unit/TypescriptMutator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,31 @@ describe('TypescriptMutator', () => {
}
]);
});

it('should skip a node when it is an interface', () => {
// Arrange
file1 = new File(
'file1.ts',
`interface Hello {
value: string;
sortable?: true;
}`);

// Act
const mutants = sut.mutate([
file1,
]);

// Assert
expect(mutants).to.deep.equal([
{
fileName: 'file1.ts',
mutatorName: 'SourceFileForTest',
range: [0, 85],
replacement: '"stryker was here"',
}
]);
});
});

});
Expand Down

0 comments on commit 86b2ffe

Please sign in to comment.