Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as types from '@babel/types';

import { NodeGenerator } from '../helpers/NodeGenerator';

import { NodeMutator } from './NodeMutator';

/**
* Represents a mutator which can mutate an object shorthand property.
*/
export default class ObjectShorthandPropertyMutator implements NodeMutator {
public name = 'ObjectShorthandProperty';

public mutate(node: types.Node): Array<[types.Node, types.Node | { raw: string }]> {
return types.isObjectExpression(node)
? node.properties
.filter((prop) => types.isObjectProperty(prop) && prop.shorthand)
.map((mutateProp) => [
node,
NodeGenerator.createMutatedCloneWithProperties(node, { properties: node.properties.filter((prop) => prop !== mutateProp) }),
])
: [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ObjectShorthandPropertyMutatorSpec } from '@stryker-mutator/mutator-specification/src/index';

import ObjectShorthandPropertyMutator from '../../../src/mutators/ObjectShorthandPropertyMutator';
import { verifySpecification } from '../../helpers/mutatorAssertions';

verifySpecification(ObjectShorthandPropertyMutatorSpec, ObjectShorthandPropertyMutator);
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { expect } from 'chai';

import ExpectMutation from './ExpectMutation';

export default function ObjectShorthandPropertyMutatorSpec(name: string, expectMutation: ExpectMutation) {
describe('ObjectShorthandPropertyMutator', () => {
it('should have name "ObjectShorthandProperty"', () => {
expect(name).eq('ObjectShorthandProperty');
});

it('should mutate a single object shorthand property', () => {
expectMutation('const o = { bar }', 'const o = {}');
});

it('should mutate multiple object shorthand properties', () => {
expectMutation('const o = { bar, baz }', 'const o = { baz }', 'const o = { bar }');
});

it('should only mutate an object shorthand property', () => {
expectMutation('const o = { bar, baz: "qux" }', 'const o = { baz: "qux" }');
});

it('shoud not mutate empty object declarations', () => {
expectMutation('const o = {}');
});
});
}
1 change: 1 addition & 0 deletions packages/mutator-specification/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export { default as ConditionalExpressionMutatorSpec } from './ConditionalExpres
export { default as EqualityOperatorMutatorSpec } from './EqualityOperatorMutatorSpec';
export { default as LogicalOperatorMutatorSpec } from './LogicalOperatorMutatorSpec';
export { default as ObjectLiteralMutatorSpec } from './ObjectLiteralMutatorSpec';
export { default as ObjectShorthandPropertyMutatorSpec } from './ObjectShorthandPropertyMutatorSpec';
export { default as StringLiteralMutatorSpec } from './StringLiteralMutatorSpec';
export { default as UnaryOperatorMutatorSpec } from './UnaryOperatorMutatorSpec';
export { default as UpdateOperatorMutatorSpec } from './UpdateOperatorMutatorSpec';