build

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package build is a fluent construction layer over the ast package.

iface := build.Interface("User").
    Doc("A user of the system.").
    Extends("Entity").
    Prop("id", ast.String).
    Prop("nickname", ast.String, build.Optional).
    Build()

Every builder returns plain AST nodes, so builders and literal nodes mix freely. Per-member settings are option values rather than chained calls, which keeps the chain flat and lets consumers define their own.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Aliased

func Aliased(name, alias string) ast.ImportSpec

Aliased returns a named specifier with a local alias.

func Fn

func Fn(fn func(*SigBuilder)) *ast.FunctionType

Fn returns a function type built from a signature builder.

func Named

func Named(name string) ast.ImportSpec

Named returns a named import or export specifier.

Types

type AliasBuilder

type AliasBuilder struct {
	// contains filtered or unexported fields
}

AliasBuilder builds a type alias declaration.

func Alias

func Alias(name string, t ast.Type) *AliasBuilder

Alias starts a type alias declaration.

func AliasOf

func AliasOf[T any](bd *bind.Binder, name string) *AliasBuilder

AliasOf returns an exported type alias binding name to the projection of T.

func (*AliasBuilder) Build

func (b *AliasBuilder) Build() *ast.TypeAliasDecl

Build returns the declaration.

func (*AliasBuilder) Doc

func (b *AliasBuilder) Doc(lines ...string) *AliasBuilder

Doc attaches a JSDoc comment.

func (*AliasBuilder) Export

func (b *AliasBuilder) Export() *AliasBuilder

Export marks the alias exported.

func (*AliasBuilder) TypeParam

func (b *AliasBuilder) TypeParam(name string, opts ...TypeParamOption) *AliasBuilder

TypeParam appends a generic type parameter.

type FileBuilder

type FileBuilder struct {
	// contains filtered or unexported fields
}

FileBuilder assembles a source file.

func File

func File(name string) *FileBuilder

File starts a source file.

func (*FileBuilder) Add

func (b *FileBuilder) Add(stmts ...ast.Stmt) *FileBuilder

Add appends statements.

func (*FileBuilder) AddDecls

func (b *FileBuilder) AddDecls(ds ...ast.Decl) *FileBuilder

AddDecls appends declarations.

func (*FileBuilder) Build

func (b *FileBuilder) Build() *ast.SourceFile

Build returns the source file.

func (*FileBuilder) DeclareOf

func (b *FileBuilder) DeclareOf[T any](bd *bind.Binder) *FileBuilder

DeclareOf projects T and appends every declaration it implied to the file.

build.File("models.ts").DeclareOf[User](b).DeclareOf[Invoice](b).Build()

func (*FileBuilder) Dts

func (b *FileBuilder) Dts() *FileBuilder

Dts marks the file as a declaration file.

func (*FileBuilder) ExportDefault

func (b *FileBuilder) ExportDefault(e ast.Expr) *FileBuilder

ExportDefault appends an export default statement.

func (*FileBuilder) Import

func (b *FileBuilder) Import(module string, specs ...ast.ImportSpec) *FileBuilder

Import appends a named import.

func (*FileBuilder) ImportDefault

func (b *FileBuilder) ImportDefault(module, name string) *FileBuilder

ImportDefault appends a default import.

func (*FileBuilder) ImportNamespace

func (b *FileBuilder) ImportNamespace(module, name string) *FileBuilder

ImportNamespace appends a namespace import.

func (*FileBuilder) ImportType

func (b *FileBuilder) ImportType(module string, specs ...ast.ImportSpec) *FileBuilder

ImportType appends a type-only import, which erases completely at build time.

func (*FileBuilder) Leading

func (b *FileBuilder) Leading(lines ...string) *FileBuilder

Leading adds file-level comment lines.

func (*FileBuilder) TSX

func (b *FileBuilder) TSX() *FileBuilder

TSX marks the file as containing JSX syntax.

type FuncBuilder

type FuncBuilder struct {
	// contains filtered or unexported fields
}

FuncBuilder builds a function declaration.

func Func

func Func(name string) *FuncBuilder

