Skip to content

Commit

Permalink
fix(String literal mutator): Don't mutate export declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
bharaninb authored and nicojs committed Nov 27, 2018
1 parent 5673e6b commit c764ccd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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!' : '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export default class StringLiteralMutator extends NodeMutator<AllStringLiterals>
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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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");');
});
});

0 comments on commit c764ccd

Please sign in to comment.