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

Implement mutationLevel construct BlockStatement #66

Merged
merged 1 commit into from
Dec 7, 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
8 changes: 6 additions & 2 deletions packages/instrumenter/src/mutators/block-statement-mutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const { types } = babel;
export const blockStatementMutator: NodeMutator = {
name: 'BlockStatement',

*mutate(path) {
if (path.isBlockStatement() && isValid(path)) {
*mutate(path, options) {
if (path.isBlockStatement() && isValid(path) && isInMutationLevel(options)) {
yield types.blockStatement([]);
}
},
Expand Down Expand Up @@ -69,3 +69,7 @@ function hasSuperExpressionOnFirstLine(constructor: NodePath<babel.types.BlockSt
types.isSuper(constructor.node.body[0].expression.callee)
);
}

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 { blockStatementMutator as sut } from '../../../src/mutators/block-statement-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 blockStatementLevel: MutationLevel = { name: 'BlockStatementLevel', BlockStatement: ['BlockStatement'] };
const blockStatementUndefinedLevel: MutationLevel = { name: 'BlockStatementLevel' };

describe(sut.name, () => {
it('should have name "BlockStatement"', () => {
Expand Down Expand Up @@ -70,5 +74,22 @@ describe(sut.name, () => {
it('should not mutate a constructor containing a super call and contains initialized properties', () => {
expectJSMutation(sut, 'class Foo extends Bar { private baz = "qux"; constructor() { super(); } }');
});

it('should only mutate what is defined in the mutator level', () => {
expectJSMutationWithLevel(sut, blockStatementLevel.BlockStatement, 'class Foo { constructor() { bar(); } }', 'class Foo { constructor() {} }');
});

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

it('should mutate everything if the mutation level is undefined', () => {
expectJSMutationWithLevel(
sut,
blockStatementUndefinedLevel.BlockStatement,
'class Foo { constructor() { bar(); } }',
'class Foo { constructor() {} }',
);
});
});
});