Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stopped mutating nodes with a declare modifier #1458

Merged
merged 3 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions packages/typescript/src/TypescriptMutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,20 @@ export class TypescriptMutator {
}

private mutateForNode<T extends ts.Node>(node: T, sourceFile: ts.SourceFile): Mutant[] {
const targetMutators = this.mutators.filter(mutator => mutator.guard(node));
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));
});
return mutants;
if (shouldNodeBeSkipped(node)) {
return [];
} else {
const targetMutators = this.mutators.filter(mutator => mutator.guard(node));
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));
});
return mutants;
}
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
}
}

const shouldNodeBeSkipped = (node: ts.Node): boolean => {
return node.modifiers !== undefined && node.modifiers.some(modifier => modifier.kind === ts.SyntaxKind.DeclareKeyword);
};
62 changes: 62 additions & 0 deletions packages/typescript/test/unit/TypescriptMutator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,67 @@ describe('TypescriptMutator', () => {
expect(mutants.filter(mutant => mutant.mutatorName === 'FunctionDeclarationForTest')).lengthOf(4);
});

describe('declaration nodes', () => {
beforeEach(() => {
sut = createSut();
});

it('should not skip a node when it does not have a declare modifier', () => {
// Arrange
file1 = new File(
'file1.ts',
`function mutated(a: number, b: number);`);

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

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

it('should skip a node when it has a declare modifier', () => {
// Arrange
file1 = new File(
'file1.ts',
`declare function notMutated(a: number, b: number);`);

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

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

});
});