Func starts a function declaration. With no body it prints as an ambient signature, as required in a .d.ts.

func (*FuncBuilder) Async

func (b *FuncBuilder) Async() *FuncBuilder

Async marks the function async.

func (*FuncBuilder) Body

func (b *FuncBuilder) Body(stmts ...ast.Stmt) *FuncBuilder

Body sets the function body.

func (*FuncBuilder) Build

func (b *FuncBuilder) Build() *ast.FunctionDecl

Build returns the declaration.

func (*FuncBuilder) Declare

func (b *FuncBuilder) Declare() *FuncBuilder

Declare marks the function ambient.

func (*FuncBuilder) Doc

func (b *FuncBuilder) Doc(lines ...string) *FuncBuilder

Doc attaches a JSDoc comment.

func (*FuncBuilder) Export

func (b *FuncBuilder) Export() *FuncBuilder

Export marks the function exported.

func (*FuncBuilder) Param

func (b *FuncBuilder) Param(name string, t ast.Type, opts ...ParamOption) *FuncBuilder

Param appends a parameter.

func (*FuncBuilder) ParamOf

func (b *FuncBuilder) ParamOf[T any](bd *bind.Binder, name string, opts ...ParamOption) *FuncBuilder

ParamOf appends a parameter whose type is projected from the Go type T.

func (*FuncBuilder) Returns

func (b *FuncBuilder) Returns(t ast.Type) *FuncBuilder

Returns sets the return type.

func (*FuncBuilder) ReturnsOf

func (b *FuncBuilder) ReturnsOf[T any](bd *bind.Binder) *FuncBuilder

ReturnsOf sets the return type from the Go type T.

func (*FuncBuilder) TypeParam

func (b *FuncBuilder) TypeParam(name string, opts ...TypeParamOption) *FuncBuilder

TypeParam appends a generic type parameter.

type InterfaceBuilder

type InterfaceBuilder struct {
	// contains filtered or unexported fields
}

InterfaceBuilder builds an interface declaration.

func Interface

func Interface(name string) *InterfaceBuilder

Interface starts an interface declaration.

func (*InterfaceBuilder) Build

func (b *InterfaceBuilder) Build() *ast.InterfaceDecl

Build returns the declaration.

func (*InterfaceBuilder) Call

func (b *InterfaceBuilder) Call(fn func(*SigBuilder)) *InterfaceBuilder

Call appends a bare call signature.

func (*InterfaceBuilder) Clone

func (b *InterfaceBuilder) Clone() *InterfaceBuilder

Clone returns an independent copy, so a partially built interface can serve as a template.

func (*InterfaceBuilder) Declare

func (b *InterfaceBuilder) Declare() *InterfaceBuilder

Declare marks the interface ambient.

func (*InterfaceBuilder) Doc

func (b *InterfaceBuilder) Doc(lines ...string) *InterfaceBuilder

Doc attaches a JSDoc comment.

func (*InterfaceBuilder) Export

func (b *InterfaceBuilder) Export() *InterfaceBuilder

Export marks the interface exported.

func (*InterfaceBuilder) Extends

func (b *InterfaceBuilder) Extends(name string, args ...ast.Type) *InterfaceBuilder

Extends appends an extends clause entry.

func (*InterfaceBuilder) ExtendsOf

func (b *InterfaceBuilder) ExtendsOf[T any](bd *bind.Binder) *InterfaceBuilder

ExtendsOf appends an extends clause entry referencing the Go type T. It is a no-op if T does not project to a named type.

func (*InterfaceBuilder) Index

func (b *InterfaceBuilder) Index(keyName string, keyType, valType ast.Type) *InterfaceBuilder

Index appends an index signature.

func (*InterfaceBuilder) Members

func (b *InterfaceBuilder) Members(ms ...ast.Member) *InterfaceBuilder

Members appends arbitrary members, the escape hatch to hand-built nodes.

func (*InterfaceBuilder) Method

func (b *InterfaceBuilder) Method(name string, fn func(*SigBuilder)) *InterfaceBuilder

Method appends a method signature, configured through a signature builder.

