Skip to content

Commit

Permalink
fix(BlockStatementMutator): Not mutate empty block (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicojs authored and simondel committed Oct 7, 2016
1 parent 6ac375f commit da4a3cf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/mutators/BlockStatementMutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class BlockStatementMutator implements Mutator {
constructor() { }

applyMutations(node: estree.Node, copy: <T>(obj: T, deep?: boolean) => T): void | estree.Node | estree.Node[] {
if (node.type === Syntax.BlockStatement) {
if (node.type === Syntax.BlockStatement && node.body.length > 0) {
let mutatedNode = copy(node);
mutatedNode.body = [];
return mutatedNode;
Expand Down
7 changes: 2 additions & 5 deletions src/utils/parserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ const esprimaOptions = {
* @param code - The code which has to be parsed.
* @returns {Object} The generated Abstract Syntax Tree.
*/
export function parse(code: string): any {
export function parse(code: string): estree.Program {
if (code === undefined) {
throw new Error('Code parameter cannot be undefined');
}
if (code === '') {
return {};
}

const abstractSyntaxTree = esprima.parse(code, esprimaOptions);

Expand All @@ -39,7 +36,7 @@ export function parse(code: string): any {
* @param abstractSyntaxTree - The current part of the abstract syntax tree which will be investigated.
* @returns All nodes with a type.
*/
export function collectFrozenNodes(abstractSyntaxTree: any, nodes?: any[]): any[] {
export function collectFrozenNodes(abstractSyntaxTree: estree.Node, nodes?: estree.Node[]): estree.Node[] {
nodes = nodes || [];

if (!_.isArray(abstractSyntaxTree) && _.isObject(abstractSyntaxTree) && abstractSyntaxTree.type && _.isUndefined(abstractSyntaxTree.nodeID)) {
Expand Down
40 changes: 40 additions & 0 deletions test/unit/mutators/BlockStatementMutatorSpec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import BlockStatementMutator from '../../../src/mutators/BlockStatementMutator';
import { expect } from 'chai';
import * as parser from '../../../src/utils/parserUtils';
import { copy } from '../../../src/utils/objectUtils';
import { Syntax } from 'esprima';
import * as estree from 'estree';

describe('BlockStatementMutator', () => {
let sut: BlockStatementMutator;

beforeEach(() => sut = new BlockStatementMutator());

it('should mutate when supplied a block statement', () => {
// Arrange
const program = parser.parse(`function a () {
'use strict';
}`);
const useStrictBlockStatement = (program.body[0] as estree.FunctionDeclaration).body;

// Act
const actual = <estree.BlockStatement>sut.applyMutations(useStrictBlockStatement, copy);

// Assert
expect(actual).to.be.ok;
expect(actual.nodeID).to.eq(useStrictBlockStatement.nodeID);
expect(actual.body).to.have.length(0);
});

it('should not mutate an empty expression', () => {
// Arrange
const program = parser.parse(`function a () {
}`);
const emptyBlockStatement = (program.body[0] as estree.FunctionDeclaration).body;

// Act
const actual = sut.applyMutations(emptyBlockStatement, copy);
expect(actual).to.not.be.ok;
});
});
7 changes: 0 additions & 7 deletions test/unit/utils/parserUtilsSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ describe('parserUtils', () => {

});


it('should return an empty object if an empty string is parsed', () => {
const emptyObject = {};

const result = parserUtils.parse('');
expect(JSON.stringify(result)).to.equal(JSON.stringify(emptyObject));
});
});

});

0 comments on commit da4a3cf

Please sign in to comment.