Skip to content

Commit

Permalink
fix(instrumenter): skip as expressions (#2471)
Browse files Browse the repository at this point in the history
Don't instrument TS `as` expressions as this can result in invalid code.
  • Loading branch information
nicojs committed Sep 10, 2020
1 parent 4c1c51c commit 2432d84
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 20 deletions.
1 change: 1 addition & 0 deletions packages/instrumenter/src/util/syntax-helpers.ts
Expand Up @@ -144,6 +144,7 @@ export function isTypeAnnotation(path: NodePath): boolean {
return (
path.isInterfaceDeclaration() ||
path.isTypeAnnotation() ||
path.isTSAsExpression() ||
types.isTSInterfaceDeclaration(path.node) ||
types.isTSTypeAnnotation(path.node) ||
types.isTSTypeAliasDeclaration(path.node) ||
Expand Down
Expand Up @@ -46,6 +46,9 @@ describe('instrumenter integration', () => {
it('should be able to instrument super calls', async () => {
await arrangeAndActAssert('super-call.ts');
});
it('should be able to instrument TS type definitions (should not produce mutants)', async () => {
await arrangeAndActAssert('type-definitions.ts');
});

async function arrangeAndActAssert(fileName: string, options = createInstrumenterOptions()) {
const fullFileName = resolveTestResource(fileName);
Expand Down
Expand Up @@ -78,10 +78,19 @@ describe('babel-transformer', () => {
expect(ast.root.program.body[0]).eq(declareGlobal);
});

it('should skip type annotations', () => {
const ast = createTSAst({ rawContent: 'const foo: string;' });
transformBabel(ast, mutantCollectorMock, context);
expectMutateNotCalledWith((t) => t.isTSTypeAnnotation());
describe('types', () => {
it('should skip type annotations', () => {
const ast = createTSAst({ rawContent: 'const foo: string;' });
transformBabel(ast, mutantCollectorMock, context);
expectMutateNotCalledWith((t) => t.isTSTypeAnnotation());
});

it('should skip `as` expressions', () => {
const ast = createTSAst({ rawContent: 'let foo = "bar" as "bar"' });
transformBabel(ast, mutantCollectorMock, context);
expectMutateNotCalledWith((t) => t.isTSLiteralType());
expectMutateNotCalledWith((t) => t.isStringLiteral() && t.parentPath.isTSLiteralType());
});
});

it('should skip import declarations', () => {
Expand Down

This file was deleted.

@@ -0,0 +1,4 @@
/**
* @see https://github.com/stryker-mutator/stryker/issues/2465
*/
const flatten = require('lodash/flatten') as typeof import('lodash/flatten');
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`instrumenter integration issues should be able to instrument urls in strings 1`] = `
exports[`instrumenter integration should be able to instrument TS type definitions (should not produce mutants) 1`] = `
"var __global_69fa48 = function (g) {
if (g.__activeMutant__ === undefined && g.process && g.process.env && g.process.env.__STRYKER_ACTIVE_MUTANT__) {
g.__activeMutant__ = Number(g.process.env.__STRYKER_ACTIVE_MUTANT__);
Expand Down Expand Up @@ -28,11 +28,8 @@ exports[`instrumenter integration issues should be able to instrument urls in st
return g;
}(new Function(\\"return this\\")());
// https://github.com/stryker-mutator/stryker/issues/2402
const object = __global_69fa48.__activeMutant__ === 0 ? {} : (__global_69fa48.__coverMutant__(0), {
fizz: __global_69fa48.__activeMutant__ === 1 ? {} : (__global_69fa48.__coverMutant__(1), {
buzz: __global_69fa48.__activeMutant__ === 2 ? \\"Stryker was here!\\" : (__global_69fa48.__coverMutant__(2), '')
}),
bar: __global_69fa48.__activeMutant__ === 3 ? \\"\\" : (__global_69fa48.__coverMutant__(3), 'file://')
});"
/**
* @see https://github.com/stryker-mutator/stryker/issues/2465
*/
const flatten = (require('lodash/flatten') as typeof import('lodash/flatten'));"
`;

0 comments on commit 2432d84

Please sign in to comment.