Skip to content

Commit

Permalink
fix(ArrowFunction mutator): don't mutate () => undefined (#2313)
Browse files Browse the repository at this point in the history
  • Loading branch information
simondel committed Aug 3, 2020
1 parent 0010741 commit 310145e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/instrumenter/src/mutators/arrow-function-mutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import { NodeMutator } from '.';
export class ArrowFunctionMutator implements NodeMutator {
public name = 'ArrowFunction';

private readonly undefinedIdentifierName = 'undefined';

public mutate(path: NodePath): NodeMutation[] {
return path.isArrowFunctionExpression() && !types.isBlockStatement(path.node.body)
? [{ original: path.node, replacement: types.arrowFunctionExpression([], types.identifier('undefined')) }]
return path.isArrowFunctionExpression() &&
!types.isBlockStatement(path.node.body) &&
!(types.isIdentifier(path.node.body) && path.node.body.name === this.undefinedIdentifierName)
? [{ original: path.node, replacement: types.arrowFunctionExpression([], types.identifier(this.undefinedIdentifierName)) }]
: [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ describe(ArrowFunctionMutator.name, () => {
it('should not mutate an anonymous function with a block as a body', () => {
expectJSMutation(sut, 'const b = () => { return 4; }');
});

it('should not mutate an anonymous function with undefined as a body', () => {
expectJSMutation(sut, 'const b = () => undefined');
});
});

0 comments on commit 310145e

Please sign in to comment.