printer

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package printer renders the AST to TypeScript source text.

Parentheses are inserted wherever operator precedence requires them, so callers never need to reason about precedence when building trees.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fprint

func Fprint(w io.Writer, n ast.Node, opts ...Option) error

Fprint renders n to w.

func Print

func Print(n ast.Node, opts ...Option) string

Print renders n and returns the resulting source text.

Types

type MemberSeparator

type MemberSeparator uint8

MemberSeparator selects the punctuation between object type members.

const (
	SemicolonSeparator MemberSeparator = iota
	CommaSeparator
	NewlineSeparator // no punctuation; members separated by line breaks only
)

type Option

type Option func(*Options)

Option mutates Options. Options compose.

func GeneratedBanner

func GeneratedBanner(by string) Option

GeneratedBanner returns the conventional header for generated files.

func WithBanner

func WithBanner(lines ...string) Option

WithBanner sets the file header lines, which are emitted verbatim.

func WithDocs

func WithDocs(b bool) Option

WithDocs enables or disables JSDoc emission.

func WithIndent

func WithIndent(s string) Option

WithIndent sets one level of indentation.

func WithMaxLineWidth

func WithMaxLineWidth(n int) Option

WithMaxLineWidth sets the wrapping column; zero disables wrapping.

func WithMemberSeparator

func WithMemberSeparator(s MemberSeparator) Option

WithMemberSeparator sets the interface member separator.

func WithQuote

func WithQuote(q QuoteStyle) Option

WithQuote sets the string literal quote style.

func WithSemicolons

func WithSemicolons(b bool) Option

WithSemicolons enables or disables statement terminators.

func WithTabs

func WithTabs() Option

WithTabs indents with tabs.

func WithTrailingComma

func WithTrailingComma(b bool) Option

WithTrailingComma enables or disables trailing commas in wrapped lists.

type Options

type Options struct {
	// Indent is one level of indentation. Use "\t" for tabs.
	Indent string

	// Quote is the quote character for string literals.
	Quote QuoteStyle

	// Semicolons emits statement terminators.
	Semicolons bool

	// MemberSep is the separator between interface and object type members.
	MemberSep MemberSeparator

	// TrailingComma emits a trailing comma in multi-line lists.
	TrailingComma bool

	// MaxLineWidth is the column at which unions, parameter lists, and object
	// literals break across lines. Zero disables wrapping entirely.
	MaxLineWidth int

	// EmitDocs emits JSDoc comments attached to declarations and members.
	EmitDocs bool

	// Banner is emitted at the top of a SourceFile, before its own Leading
	// comments. Each entry becomes one line; the caller supplies any comment
	// syntax. This is where a "generated, do not edit" header belongs.
	Banner []string

	// FinalNewline terminates the output with a line break.
	FinalNewline bool
}

Options controls emission. The zero value is not the default; use Default or pass Option values to New.

func Default

func Default() Options

Default returns the recommended configuration: two-space indent, single quotes, semicolons, docs on, wrapping at 100 columns.

type Printer

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

Printer renders AST nodes to TypeScript source.

A Printer is single-use and not safe for concurrent use; prefer the package-level Print and Fprint.

func New

func New(opts ...Option) *Printer

New returns a Printer configured from Default and the supplied options.

func NewWith

func NewWith(o Options) *Printer

NewWith returns a Printer using o verbatim.

func (*Printer) Print

func (p *Printer) Print(n ast.Node) string

Print renders n using this Printer's configuration and returns the text.

func (*Printer) Type

func (p *Printer) Type(t ast.Type)

Type emits a type at the loosest precedence.

type QuoteStyle

type QuoteStyle uint8

QuoteStyle selects the quote character for emitted string literals.

const (
	SingleQuote QuoteStyle = iota
	DoubleQuote
	BacktickQuote
)

Jump to

Keyboard shortcuts

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