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

Restricted logical-operator-mutator.ts #57

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions packages/instrumenter/src/mutators/logical-operator-mutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import { deepCloneNode } from '../util/index.js';

import { NodeMutator } from './index.js';

const logicalOperatorReplacements = Object.freeze({
'&&': '||',
'||': '&&',
'??': '&&',
const operators = Object.freeze({
'&&': { replacement: '||', mutatorName: '&&To||' },
'||': { replacement: '&&', mutatorName: '||To&&' },
'??': { replacement: '&&', mutatorName: '??To&&' },
} as const);

export const logicalOperatorMutator: NodeMutator = {
name: 'LogicalOperator',

*mutate(path) {
if (path.isLogicalExpression() && isSupported(path.node.operator)) {
const mutatedOperator = logicalOperatorReplacements[path.node.operator];
*mutate(path, operations) {
if (path.isLogicalExpression() && isSupported(path.node.operator) && isInMutationLevel(path.node.operator, operations)) {
const mutatedOperator = operators[path.node.operator].replacement;

const replacement = deepCloneNode(path.node);
replacement.operator = mutatedOperator;
Expand All @@ -22,6 +22,10 @@ export const logicalOperatorMutator: NodeMutator = {
},
};

function isSupported(operator: string): operator is keyof typeof logicalOperatorReplacements {
return Object.keys(logicalOperatorReplacements).includes(operator);
function isSupported(operator: string): operator is keyof typeof operators {
return Object.keys(operators).includes(operator);
}

function isInMutationLevel(operator: string, operations: string[] | undefined): operator is keyof typeof operators {
return operations === undefined || operations.some((op) => op.startsWith(operator));
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai';

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

describe(sut.name, () => {
it('should have name "LogicalOperator"', () => {
Expand All @@ -24,4 +24,17 @@ describe(sut.name, () => {
it('should mutate ?? to &&', () => {
expectJSMutation(sut, 'a ?? b', 'a && b');
});

it('should only mutate || and &&', () => {
const level = ['||To&&', '&&To||'];
expectJSMutationWithLevel(sut, level, 'a || b; a && b; a ?? b', 'a && b; a && b; a ?? b', 'a || b; a || b; a ?? b');
});

it('should mutate all three', () => {
expectJSMutationWithLevel(sut, undefined, 'a || b; a && b; a ?? b', 'a && b; a && b; a ?? b', 'a || b; a || b; a ?? b', 'a || b; a && b; a && b');
});

it('should mutate nothing', () => {
expectJSMutationWithLevel(sut, [], 'a || b; a && b; a ?? b' /*Nothing*/);
});
});