Skip to content

Commit

Permalink
Don't crash on regex parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
nicojs committed Jan 22, 2021
1 parent 42500c3 commit 57d5f2a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
32 changes: 21 additions & 11 deletions packages/instrumenter/src/mutators/regex-mutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,31 @@ import { NodeMutation } from '../mutant';

import { NodeMutator } from '.';

// function isObviousRegexString(path: NodePath<types.StringLiteral>){
// if(path.parentPath.isCallExpression() && path.parentPath.isNewExpression())
// }

export class RegexMutator implements NodeMutator {
public name = 'Regex';

public mutate(path: NodePath): NodeMutation[] {
if (path.isRegExpLiteral()) {
return mutate(path.node.pattern, { mutationLevels: [1] }).map((mutant) => {
const replacement = types.cloneNode(path.node, false);
replacement.pattern = mutant.pattern;
return {
original: path.node,
replacement,
};
});
} else {
return [];
}
try {
return mutate(path.node.pattern, { mutationLevels: [1] }).map((mutant) => {
const replacement = types.cloneNode(path.node, false);
replacement.pattern = mutant.pattern;
return {
original: path.node,
replacement,
};
});
} catch (err) {
console.error(
`[RegexMutator]: The Regex parser of weapon-regex couldn't parse this regex pattern: "${path.node.pattern}". Please report this issue at https://github.com/stryker-mutator/weapon-regex/issues. Inner error: ${err.message}`
);
}
}//else if(path.isStringLiteral() && isObviousRegexString(path)){}
return [];
}

}
36 changes: 36 additions & 0 deletions packages/instrumenter/test/unit/mutators/regex-mutator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expect } from 'chai';
import sinon from 'sinon';

import { RegexMutator } from '../../../src/mutators/regex-mutator';
import { expectJSMutation } from '../../helpers/expect-mutation';

describe.only(RegexMutator.name, () => {
let sut: RegexMutator;
beforeEach(() => {
sut = new RegexMutator();
});

it('should have name "Regex"', () => {
expect(sut.name).eq('Regex');
});

it('should not mutate normal string literals', () => {
expectJSMutation(sut, '""');
});

it('should mutate a regex literal', () => {
expectJSMutation(sut, '/\\d{4}/', '/\\d/', '/\\D{4}/');
});

it("should not crash if a regex couldn't be parsed", () => {
const errorStub = sinon.stub(console, 'error');
expectJSMutation(sut, '/[[]]/');
expect(errorStub).calledWith(
'[RegexMutator]: The Regex parser of weapon-regex couldn\'t parse this regex pattern: "[[]]". Please report this issue at https://github.com/stryker-mutator/weapon-regex/issues. Inner error: [Error] Parser: Position 1:1, found "[[]]"'
);
});

// it('should mutate obvious Regex string literals', () => {
// expectJSMutation(sut, 'new RegExp("\\d{4}", "i")', 'new RegExp("\\d", "i")', 'new RegExp("\\D{4}", "i")');
// });
});

0 comments on commit 57d5f2a

Please sign in to comment.