public toJson()

in src/docgen/transpile/transpile.ts [423:485]


  public toJson(): TypeSchema {
    if (this.primitive) {
      return {
        formattingPattern: this.primitive,
      };
    }

    if (this.type) {
      if (!this.ref.fqn) {
        throw new Error(`Original type reference for ${this.type.fqn} does not have a fqn.`);
      }

      // If we are running in a test, report a fake stable version since types
      // may belong to dependency packages, which could be specified with caret
      // dependencies - this means the packageVersion of a type like
      // `construct.Construct` will be set to whichever version is installed.
      // This is okay in practice, but makes snapshot tests inconsistent.
      const IS_TEST_RUN = process.env.NODE_ENV === 'test';
      const packageVersion = IS_TEST_RUN ? '99.99.99' : this.type.source.assembly.version;

      return {
        formattingPattern: '%',
        types: [
          filterUndefined({
            id: this.ref.fqn,
            displayName: this.type.name,
            fqn: this.type.fqn,
            packageName: this.type.source.assembly.name,
            packageVersion,
            submodule: this.type.submodulePath,
          }),
        ],
      };
    }

    if (this.isAny) {
      return {
        formattingPattern: this.transpile.any(),
      };
    }

    if (this.arrayOfType) {
      return {
        formattingPattern: this.transpile.listOf('%'),
        types: [this.arrayOfType.toJson()],
      };
    }
    if (this.mapOfType) {
      return {
        formattingPattern: this.transpile.mapOf('%'),
        types: [this.mapOfType.toJson()],
      };
    }
    if (this.unionOfTypes) {
      const inner = Array(this.unionOfTypes.length).fill('%');
      return {
        formattingPattern: this.transpile.unionOf(inner),
        types: this.unionOfTypes.map((t) => t.toJson()),
      };
    }

    throw new Error(`Invalid type reference: ${this.ref.toString()}`);
  }