private dispatch()

in packages/jsii-rosetta/lib/renderer.ts [238:396]


  private dispatch(tree: ts.Node): OTree {
    // Special nodes
    if (ts.isEmptyStatement(tree)) {
      // Additional semicolon where it doesn't belong.
      return NO_SYNTAX;
    }

    const visitor = this.handler;

    // Nodes with meaning
    if (ts.isSourceFile(tree)) {
      return visitor.sourceFile(tree, this);
    }
    if (ts.isImportEqualsDeclaration(tree)) {
      return visitor.importStatement(analyzeImportEquals(tree, this), this);
    }
    if (ts.isImportDeclaration(tree)) {
      return visitor.importStatement(analyzeImportDeclaration(tree, this), this);
    }
    if (ts.isStringLiteral(tree) || ts.isNoSubstitutionTemplateLiteral(tree)) {
      return visitor.stringLiteral(tree, this);
    }
    if (ts.isFunctionDeclaration(tree)) {
      return visitor.functionDeclaration(tree, this);
    }
    if (ts.isIdentifier(tree)) {
      return visitor.identifier(tree, this);
    }
    if (ts.isBlock(tree)) {
      return visitor.block(tree, this);
    }
    if (ts.isParameter(tree)) {
      return visitor.parameterDeclaration(tree, this);
    }
    if (ts.isReturnStatement(tree)) {
      return visitor.returnStatement(tree, this);
    }
    if (ts.isBinaryExpression(tree)) {
      return visitor.binaryExpression(tree, this);
    }
    if (ts.isIfStatement(tree)) {
      return visitor.ifStatement(tree, this);
    }
    if (ts.isPropertyAccessExpression(tree)) {
      return visitor.propertyAccessExpression(tree, this);
    }
    if (ts.isCallExpression(tree)) {
      return visitor.callExpression(tree, this);
    }
    if (ts.isExpressionStatement(tree)) {
      return visitor.expressionStatement(tree, this);
    }
    if (ts.isToken(tree)) {
      return visitor.token(tree, this);
    }
    if (ts.isObjectLiteralExpression(tree)) {
      return visitor.objectLiteralExpression(tree, this);
    }
    if (ts.isNewExpression(tree)) {
      return visitor.newExpression(tree, this);
    }
    if (ts.isPropertyAssignment(tree)) {
      return visitor.propertyAssignment(tree, this);
    }
    if (ts.isVariableStatement(tree)) {
      return visitor.variableStatement(tree, this);
    }
    if (ts.isVariableDeclarationList(tree)) {
      return visitor.variableDeclarationList(tree, this);
    }
    if (ts.isVariableDeclaration(tree)) {
      return visitor.variableDeclaration(tree, this);
    }
    if (ts.isJSDoc(tree)) {
      return visitor.jsDoc(tree, this);
    }
    if (ts.isArrayLiteralExpression(tree)) {
      return visitor.arrayLiteralExpression(tree, this);
    }
    if (ts.isShorthandPropertyAssignment(tree)) {
      return visitor.shorthandPropertyAssignment(tree, this);
    }
    if (ts.isForOfStatement(tree)) {
      return visitor.forOfStatement(tree, this);
    }
    if (ts.isClassDeclaration(tree)) {
      return visitor.classDeclaration(tree, this);
    }
    if (ts.isConstructorDeclaration(tree)) {
      return visitor.constructorDeclaration(tree, this);
    }
    if (ts.isPropertyDeclaration(tree)) {
      return visitor.propertyDeclaration(tree, this);
    }
    if (ts.isComputedPropertyName(tree)) {
      return visitor.computedPropertyName(tree.expression, this);
    }
    if (ts.isMethodDeclaration(tree)) {
      return visitor.methodDeclaration(tree, this);
    }
    if (ts.isInterfaceDeclaration(tree)) {
      return visitor.interfaceDeclaration(tree, this);
    }
    if (ts.isPropertySignature(tree)) {
      return visitor.propertySignature(tree, this);
    }
    if (ts.isMethodSignature(tree)) {
      return visitor.methodSignature(tree, this);
    }
    if (ts.isAsExpression(tree)) {
      return visitor.asExpression(tree, this);
    }
    if (ts.isPrefixUnaryExpression(tree)) {
      return visitor.prefixUnaryExpression(tree, this);
    }
    if (ts.isSpreadAssignment(tree)) {
      if (this.textOf(tree) === '...') {
        return visitor.ellipsis(tree, this);
      }
      return visitor.spreadAssignment(tree, this);
    }
    if (ts.isSpreadElement(tree)) {
      if (this.textOf(tree) === '...') {
        return visitor.ellipsis(tree, this);
      }
      return visitor.spreadElement(tree, this);
    }
    if (ts.isElementAccessExpression(tree)) {
      return visitor.elementAccessExpression(tree, this);
    }
    if (ts.isTemplateExpression(tree)) {
      return visitor.templateExpression(tree, this);
    }
    if (ts.isNonNullExpression(tree)) {
      return visitor.nonNullExpression(tree, this);
    }
    if (ts.isParenthesizedExpression(tree)) {
      return visitor.parenthesizedExpression(tree, this);
    }
    if (ts.isVoidExpression(tree)) {
      return visitor.maskingVoidExpression(tree, this);
    }

    this.reportUnsupported(tree, undefined);

    if (this.options.bestEffort !== false) {
      // When doing best-effort conversion and we don't understand the node type, just return the complete text of it as-is
      return new OTree([this.textOf(tree)]);
    }
    // Otherwise, show a placeholder indicating we don't recognize the type
    const nodeKind = ts.SyntaxKind[tree.kind];
    return new UnknownSyntax(
      [`<${nodeKind} ${this.textOf(tree)}>`],
      ['\n', ...nodeChildren(tree).map(this.convert.bind(this))],
      {
        indent: 2,
      },
    );
  }