From c764ccd17e0cf745c069b462f512b9041d6c8c90 Mon Sep 17 00:00:00 2001 From: Bharanidharan Natesan Date: Tue, 27 Nov 2018 17:27:20 +0530 Subject: [PATCH] fix(String literal mutator): Don't mutate export declarations --- .../src/mutators/StringLiteralMutator.ts | 2 +- .../src/StringLiteralMutatorSpec.ts | 5 +++++ .../src/mutator/StringLiteralMutator.ts | 3 +++ .../unit/mutator/StringLiteralMutatorSpec.ts | 20 ++++++++++++++++++- 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/packages/stryker-javascript-mutator/src/mutators/StringLiteralMutator.ts b/packages/stryker-javascript-mutator/src/mutators/StringLiteralMutator.ts index 5a25ada110..5665164b77 100644 --- a/packages/stryker-javascript-mutator/src/mutators/StringLiteralMutator.ts +++ b/packages/stryker-javascript-mutator/src/mutators/StringLiteralMutator.ts @@ -25,7 +25,7 @@ export default class StringLiteralMutator implements NodeMutator { } nodes.push(mutatedNode); - } else if ((!node.parent || (!types.isImportDeclaration(node.parent) && !types.isJSXAttribute(node.parent))) + } else if ((!node.parent || (!types.isImportDeclaration(node.parent) && !types.isExportDeclaration(node.parent) && !types.isJSXAttribute(node.parent))) && types.isStringLiteral(node)) { const mutatedNode = copy(node); mutatedNode.value = mutatedNode.value.length === 0 ? 'Stryker was here!' : ''; diff --git a/packages/stryker-mutator-specification/src/StringLiteralMutatorSpec.ts b/packages/stryker-mutator-specification/src/StringLiteralMutatorSpec.ts index fe961ea9c1..4db4f3923e 100644 --- a/packages/stryker-mutator-specification/src/StringLiteralMutatorSpec.ts +++ b/packages/stryker-mutator-specification/src/StringLiteralMutatorSpec.ts @@ -42,6 +42,11 @@ export default function StringLiteralMutatorSpec(name: string, expectMutation: E expectMutation('import "foo";'); }); + it('should not mutate export statements', () => { + expectMutation('export * from "./foo";'); + expectMutation('export { foo as boo } from "./foo";'); + }); + it('should not mutate type declarations', () => { expectMutation('const a: "hello" = "hello";', 'const a: "hello" = "";'); expectMutation('const a: Record<"id", number> = { id: 10 }'); diff --git a/packages/stryker-typescript/src/mutator/StringLiteralMutator.ts b/packages/stryker-typescript/src/mutator/StringLiteralMutator.ts index 79508b1dec..c79c341d18 100644 --- a/packages/stryker-typescript/src/mutator/StringLiteralMutator.ts +++ b/packages/stryker-typescript/src/mutator/StringLiteralMutator.ts @@ -26,6 +26,9 @@ export default class StringLiteralMutator extends NodeMutator private isInvalidParent(parent: ts.Node): boolean { switch (parent.kind) { case ts.SyntaxKind.ImportDeclaration: + case ts.SyntaxKind.ExportDeclaration: + case ts.SyntaxKind.ModuleDeclaration: + case ts.SyntaxKind.ExternalModuleReference: case ts.SyntaxKind.LastTypeNode: case ts.SyntaxKind.JsxAttribute: case ts.SyntaxKind.ExpressionStatement: diff --git a/packages/stryker-typescript/test/unit/mutator/StringLiteralMutatorSpec.ts b/packages/stryker-typescript/test/unit/mutator/StringLiteralMutatorSpec.ts index ab60cafca6..8829e20245 100644 --- a/packages/stryker-typescript/test/unit/mutator/StringLiteralMutatorSpec.ts +++ b/packages/stryker-typescript/test/unit/mutator/StringLiteralMutatorSpec.ts @@ -1,5 +1,23 @@ -import { verifySpecification } from './mutatorAssertions'; +import { verifySpecification, expectMutation } from './mutatorAssertions'; import StringLiteralMutator from '../../../src/mutator/StringLiteralMutator'; import StringLiteralMutatorSpec from 'stryker-mutator-specification/src/StringLiteralMutatorSpec'; +import NodeMutator from '../../../src/mutator/NodeMutator'; verifySpecification(StringLiteralMutatorSpec, StringLiteralMutator); + +describe('StringLiteralMutator - Extras', () => { + + let mutator: NodeMutator; + before(() => { + mutator = new StringLiteralMutator(); + }); + + it('should not mutate module declarations', () => { + expectMutation(mutator, 'declare module "new-foo-module";'); + expectMutation(mutator, 'declare module "*!foo" { }'); + }); + + it('should not mutate `import = require()` statements', () => { + expectMutation(mutator, 'import foo = require("foo");'); + }); +});