public objectLiteralExpression()

in src/languages/default.ts [159:196]


  public objectLiteralExpression(node: ts.ObjectLiteralExpression, context: AstRenderer<C>): OTree {
    // If any of the elements of the objectLiteralExpression are not a literal property
    // assignment, report them. We can't support those.
    const unsupported = node.properties.filter(
      (p) => !ts.isPropertyAssignment(p) && !ts.isShorthandPropertyAssignment(p),
    );
    for (const unsup of unsupported) {
      context.report(unsup, `Use of ${ts.SyntaxKind[unsup.kind]} in an object literal is not supported.`);
    }

    const anyMembersFunctions = node.properties.some((p) =>
      ts.isPropertyAssignment(p)
        ? isExpressionOfFunctionType(context.typeChecker, p.initializer)
        : ts.isShorthandPropertyAssignment(p)
        ? isExpressionOfFunctionType(context.typeChecker, p.name)
        : false,
    );

    const inferredType = inferredTypeOfExpression(context.typeChecker, node);
    if ((inferredType && isJsiiProtocolType(context.typeChecker, inferredType)) || anyMembersFunctions) {
      context.report(
        node,
        `You cannot use an object literal to make an instance of an interface. Define a class instead.`,
      );
    }

    const lit = analyzeObjectLiteral(context.typeChecker, node);

    switch (lit.kind) {
      case 'unknown':
        return this.unknownTypeObjectLiteralExpression(node, context);
      case 'struct':
      case 'local-struct':
        return this.knownStructObjectLiteralExpression(node, lit, context);
      case 'map':
        return this.keyValueObjectLiteralExpression(node, context);
    }
  }