Help us shape the future of Go! Take the 2026 Go Developer Survey

format

package
v1.21.2 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 11 Imported by: 5

Documentation

Overview

Package format implements formatting of Rego source files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArityFormatMismatchError

func ArityFormatMismatchError(operands []*ast.Term, operator string, loc *ast.Location, f *types.Function) *ast.Error

ArityFormatMismatchError but for `fmt` checks since the compiler has not run yet.

func Ast

func Ast(x any) ([]byte, error)

Ast formats a Rego AST element. If the passed value is not a valid AST element, Ast returns nil and an error. If AST nodes are missing locations an arbitrary location will be used.

func AstWithOpts

func AstWithOpts(x any, opts Opts) ([]byte, error)

func MustAst

func MustAst(x any) []byte

MustAst is a helper function to format a Rego AST element. If any errors occur this function will panic. This is mostly used for test

func MustAstWithOpts

func MustAstWithOpts(x any, opts Opts) []byte

MustAstWithOpts is a helper function to format a Rego AST element. If any errors occur this function will panic. This is mostly used for test

func Source

func Source(filename string, src []byte) ([]byte, error)

Source formats a Rego source file. The bytes provided must describe a complete Rego module. If they don't, Source will return an error resulting from the attempt to parse the bytes.

func SourceWithOpts

func SourceWithOpts(filename string, src []byte, opts Opts) ([]byte, error)

Types

type ArityFormatErrDetail

type ArityFormatErrDetail struct {
	Have []string `json:"have"`
	Want []string `json:"want"`
}

ArityFormatErrDetail but for `fmt` checks since compiler has not run yet.

func (*ArityFormatErrDetail) Lines

func (d *ArityFormatErrDetail) Lines() []string

Lines returns the string representation of the detail.

type Opts

type Opts struct {
	// IgnoreLocations instructs the formatter not to use the AST nodes' locations
	// into account when laying out the code: notably, when the input is the result
	// of partial evaluation, arguments maybe have been shuffled around, but still
	// carry along their original source locations.
	IgnoreLocations bool

	// RegoVersion is the version of Rego to format code for.
	RegoVersion ast.RegoVersion

	// ParserOptions is the parser options used when parsing the module to be formatted.
	ParserOptions *ast.ParserOptions

	// DropV0Imports instructs the formatter to drop all v0 imports from the module; i.e. 'rego.v1' and 'future.keywords' imports.
	// Imports are only removed if [Opts.RegoVersion] makes them redundant.
	DropV0Imports bool

	// SkipDefensiveCopying, if true, will avoid deep-copying the AST before formatting it.
	// This is true by default for all Source* functions, but false by default for Ast* functions,
	// as some formatting operations may otherwise mutate the AST.
	SkipDefensiveCopying bool

	Capabilities *ast.Capabilities
}

Opts lets you control the code formatting via `AstWithOpts()`.

Jump to

Keyboard shortcuts

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