diff --git a/src/mutators/BlockStatementMutator.ts b/src/mutators/BlockStatementMutator.ts index 3a159f55a0..e7847bb79e 100644 --- a/src/mutators/BlockStatementMutator.ts +++ b/src/mutators/BlockStatementMutator.ts @@ -12,7 +12,7 @@ export default class BlockStatementMutator implements Mutator { constructor() { } applyMutations(node: estree.Node, copy: (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; diff --git a/src/utils/parserUtils.ts b/src/utils/parserUtils.ts index f85cb8c21c..dc8ca9e82b 100644 --- a/src/utils/parserUtils.ts +++ b/src/utils/parserUtils.ts @@ -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); @@ -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)) { diff --git a/test/unit/mutators/BlockStatementMutatorSpec.ts b/test/unit/mutators/BlockStatementMutatorSpec.ts new file mode 100644 index 0000000000..3f4b390476 --- /dev/null +++ b/test/unit/mutators/BlockStatementMutatorSpec.ts @@ -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 = 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; + }); +}); \ No newline at end of file diff --git a/test/unit/utils/parserUtilsSpec.ts b/test/unit/utils/parserUtilsSpec.ts index 3d6c7085e9..41a47810e0 100644 --- a/test/unit/utils/parserUtilsSpec.ts +++ b/test/unit/utils/parserUtilsSpec.ts @@ -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)); - }); }); });