func (*InterfaceBuilder) Prop

func (b *InterfaceBuilder) Prop(name string, t ast.Type, opts ...PropOption) *InterfaceBuilder

Prop appends a property signature.

func (*InterfaceBuilder) PropOf

func (b *InterfaceBuilder) PropOf[T any](bd *bind.Binder, name string, opts ...PropOption) *InterfaceBuilder

PropOf appends a property whose type is projected from the Go type T.

func (*InterfaceBuilder) Tag

func (b *InterfaceBuilder) Tag(name, text string) *InterfaceBuilder

Tag appends a JSDoc block tag.

func (*InterfaceBuilder) TypeParam

func (b *InterfaceBuilder) TypeParam(name string, opts ...TypeParamOption) *InterfaceBuilder

TypeParam appends a generic type parameter.

type ParamOption

type ParamOption func(*ast.Param)

ParamOption customizes a function parameter.

var OptionalParam ParamOption = func(p *ast.Param) { p.Optional = true }

OptionalParam marks a parameter optional.

var Rest ParamOption = func(p *ast.Param) { p.Rest = true }

Rest marks a parameter as rest.

func DefaultValue

func DefaultValue(e ast.Expr) ParamOption

DefaultValue sets a parameter's default expression.

type PropOption

type PropOption func(*ast.PropertySignature)

PropOption customizes a property signature.

var Optional PropOption = func(p *ast.PropertySignature) { p.Optional = true }

Optional marks a property optional.

var Readonly PropOption = func(p *ast.PropertySignature) { p.Readonly = true }

Readonly marks a property readonly.

func Deprecated

func Deprecated(reason string) PropOption

Deprecated marks a property deprecated.

func PropDoc

func PropDoc(lines ...string) PropOption

PropDoc attaches a JSDoc comment to a property.

func PropTag

func PropTag(name, text string) PropOption

PropTag attaches a JSDoc block tag to a property.

type SigBuilder

type SigBuilder struct {
	// contains filtered or unexported fields
}

SigBuilder builds the callable part of a function, method, or constructor.

func (*SigBuilder) Doc

func (s *SigBuilder) Doc(lines ...string) *SigBuilder

Doc attaches a JSDoc comment.

func (*SigBuilder) Param

func (s *SigBuilder) Param(name string, t ast.Type, opts ...ParamOption) *SigBuilder

Param appends a parameter.

func (*SigBuilder) ParamOf

func (s *SigBuilder) ParamOf[T any](bd *bind.Binder, name string, opts ...ParamOption) *SigBuilder

ParamOf appends a parameter whose type is projected from the Go type T.

func (*SigBuilder) Returns

func (s *SigBuilder) Returns(t ast.Type) *SigBuilder

Returns sets the return type.

func (*SigBuilder) ReturnsOf

func (s *SigBuilder) ReturnsOf[T any](bd *bind.Binder) *SigBuilder

ReturnsOf sets the return type from the Go type T.

func (*SigBuilder) Signature

func (s *SigBuilder) Signature() ast.Signature

Signature returns the built signature.

func (*SigBuilder) TypeParam

func (s *SigBuilder) TypeParam(name string, opts ...TypeParamOption) *SigBuilder

TypeParam appends a generic type parameter.

type TypeParamOption

type TypeParamOption func(*ast.TypeParam)

TypeParamOption customizes a type parameter.

var (
	In    TypeParamOption = func(p *ast.TypeParam) { p.Variance = ast.VarianceIn }
	Out   TypeParamOption = func(p *ast.TypeParam) { p.Variance = ast.VarianceOut }
	InOut TypeParamOption = func(p *ast.TypeParam) { p.Variance = ast.VarianceInOut }
)

In, Out, and InOut set explicit variance annotations.

var Const TypeParamOption = func(p *ast.TypeParam) { p.Const = true }

Const marks a const type parameter.

func Constraint

func Constraint(t ast.Type) TypeParamOption

Constraint sets a type parameter's extends clause.

func Default

func Default(t ast.Type) TypeParamOption

Default sets a type parameter's default.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL