private visitClass()

in packages/jsii-pacmak/lib/generator.ts [519:600]


  private visitClass(cls: spec.ClassType) {
    const initializer = cls.initializer;
    if (initializer) {
      this.onInitializer(cls, initializer);

      // if method has optional arguments and
      for (const overload of this.createOverloadsForOptionals(initializer)) {
        this.onInitializerOverload(cls, overload, initializer);
      }
    }

    // if running in 'pure' mode and the class has methods, emit them as abstract methods.
    if (cls.methods) {
      this.onBeginMethods(cls);
      cls.methods.forEach((method) => {
        if (!method.static) {
          this.onMethod(cls, method);

          for (const overload of this.createOverloadsForOptionals(method)) {
            this.onMethodOverload(cls, overload, method);
          }
        } else {
          this.onStaticMethod(cls, method);

          for (const overload of this.createOverloadsForOptionals(method)) {
            this.onStaticMethodOverload(cls, overload, method);
          }
        }
      });
      this.onEndMethods(cls);
    }

    if (cls.properties) {
      this.onBeginProperties(cls);
      cls.properties.forEach((prop) => {
        if (this.hasField(cls, prop)) {
          this.onField(
            cls,
            prop,
            spec.isUnionTypeReference(prop.type) ? prop.type : undefined,
          );
        }
      });

      cls.properties.forEach((prop) => {
        if (!spec.isUnionTypeReference(prop.type)) {
          if (!prop.static) {
            this.onProperty(cls, prop);
          } else {
            this.onStaticProperty(cls, prop);
          }
        } else {
          // okay, this is a union. some languages support unions (mostly the dynamic ones) and some will need some help
          // if `expandUnionProperties` is set, we will "expand" each property that has a union type into multiple properties
          // and postfix their name with the type name (i.e. FooAsToken).

          // first, emit a property for the union, for languages that support unions.
          this.onUnionProperty(cls, prop, prop.type);

          // if require, we also "expand" the union for languages that don't support unions.
          if (this.options.expandUnionProperties) {
            for (const [index, type] of prop.type.union.types.entries()) {
              // create a clone of this property
              const propClone = clone(prop);
              const primary = this.isPrimaryExpandedUnionProperty(
                prop.type,
                index,
              );
              const propertyName = primary
                ? prop.name
                : `${prop.name}As${this.displayNameForType(type)}`;
              propClone.type = type;
              propClone.optional = prop.optional;
              propClone.name = propertyName;
              this.onExpandedUnionProperty(cls, propClone, prop.name);
            }
          }
        }
      });
      this.onEndProperties(cls);
    }
  }