Skip to content

Commit

Permalink
#30 restrict updateoperator mutator (#51)
Browse files Browse the repository at this point in the history
* updated to conform to mutationlevels

* modified tests

* modified config file

* initial implementation of condition

* Revert "initial implementation of condition"

This reverts commit bcb6cc0.

* changed tests to pass CI/CD

* changes nessecary for pass

* small refactor

* remove line added by mistake

---------

Co-authored-by: Danut Copae <[email protected]>
Co-authored-by: Danut Copae <[email protected]>
  • Loading branch information
3 people committed Jan 14, 2024
1 parent 31f53fc commit 242af26
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
34 changes: 28 additions & 6 deletions packages/instrumenter/src/mutators/update-operator-mutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,39 @@ import { NodeMutator } from './index.js';

const { types } = babel;

enum UpdateOperators {
'++' = '--',
'--' = '++',
}
const operators = Object.assign({
'Post++To--': { replacementOperator: '--', mutatorName: 'Post++To--' },
'Post--To++': { replacementOperator: '++', mutatorName: 'Post--To++' },
'Pre++To--': { replacementOperator: '--', mutatorName: 'Pre++To--' },
'Pre--To++': { replacementOperator: '++', mutatorName: 'Pre--To++' },
'++': { replacementOperator: '--', mutatorName: '++all' },
'--': { replacementOperator: '++', mutatorName: '--all' },
} as const);

export const updateOperatorMutator: NodeMutator = {
name: 'UpdateOperator',

*mutate(path) {
*mutate(path, operations) {
if (path.isUpdateExpression()) {
yield types.updateExpression(UpdateOperators[path.node.operator], deepCloneNode(path.node.argument), path.node.prefix);
if (operations === undefined) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
yield types.updateExpression(operators[path.node.operator].replacementOperator, deepCloneNode(path.node.argument), path.node.prefix);
} else {
let replacement = undefined;
if (path.node.prefix && path.node.operator == '++' && operations.includes(operators['Pre++To--'].mutatorName as string)) {
replacement = operators['Pre++To--'].replacementOperator;
} else if (path.node.prefix && path.node.operator == '--' && operations.includes(operators['Pre--To++'].mutatorName as string)) {
replacement = operators['Pre--To++'].replacementOperator;
} else if (!path.node.prefix && path.node.operator == '++' && operations.includes(operators['Post++To--'].mutatorName as string)) {
replacement = operators['Post++To--'].replacementOperator;
} else if (!path.node.prefix && path.node.operator == '--' && operations.includes(operators['Post--To++'].mutatorName as string)) {
replacement = operators['Post--To++'].replacementOperator;
}
if (replacement !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
yield types.updateExpression(replacement, deepCloneNode(path.node.argument), path.node.prefix);
}
}
}
},
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { expect } from 'chai';

import { updateOperatorMutator as sut } from '../../../src/mutators/update-operator-mutator.js';
import { expectJSMutation } from '../../helpers/expect-mutation.js';
import { expectJSMutation, expectJSMutationWithLevel } from '../../helpers/expect-mutation.js';

const updateLevel: string[] = ['Pre--To++', 'Pre++To--'];
const updateLevel2: string[] = ['Post++To--', 'Post--To++'];
const updateLevel3 = undefined;

describe(sut.name, () => {
it('should have name "UpdateOperator"', () => {
Expand All @@ -23,4 +27,36 @@ describe(sut.name, () => {
it('should mutate --a to ++a', () => {
expectJSMutation(sut, '--a', '++a');
});

it('should only mutate --a and ++a', () => {
expectJSMutationWithLevel(
sut,
updateLevel,
'--a; ++a; a--; a++',
'++a; ++a; a--; a++', //mutates --a
'--a; --a; a--; a++', //mutates ++a
);
});

it('should only mutate a-- and a++', () => {
expectJSMutationWithLevel(
sut,
updateLevel2,
'--a; ++a; a--; a++',
'--a; ++a; a--; a--', //mutates a++
'--a; ++a; a++; a++', //mutates a--
);
});

it('should mutate all', () => {
expectJSMutationWithLevel(
sut,
updateLevel3,
'--a; ++a; a--; a++',
'++a; ++a; a--; a++', //mutates --a
'--a; --a; a--; a++', //mutates ++a
'--a; ++a; a--; a--', //mutates a++
'--a; ++a; a++; a++', //mutates a--
);
});
});

0 comments on commit 242af26

Please sign in to comment.