All files / utils unwrap.ts

100% Statements 8/8
100% Branches 6/6
100% Functions 3/3
100% Lines 8/8

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          7x             105x   6x   99x             251x 251x 10x   241x    
import { Statement, Block, Node, ts, SyntaxKind } from 'ts-morph';
import { HandlerReturnType } from '../optimization';
 
export function unwrapBlock(target: Statement | Block): string {
  // https://github.com/dsherret/ts-morph/issues/641
  return target.getChildSyntaxListOrThrow().getText({ trimLeadingIndentation: true });
}
 
/**
 * Replace a ParenthesizedExpression from bottom up
 */
export function tryReplaceParentParentheses(node: Node<ts.Node> | undefined): HandlerReturnType {
  if (node && node.getParentIfKind(SyntaxKind.ParenthesizedExpression)) {
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    return node.getParent()!.replaceWithText(node.getText());
  }
  return node;
}
 
/**
 * Unwrap a ParenthesizedExpression from top to bottom
 */
export function tryUnwrapParentheses(node: Node<ts.Node>): Node<ts.Node> {
  const innerExpr = node.asKind(SyntaxKind.ParenthesizedExpression)?.getExpression();
  if (innerExpr) {
    return tryUnwrapParentheses(innerExpr);
  }
  return node;
}