function wrapWithRethrow()

in src/transforms/deprecation-warnings.ts [737:783]


function wrapWithRethrow(statements: ts.Statement[], caller: ts.Expression): ts.Statement[] {
  if (statements.length === 0) {
    return statements;
  }
  return [
    ts.factory.createTryStatement(
      ts.factory.createBlock(statements),
      ts.factory.createCatchClause(
        ts.factory.createVariableDeclaration('error'),
        ts.factory.createBlock([
          // If this is a DeprecationError, trim its stack trace to surface level before re-throwing,
          // so we don't carry out possibly confusing frames from injected code. That can be toggled
          // off by setting JSII_DEBUG=1, so we can also diagnose in-injected code faults.
          ts.factory.createIfStatement(
            ts.factory.createBinaryExpression(
              ts.factory.createBinaryExpression(
                ts.factory.createPropertyAccessExpression(
                  ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('process'), 'env'),
                  'JSII_DEBUG',
                ),
                ts.SyntaxKind.ExclamationEqualsEqualsToken,
                ts.factory.createStringLiteral('1'),
              ),
              ts.SyntaxKind.AmpersandAmpersandToken,
              ts.factory.createBinaryExpression(
                ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('error'), 'name'),
                ts.SyntaxKind.EqualsEqualsEqualsToken,
                ts.factory.createStringLiteral(DEPRECATION_ERROR),
              ),
            ),
            ts.factory.createBlock([
              ts.factory.createExpressionStatement(
                ts.factory.createCallExpression(
                  ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('Error'), 'captureStackTrace'),
                  undefined,
                  [ts.factory.createIdentifier('error'), caller],
                ),
              ),
            ]),
          ),
          ts.factory.createThrowStatement(ts.factory.createIdentifier('error')),
        ]),
      ),
      undefined,
    ),
  ];
}