Skip to content

Commit

Permalink
Added support for arrowfunction (#47)
Browse files Browse the repository at this point in the history
* Added support for arrowfunction

* update schema

* Fix up arrow function sting

---------

Co-authored-by: Danut Copae <[email protected]>
Co-authored-by: Danut Copae <[email protected]>
  • Loading branch information
3 people authored Dec 5, 2023
1 parent 7ac2587 commit 1523566
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
5 changes: 5 additions & 0 deletions packages/api/schema/stryker-core.json
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@
{ "$ref": "#/definitions/ArrayDeclaration" },
{ "$ref": "#/definitions/AssignmentOperator" },
{ "$ref": "#/definitions/BlockStatement" },
{ "$ref": "#/definitions/ArrowFunction"},
{ "$ref": "#/definitions/BooleanLiteral" },
{ "$ref": "#/definitions/ConditionalExpression" },
{ "$ref": "#/definitions/EqualityOperator" },
Expand Down Expand Up @@ -417,6 +418,10 @@
}
]
},
"ArrowFunction": {
"const": "ArrowFunction",
"description": "Mutates bodies of arrow functions to undefined"
},
"BlockStatement": {
"const": "BlockStatement",
"description": "Removes the content of every block statement."
Expand Down
6 changes: 4 additions & 2 deletions packages/instrumenter/src/mutation-level/mutation-level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from 'fs';
import {
ArithmeticOperator,
ArrayDeclaration,
ArrowFunction,
AssignmentOperator,
BlockStatement,
BooleanLiteral,
Expand All @@ -27,14 +28,15 @@ export interface MutationLevel {
ArithmeticOperator?: ArithmeticOperator[];
ArrayDeclaration?: ArrayDeclaration[];
AssignmentOperator?: AssignmentOperator[];
BlockStatement?: BlockStatement;
ArrowFunction?: ArrowFunction[];
BlockStatement?: BlockStatement[];
BooleanLiteral?: BooleanLiteral[];
ConditionalExpression?: ConditionalExpression[];
EqualityOperator?: EqualityOperator[];
MethodExpression?: MethodExpression[];
ObjectLiteral?: ObjectLiteralMutator[];
OptionalChaining?: OptionalChaining[];
Regex?: Regex;
Regex?: Regex[];
StringLiteral?: StringLiteral[];
UnaryOperator?: UnaryOperator[];
UpdateOperator?: UpdateOperator[];
Expand Down
9 changes: 7 additions & 2 deletions packages/instrumenter/src/mutators/arrow-function-mutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ import { NodeMutator } from './index.js';
export const arrowFunctionMutator: NodeMutator = {
name: 'ArrowFunction',

*mutate(path) {
*mutate(path, options) {
if (
path.isArrowFunctionExpression() &&
!types.isBlockStatement(path.node.body) &&
!(types.isIdentifier(path.node.body) && path.node.body.name === 'undefined')
!(types.isIdentifier(path.node.body) && path.node.body.name === 'undefined') &&
isInMutationLevel(options)
) {
yield types.arrowFunctionExpression([], types.identifier('undefined'));
}
},
};

function isInMutationLevel(operations: string[] | undefined): boolean {
return operations === undefined || operations.length > 0;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { expect } from 'chai';

import { arrowFunctionMutator as sut } from '../../../src/mutators/arrow-function-mutator.js';
import { expectJSMutation } from '../../helpers/expect-mutation.js';
import { expectJSMutation, expectJSMutationWithLevel } from '../../helpers/expect-mutation.js';
import { MutationLevel } from '../../../src/mutation-level/mutation-level.js';

const arrowFunctionLevel: MutationLevel = { name: 'ArrowFunctionLevel', ArrowFunction: ['ArrowFunction'] };
const arrowFunctionUndefinedLevel: MutationLevel = { name: 'ArrowFunctionLevel' };

describe(sut.name, () => {
it('should have name "ArrowFunction"', () => {
Expand All @@ -19,4 +23,16 @@ describe(sut.name, () => {
it('should not mutate an anonymous function with undefined as a body', () => {
expectJSMutation(sut, 'const b = () => undefined');
});

it('should only mutate what is defined in the mutator level', () => {
expectJSMutationWithLevel(sut, arrowFunctionLevel.ArrowFunction, 'const b = () => 4;', 'const b = () => undefined;');
});

it('should not mutate anything if there are no values in the mutation level', () => {
expectJSMutationWithLevel(sut, [], 'const b = () => 4;');
});

it('should mutate everything if the mutation level is undefined', () => {
expectJSMutationWithLevel(sut, arrowFunctionUndefinedLevel.ArrowFunction, 'const b = () => 4;', 'const b = () => undefined;');
});
});

0 comments on commit 1523566

Please sign in to comment.