All files / optimization optimize.ts

100% Statements 12/12
100% Branches 9/9
100% Functions 2/2
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35                        1x       24x   23x   8x 8x   7x 7x   4x 4x     23x 10x      
/*
 * @copyright Microsoft Corporation. All rights reserved.
 */
 
import { BinaryExpression, ConditionalExpression, IfStatement, Node, SyntaxKind, ts } from 'ts-morph';
import { handleBinaryExp } from './binaryExp';
import { handleConditionalExp } from './conditionalExp';
import { handleIf } from './if';
 
export type HandlerReturnType = Node<ts.Node> | undefined;
 
export function optimize(nodes: Set<Node<ts.Node>>) {
  nodes.forEach(optimizeNode);
}
 
export function optimizeNode(node: Node<ts.Node> | undefined) {
  if (!node || node.wasForgotten()) return;
  let newWork: HandlerReturnType;
  switch (node.getKind()) {
  case SyntaxKind.BinaryExpression:
    newWork = handleBinaryExp(node as BinaryExpression);
    break;
  case SyntaxKind.ConditionalExpression:
    newWork = handleConditionalExp(node as ConditionalExpression);
    break;
  case SyntaxKind.IfStatement:
    handleIf(node as IfStatement);
    break;
  }
  // if there's any optimization happened
  if (newWork) {
    optimizeNode(newWork.getParent());
  }
}