tsbuf

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package tsbuf builds one TypeScript file.

It is github.com/simonjanss/rig/internal/gen/gobuf for TypeScript, and the differences are the ones the language forces. There is no gofmt here: Go generators can emit approximately correct source and let the formatter settle the layout, and nothing in the Go toolchain formats TypeScript. So this emits what it means, already laid out — four spaces, one import per module, a trailing comma where Prettier would put one — and the output is compared against a golden file rather than against a formatter.

The other difference is imports. A Go import is a path and a package name; a TypeScript import is a module specifier and a set of names, and the same module can be imported twice over — once for values and once for types. So Buf.Import and Buf.ImportType collect names per module and the two are emitted as separate statements, because `import type` is what tells a bundler the import disappears at build time.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ident

func Ident(s string) bool

Ident reports whether a name can be written bare as an object key, or has to be quoted. A column called `order` is fine; one called `full-name` is not.

func Key

func Key(s string) string

Key renders a name as an object or interface key, quoting it when it is not a bare identifier.

func Quote

func Quote(s string) string

Quote renders a string as a TypeScript literal.

Double quotes, because that is what Prettier's default produces and the generated files sit beside hand-written ones that went through it.

Types

type Buf

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

Buf accumulates one file.

func New

func New() *Buf

New builds a buffer for a generated file, which carries the do-not-edit banner.

func NewHandOwned

func NewHandOwned() *Buf

NewHandOwned builds a buffer for a file a developer owns, which carries no banner and is written once.

func (*Buf) Bytes

func (b *Buf) Bytes() ([]byte, error)

Bytes renders the file.

func (*Buf) Comment

func (b *Buf) Comment(text string)

Comment writes a JSDoc block, wrapped.

A block rather than `//`, because this is the form an editor shows on hover and the form TypeDoc reads: a description the document carries is documentation, and a `//` comment above a member is invisible to everything that would display it.

func (*Buf) Doc

func (b *Buf) Doc(text string)

Doc sets the file's leading comment, emitted above the imports.

func (*Buf) Import

func (b *Buf) Import(module, name string) string

Import records a value import and returns the name, so a caller can write the name it was given rather than the one it asked for.

func (*Buf) ImportType

func (b *Buf) ImportType(module, name string) string

ImportType records a type-only import. Separate from Buf.Import because `import type` is what says the import is erased at build time — and a type imported as a value is a runtime dependency on a module that may have no runtime at all.

func (*Buf) Indent

func (b *Buf) Indent()

Indent shifts everything written after it one level right.

func (*Buf) L

func (b *Buf) L(format string, args ...any)

L writes one line at the current indentation.

func (*Buf) NL

func (b *Buf) NL()

NL writes a blank line.

func (*Buf) Outdent

func (b *Buf) Outdent()

Outdent undoes one Buf.Indent.

func (*Buf) P

func (b *Buf) P(format string, args ...any)

P writes without ending the line, for a line assembled in pieces.

Jump to

Keyboard shortcuts

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