Skip to content

Commit

Permalink
#63 implement objectliteral mutator (#65)
Browse files Browse the repository at this point in the history
* object literal implementation of mutation levels

* change construct of ObjectLiteral

* Last Modifications to adhere to new implementation
  • Loading branch information
Ja4pp authored Dec 6, 2023
1 parent 1523566 commit ff9c4c3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
6 changes: 2 additions & 4 deletions packages/api/schema/stryker-core.json
Original file line number Diff line number Diff line change
Expand Up @@ -682,10 +682,8 @@
]
},
"ObjectLiteral": {
"title": "ObjectLiteralMutator",
"type": "boolean",
"description": "Replace ```{ foo: 'bar' }``` with ```{ }```.",
"default": false
"const": "ObjectLiteral",
"description": "Replace ```{ foo: 'bar' }``` with ```{ }```."
},
"OptionalChaining": {
"title": "OptionalChaining",
Expand Down
4 changes: 2 additions & 2 deletions packages/instrumenter/src/mutation-level/mutation-level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ConditionalExpression,
EqualityOperator,
MethodExpression,
ObjectLiteralMutator,
ObjectLiteral,
OptionalChaining,
Regex,
StringLiteral,
Expand All @@ -34,7 +34,7 @@ export interface MutationLevel {
ConditionalExpression?: ConditionalExpression[];
EqualityOperator?: EqualityOperator[];
MethodExpression?: MethodExpression[];
ObjectLiteral?: ObjectLiteralMutator[];
ObjectLiteral?: ObjectLiteral[];
OptionalChaining?: OptionalChaining[];
Regex?: Regex[];
StringLiteral?: StringLiteral[];
Expand Down
8 changes: 6 additions & 2 deletions packages/instrumenter/src/mutators/object-literal-mutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ const { types } = babel;
export const objectLiteralMutator: NodeMutator = {
name: 'ObjectLiteral',

*mutate(path) {
if (path.isObjectExpression() && path.node.properties.length > 0) {
*mutate(path, options) {
if (path.isObjectExpression() && path.node.properties.length > 0 && isInMutationLevel(options)) {
yield types.objectExpression([]);
}
},
};

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 { objectLiteralMutator as sut } from '../../../src/mutators/object-literal-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 objectLiteralLevel: MutationLevel = { name: 'ObjectLiteralLevel', ObjectLiteral: ['ObjectLiteral'] };
const objectLiteralUndefinedLevel: MutationLevel = { name: 'ObjectLiteralLevel' };

describe(sut.name, () => {
it('should have name "ObjectLiteral"', () => {
Expand All @@ -23,4 +27,16 @@ describe(sut.name, () => {
it('shoud not mutate empty object declarations', () => {
expectJSMutation(sut, 'const o = {}');
});

it('should only mutate what is defined in the mutator level', () => {
expectJSMutationWithLevel(sut, objectLiteralLevel.ObjectLiteral, 'const o = { ["foo"]: "bar" }', 'const o = {}');
});

it('should not mutate anything if there are no values in the mutation level', () => {
expectJSMutationWithLevel(sut, [], "{ foo: 'bar' }");
});

it('should mutate everything if the mutation level is undefined', () => {
expectJSMutationWithLevel(sut, objectLiteralUndefinedLevel.ObjectLiteral, 'const o = { ["foo"]: "bar" }', 'const o = {}');
});
});

0 comments on commit ff9c4c3

Please sign in to comment.