Skip to content

hoistAnonymousTypes

Import from @varavel/vdl-plugin-sdk/utils/ir.

function hoistAnonymousTypes(schema, nameFn?): IrSchema;

Hoists anonymous inline object types into generated top-level TypeDefs.

This helper converts nested anonymous object types into named top-level IR types.

It exists for plugin authors who want a flatter, easier-to-generate type graph without changing the compiler's canonical IR format.

In other words: the compiler can keep emitting faithful nested IR, and a plugin can call this helper when it wants to treat inline object shapes as "syntactic sugar" and work only with named types.

What the helper does:

  • walks schema.types
  • finds anonymous inline objects nested inside those top-level types
  • creates a new synthetic top-level TypeDef for each one
  • replaces the original inline object with a kind: "type" reference to the generated type name
  • repeats the same process recursively for deeper nested objects
  • also hoists anonymous objects that appear inside arrays and maps
  • never mutates the input schema

Default naming behavior:

  • nested field objects become names like MyRpcBarProcInput
  • anonymous array item objects end with Item
  • anonymous map value objects end with Value
  • name conflicts are resolved automatically with 2, 3, and so on

Scope and non-goals:

  • only anonymous objects reachable from schema.types are hoisted
  • constants, enums, docs, and already named type references are left alone
  • identical object shapes are not deduplicated; each anonymous occurrence is treated as its own generated type

This makes the function especially useful for languages or generators that do not want to preserve nested inline object syntax in the output.

Parameters

Parameter Type Description
schema IrSchema IR schema to transform.
nameFn? (context) => string Optional callback that returns the base name to use for each generated type before uniqueness suffixes are applied.

Returns

IrSchema

A new schema where nested anonymous object types have been hoisted to top-level named types.

Examples

const flatSchema = ir.hoistAnonymousTypes(schema);

flatSchema.types.map((typeDef) => typeDef.name);
// ['FooType', 'MyRpc', 'MyRpcBarProc', 'MyRpcBarProcInput', 'MyRpcBarProcOutput']
const flatSchema = ir.hoistAnonymousTypes(schema, ({ defaultName }) => {
  return `${defaultName}Dto`;
});

// Example:
// Request.payload { ... }
// becomes a generated type named RequestPayloadDto
// Before hoisting:
// type Api {
//   request {
//     payload {
//       id string
//     }
//   }
// }

const flatSchema = ir.hoistAnonymousTypes(schema);

// After hoisting, the plugin can work with top-level names like:
// - Api
// - ApiRequest
// - ApiRequestPayload