Documentation
¶
Overview ¶
Package format implements formatting of Rego source files.
Index ¶
- func ArityFormatMismatchError(operands []*ast.Term, operator string, loc *ast.Location, f *types.Function) *ast.Error
- func Ast(x any) ([]byte, error)
- func AstWithOpts(x any, opts Opts) ([]byte, error)
- func MustAst(x any) []byte
- func MustAstWithOpts(x any, opts Opts) []byte
- func Source(filename string, src []byte) ([]byte, error)
- func SourceWithOpts(filename string, src []byte, opts Opts) ([]byte, error)
- type ArityFormatErrDetail
- type Opts
- type UnformattedError
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 ¶
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 MustAst ¶
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 ¶
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
Types ¶
type ArityFormatErrDetail ¶
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
// RegoVersionSpan is the span of Rego versions the formatted code must parse at: [v0, v2] for code
// compatible with both Rego v0 and v2, [v2, v2] for Rego v2 only. A span reaching v2 from an older
// version adds the 'rego.v2' import. If defined, it takes precedence over [Opts.RegoVersion].
RegoVersionSpan ast.RegoVersionSpan
// ParserOptions is the parser options used when parsing the module to be formatted.
ParserOptions *ast.ParserOptions
// DropRedundantDirectiveImports instructs the formatter to drop directive imports, which change how
// a module is parsed rather than bring anything into scope: 'rego.*' and 'future.*'.
// Imports are only removed if [Opts.RegoVersion] or [Opts.RegoVersionSpan] makes them redundant.
// Regardless of this option, 'rego.v1' is dropped from a module that imports 'rego.v2'.
DropRedundantDirectiveImports bool
// DropV0Imports is equivalent to [Opts.DropRedundantDirectiveImports]; either setting drops the imports.
//
// Deprecated: Use [Opts.DropRedundantDirectiveImports] instead.
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
// FailFast instructs the formatter to stop at the first element whose formatted form differs from the
// source text, and return an [UnformattedError] instead of laying out the rest of the module.
// Only the Source* functions honour this option, as they alone have source text to compare against.
// Not returning an [UnformattedError] doesn't promise that the output matches the source: FailFast never
// reports a formatted module as unformatted, but it doesn't catch every difference either.
FailFast bool
Capabilities *ast.Capabilities
// contains filtered or unexported fields
}
Opts lets you control the code formatting via `AstWithOpts()`.
type UnformattedError ¶ added in v1.22.0
UnformattedError is returned by the Source* functions when Opts.FailFast is set and the formatted output differs from the source. Loc is the element being written when the difference was noticed.
func (UnformattedError) Error ¶ added in v1.22.0
func (e UnformattedError) Error() string