Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#30 restrict updateoperator mutator #51

Merged
merged 11 commits into from
Dec 5, 2023
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--
);
});
});