Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function hasContext(type) {
return [
Syntax.ArrowFunctionExpression,
Syntax.FunctionDeclaration,
Syntax.FunctionExpression,
Syntax.ForStatement,
Syntax.ForInStatement,
Syntax.ForOfStatement,
Syntax.BlockStatement,
].indexOf(type) !== -1;
}
import { Syntax } from 'esprima';
import * as estree from 'estree';
import NodeMutator from './NodeMutator';
import { IdentifiedNode, Identified } from './IdentifiedNode';
type ConditionExpression = estree.DoWhileStatement | estree.IfStatement | estree.ForStatement | estree.WhileStatement | estree.ConditionalExpression;
/**
* Represents a mutator which can remove the conditional clause from statements.
*/
export default class RemoveConditionalsMutator implements NodeMutator {
public name = 'RemoveConditionals';
private readonly types: string[] = [Syntax.DoWhileStatement, Syntax.IfStatement, Syntax.ForStatement, Syntax.WhileStatement, Syntax.ConditionalExpression];
constructor() { }
public applyMutations(node: IdentifiedNode, copy: (obj: T, deep?: boolean) => T): IdentifiedNode[] | void {
if (this.canMutate(node)) {
const nodes: IdentifiedNode[] = [];
if (node.test) {
nodes.push(this.booleanLiteralNode((node.test as IdentifiedNode).nodeID, false));
} else {
const mutatedNode = copy(node);
mutatedNode.test = this.booleanLiteralNode(-1, false);
nodes.push(mutatedNode);
}
if (node.type === Syntax.IfStatement || node.type === Syntax.ConditionalExpression) {