private extractGoogExports()

in src/ts_migration_exports_shim.ts [250:319]


  private extractGoogExports(exportsExpr: ts.Expression): GoogExports
      |undefined {
    let googExports: GoogExports|undefined;
    const diagnosticCount = this.diagnostics.length;

    if (ts.isObjectLiteralExpression(exportsExpr)) {
      googExports = new Map();
      for (const property of exportsExpr.properties) {
        if (ts.isShorthandPropertyAssignment(property)) {
          // {Bar}
          const symbol =
              this.typeChecker.getShorthandAssignmentValueSymbol(property);
          this.checkIsModuleExport(property.name, symbol);
          googExports.set(property.name.text, property.name.text);
        } else if (ts.isPropertyAssignment(property)) {
          // {Foo: Bar}
          const name = property.name;
          if (!ts.isIdentifier(name)) {
            this.report(name, 'export names must be simple keys');
            continue;
          }

          const initializer = property.initializer;

          let identifier: ts.Identifier|null = null;
          if (ts.isAsExpression(initializer)) {
            identifier = this.maybeExtractTypeName(initializer);
          } else if (ts.isIdentifier(initializer)) {
            identifier = initializer;
          } else {
            this.report(initializer, 'export values must be plain identifiers');
            continue;
          }

          if (identifier == null) {
            continue;
          }

          const symbol = this.typeChecker.getSymbolAtLocation(identifier);
          this.checkIsModuleExport(identifier, symbol);
          googExports.set(name.text, identifier.text);
        } else {
          this.report(
              property,
              `exports object must only contain (shorthand) properties`);
        }
      }
    } else if (ts.isIdentifier(exportsExpr)) {
      const symbol = this.typeChecker.getSymbolAtLocation(exportsExpr);
      this.checkIsModuleExport(exportsExpr, symbol);
      googExports = exportsExpr.text;
    } else if (ts.isAsExpression(exportsExpr)) {
      // {} as DefaultTypeExport
      const identifier = this.maybeExtractTypeName(exportsExpr);
      if (!identifier) {
        return undefined;
      }
      const symbol = this.typeChecker.getSymbolAtLocation(identifier);
      this.checkIsModuleExport(identifier, symbol);
      googExports = identifier.text;
    } else {
      this.report(
          exportsExpr,
          `exports object must be either an object literal ({A, B}) or the ` +
              `identifier of a module export (A)`);
    }

    return (diagnosticCount === this.diagnostics.length) ? googExports :
                                                           undefined;
  }