-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(arrow mutations): add arrow mutations and refactor JavaScript mu…
…tators (#1898) * JavaScript mutator code improvements - use raw string mutations *internally* whenever possible - generate some of the mutations as AST nodes from the Babel types API - use some ternaries - remove some intermediate const declarations no longer needed - remove a couple of internal utility functions no longer needed * add Arrow Function Mutator for JavaScript (already supported by the existing TypeScript mutator) * remove `lodash.clonedeep` from dependencies in packages/javascript-mutator/package.json
- Loading branch information
Showing
20 changed files
with
146 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,10 @@ | ||
import * as types from '@babel/types'; | ||
|
||
export class NodeGenerator { | ||
public static createBooleanLiteralNode(originalNode: types.Node, value: boolean): types.BooleanLiteral { | ||
public static createMutatedCloneWithProperties(originalNode: types.Node, props: { [key: string]: any }): types.Node { | ||
return { | ||
end: originalNode.end, | ||
innerComments: originalNode.innerComments, | ||
leadingComments: originalNode.leadingComments, | ||
loc: originalNode.loc, | ||
start: originalNode.start, | ||
trailingComments: originalNode.trailingComments, | ||
type: 'BooleanLiteral', | ||
value | ||
...originalNode, | ||
...props | ||
}; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/javascript-mutator/src/mutators/ArrowFunctionMutator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as types from '@babel/types'; | ||
|
||
import { NodeMutator } from './NodeMutator'; | ||
|
||
/** | ||
* Represents a mutator which can remove the content of a Object. | ||
*/ | ||
export default class ArrowFunctionMutator implements NodeMutator { | ||
public name = 'ArrowFunction'; | ||
|
||
public mutate(node: types.Node): Array<[types.Node, types.Node | { raw: string }]> { | ||
return types.isArrowFunctionExpression(node) && !types.isBlockStatement(node.body) | ||
? [[node, { raw: '() => undefined' }]] // raw string replacement | ||
: []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 6 additions & 10 deletions
16
packages/javascript-mutator/src/mutators/BooleanLiteralMutator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,22 @@ | ||
import * as types from '@babel/types'; | ||
|
||
import { NodeGenerator } from '../helpers/NodeGenerator'; | ||
|
||
import { NodeMutator } from './NodeMutator'; | ||
|
||
export default class BooleanLiteralMutator implements NodeMutator { | ||
public name = 'BooleanLiteral'; | ||
|
||
private readonly unaryBooleanPrefix = '!'; | ||
|
||
public mutate(node: types.Node, copy: <T extends types.Node>(obj: T, deep?: boolean) => T): types.Node[] { | ||
const nodes: types.Node[] = []; | ||
|
||
public mutate(node: types.Node): Array<[types.Node, types.Node | { raw: string }]> { | ||
// true -> false or false -> true | ||
if (types.isBooleanLiteral(node)) { | ||
const mutatedNode = copy(node); | ||
mutatedNode.value = !mutatedNode.value; | ||
nodes.push(mutatedNode); | ||
return [[node, NodeGenerator.createMutatedCloneWithProperties(node, { value: !node.value })]]; | ||
} else if (types.isUnaryExpression(node) && node.operator === this.unaryBooleanPrefix && node.prefix) { | ||
const mutatedNode = copy(node.argument); | ||
mutatedNode.start = node.start; | ||
nodes.push(mutatedNode); | ||
return [[node, node.argument]]; | ||
} | ||
|
||
return nodes; | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.