🚀 Socket Launch Week Day 4:Socket MCP Adds Org Alerts, Threat Feed Review, and Package Inspection.Learn more
Sign In

@kubb/ast

Package Overview
Dependencies
Maintainers
1
Versions
173
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@kubb/ast

Spec-agnostic AST layer for Kubb. Defines the node tree, visitor pattern, factory functions, and type guards used across all code generation plugins.

Source
npmnpm
Version
5.0.0-beta.64
Version published
Weekly downloads
84K
18.36%
Maintainers
1
Weekly downloads
 
Created
Source
Kubb banner

npm version npm downloads Stars License Node

Documentation · Report Bug · Request Feature


@kubb/ast

Spec-agnostic AST layer for Kubb

Defines the node tree, visitor pattern, factory functions, and type guards used across every Kubb code generation plugin.

Imports

PathContents
@kubb/astRuntime: node definitions, guards, visitor, macro engine, constants
@kubb/ast/factoryNode constructors (createSchema, createFile, and friends), the ts.factory analogue
@kubb/ast/macrosBuilt-in macro presets: macroDiscriminatorEnum, macroSimplifyUnion, macroEnumName
@kubb/ast/typesTypes only: all node interfaces, type aliases, visitor types
@kubb/ast/utilsSpec-agnostic string and identifier helpers, ref helpers

Node tree

RootNode
├── schemas: SchemaNode[]
└── operations: OperationNode[]
    ├── parameters: ParameterNode[]   → SchemaNode
    ├── requestBody?: SchemaNode
    └── responses: ResponseNode[]     → SchemaNode?

SchemaNode (discriminated union)
  object        → properties: PropertyNode[] → SchemaNode
  array | tuple → items: SchemaNode[]
  union | intersection → members: SchemaNode[]
  enum | ref | string | number | integer | bigint
  boolean | null | any | unknown | void
  date | datetime | time | uuid | email | url | blob

Usage

Factory

Constructors live on the @kubb/ast/factory subpath, mirroring ts.factory.createX. Through @kubb/core the same set is reachable as ast.factory.createSchema(...).

import { createInput, createSchema, createProperty } from '@kubb/ast/factory'

const root = createInput({
  schemas: [
    createSchema({
      name: 'Pet',
      type: 'object',
      properties: [
        createProperty({
          name: 'id',
          schema: createSchema({ type: 'integer' }),
          required: true,
        }),
        createProperty({
          name: 'name',
          schema: createSchema({ type: 'string' }),
          required: true,
        }),
      ],
    }),
  ],
})

Visitor

import { walk, transform, collect } from '@kubb/ast'

// Side effects
await walk(root, {
  schema(node) {
    console.log(node.type)
  },
})

// Immutable transformation
const updated = transform(root, {
  schema(node) {
    return { ...node, description: 'generated' }
  },
})

// Extraction
const types = collect<string>(root, {
  schema(node) {
    return node.type
  },
})

Guards

import { narrowSchema, schemaDef } from '@kubb/ast'
import type { Node } from '@kubb/ast/types'

function process(node: Node) {
  if (schemaDef.is(node)) {
    const obj = narrowSchema(node, 'object')
    obj?.properties?.forEach((p) => console.log(p.name))
  }
}

Refs

import { extractRefName } from '@kubb/ast/utils'

extractRefName('#/components/schemas/Pet') // 'Pet'

Adding a node

Adding a node touches three files. The barrels and visitor tables derive the rest.

  • Define the node in its own src/nodes/*.ts file. Call defineNode and export the resulting fooDef, the createFoo constructor, and the node's type.
  • Add fooDef to the nodeDefs array in src/registry.ts.
  • Re-export createFoo from src/factory.ts.

Everything else follows from there. @kubb/ast/types picks up the node type through export type *, the @kubb/ast barrel picks up fooDef through export * from './registry.ts', and the visitor tables (VISITOR_KEYS, VISITOR_KEY_BY_KIND, nodeRebuilders) come from the def's children, visitorKey, and rebuild fields. registry.test.ts fails when a def has no matching factory.create*, so missing wiring is caught in CI.

Supporting Kubb

Kubb is an open source project, and its development is funded entirely by sponsors. If you would like to become a sponsor, please consider:

My sponsors

License

MIT

Keywords

ast

FAQs

Package last updated on 17 Jun 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts