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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/typescript/src/TypescriptMutator.ts
Expand Up @@ -30,6 +30,10 @@ export class TypescriptMutator {
}

private mutateForNode<T extends ts.Node>(node: T, sourceFile: ts.SourceFile): Mutant[] {
if (shouldNodeBeSkipped(node)) {
return [];
}
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved

const targetMutators = this.mutators.filter(mutator => mutator.guard(node));
const mutants = flatMap(targetMutators, mutator => mutator.mutate(node, sourceFile));
node.forEachChild(child => {
Expand All @@ -39,3 +43,7 @@ export class TypescriptMutator {
return mutants;
}
}

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

describe('declaration nodes', () => {
beforeEach(() => {
sut = createSut();
file1 = new File(
'file1.ts',
`declare function notMutated(a: number, b: number);
declare module "not-mutated" { }

function mutated(a: number) { }`);
});

it('should skip nodes with declare keywords', () => {
const mutants = sut.mutate([
file1,
]);
expect(mutants.filter(mutant => mutant.mutatorName === 'SourceFileForTest')).lengthOf(1);
expect(mutants.filter(mutant => mutant.mutatorName !== 'SourceFileForTest')).lengthOf(2);
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
});
});

});
});