format

package
v2.932.6 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: AGPL-3.0 Imports: 21 Imported by: 0

README

Formatter Package

This package formats generated source code before it is written to disk. Each language target dispatches to a language-specific formatter. Languages without a formatter pass through unchanged.

Dispatch

Format(target, fileName, data) in format.go routes by target and file extension:

Target Extensions Formatter Runtime
csharp, unity .cs clang-format wazero (WASM/WASI)
typescript, mcp-typescript .ts, .js dprint-plugin-typescript wazero (WASM)
python .py dprint-plugin-ruff wazero (WASM)
java, javav2 .java dprint-plugin-java wazero (WASM)
ruby .rb, .rbi rubyfmt wazero (WASM/WASI)
php .php dprint-plugin-mago (mago) wazero (WASM)
go, cli, mockserver .go go/format (stdlib) native
terraform .tf, .tfvars, .tftest.hcl hclwrite native

All other targets/extensions return data unchanged.

Embedded Binaries

The formatters that run via WASM have their binaries embedded into the Go binary at compile time using //go:embed:

File Source How to rebuild
clangfmt.wasm Built from LLVM source cd formatters/clangfmt && ./build.sh
dprint-plugin-typescript.wasm Pre-built from dprint releases Download from dprint plugin registry
dprint-plugin-ruff.wasm Pre-built from dprint releases Download from dprint plugin registry
dprint-plugin-java.wasm Pre-built from dprint releases Download from dprint plugin registry
dprint-plugin-mago.wasm Pre-built from dprint releases Download from dprint plugin registry
rubyfmt.wasm Built from Rust source cd formatters/rubyfmt && ./build.sh

Formatter Runtimes

wazero (WASM)

wazero is a zero-dependency WebAssembly runtime for Go.

dprint (dprint.go): The TypeScript, Python, Java, and PHP formatters use the dprint plugin ABI. A single WASM module is instantiated per language at startup and reused for all formatting calls. Communication happens through shared memory buffers and exported functions (format, set_file_path, get_formatted_text, etc.).

rubyfmt (rubyfmt.go): The Ruby formatter uses the simpler WASI stdin/stdout model. The WASM module is compiled once at startup, then a fresh instance is created per format call (each needs a unique module name). The Rust WASI binary imports env.__wasi_init_tp which is stubbed as a no-op. WASI proc_exit(0) is treated as success.

Native

Go files use the standard library go/format.Source(). Terraform files use the hclwrite package from HashiCorp (inlined MPL-2.0 code from the Terraform CLI).

WASM Build Tag

Files that depend on wazero are gated with //go:build !js || !wasm. When compiling to WASM itself (e.g. for browser use), dprint_wasm.go provides stub implementations that return data unchanged.

Build Projects

Formatter build tooling lives in the formatters/ directory at the repo root:

  • formatters/rubyfmt/ — Rust/Cargo project for compiling rubyfmt to WASI WASM

Documentation

Index

Constants

View Source
const NoFormatMarker = "__SPEAKEASY_NO_FORMAT__"

NoFormatMarker is the sentinel emitted by the `skipFormat` template function to signal that the rendered file must skip the formatting pipeline. It must appear as the very first bytes of the rendered file. The marker (and its trailing newline, if present) is stripped before the file is written to disk.

Variables

This section is empty.

Functions

func Format

func Format(ctx context.Context, target types.Target, fileName string, data []byte) ([]byte, error)

func SetFormattingEnabled

func SetFormattingEnabled(target string, enabled bool)

SetFormattingEnabled enables or disables formatting for a specific target. Used to gate formatting behind a gen.yaml config flag.

func ShouldSkipFormat

func ShouldSkipFormat(data []byte) (bool, []byte)

Types

This section is empty.

Jump to

Keyboard shortcuts

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