formatter

package
v0.0.0-...-0137c81 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package formatter provides API for producing pretty-printed source from AST.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(filename string, input string, options Options) (string, error)

Format returns code that is equivalent to its input but better formatted according to the given options.

func FormatNode

func FormatNode(node ast.Node, finalFodder ast.Fodder, options Options) (string, error)

FormatNode returns code that is equivalent to its input but better formatted according to the given options.

func SortImports

func SortImports(file *ast.Node)

SortImports sorts imports at the top of the file into alphabetical order by path.

Top-level imports are `local x = import 'xxx.jsonnet` expressions that go before anything else in the file (more precisely all such imports that are either the root of AST or a direct child (body) of a top-level import. Top-level imports are therefore more top-level than top-level functions.

Grouping of imports is preserved. Groups of imports are separated by blank lines or lines containing comments.

Types

type AddPlusObject

type AddPlusObject struct {
	pass.Base
}

AddPlusObject is a formatter pass that replaces e {} with e + {}.

func (*AddPlusObject) Visit

func (c *AddPlusObject) Visit(p pass.ASTPass, node *ast.Node, ctx pass.Context)

Visit replaces ApplyBrace with Binary node.

type CommentStyle

type CommentStyle int

CommentStyle controls how the reformatter rewrites comments. Comments that look like a #! hashbang are always left alone.

const (
	// CommentStyleHash means #.
	CommentStyleHash CommentStyle = iota
	// CommentStyleSlash means //.
	CommentStyleSlash
	// CommentStyleLeave means comments are left as they are found.
	CommentStyleLeave
)

type EnforceCommentStyle

type EnforceCommentStyle struct {
	pass.Base
	Options Options
	// contains filtered or unexported fields
}

EnforceCommentStyle is a formatter pass that ensures the comments are styled according to the configuration in Options.

func (*EnforceCommentStyle) FodderElement

func (c *EnforceCommentStyle) FodderElement(p pass.ASTPass, element *ast.FodderElement, ctx pass.Context)

FodderElement implements this pass.

type EnforceMaxBlankLines

type EnforceMaxBlankLines struct {
	pass.Base
	Options Options
}

EnforceMaxBlankLines is a formatter pass that ensures there are not too many blank lines in the code.

func (*EnforceMaxBlankLines) FodderElement

func (c *EnforceMaxBlankLines) FodderElement(p pass.ASTPass, element *ast.FodderElement, ctx pass.Context)

FodderElement implements this pass.

type EnforceStringStyle

type EnforceStringStyle struct {
	pass.Base
	Options Options
}

EnforceStringStyle is a formatter pass that manages string literals

func (*EnforceStringStyle) LiteralString

func (c *EnforceStringStyle) LiteralString(p pass.ASTPass, lit *ast.LiteralString, ctx pass.Context)

LiteralString implements this pass.

type FixIndentation

type FixIndentation struct {
	pass.Base

	Options Options
	// contains filtered or unexported fields
}

FixIndentation is a formatter pass that changes the indentation of new line fodder so that it follows the nested structure of the code.

func (*FixIndentation) Visit

func (c *FixIndentation) Visit(expr ast.Node, currIndent indent, crowded bool)

Visit has logic common to all nodes.

func (*FixIndentation) VisitFile

func (c *FixIndentation) VisitFile(body ast.Node, finalFodder ast.Fodder)

VisitFile corrects the whole file including the final fodder.

type FixNewlines

type FixNewlines struct {
	pass.Base
}

FixNewlines is a formatter pass that adds newlines inside complex structures (arrays, objects etc.).

The main principle is that a structure can either be: * expanded and contain newlines in all the designated places * unexpanded and contain newlines in none of the designated places

It only looks shallowly at the AST nodes, so there may be some newlines deeper that don't affect expanding. For example:

[{
    'a': 'b',
    'c': 'd',
}]

The outer array can stay unexpanded, because there are no newlines between the square brackets and the braces.

func (*FixNewlines) Arguments

func (c *FixNewlines) Arguments(p pass.ASTPass, l *ast.Fodder, args *ast.Arguments, r *ast.Fodder, ctx pass.Context)

Arguments handles parameters Example2:

f(1, 2,
  3)

Should be expanded to:

f(1,
  2,
  3)

And:

foo(
    1, 2, 3)

Should be expanded to:

foo(
    1, 2, 3
)

func (*FixNewlines) Array

func (c *FixNewlines) Array(p pass.ASTPass, array *ast.Array, ctx pass.Context)

Array handles this type of node

func (*FixNewlines) ArrayComp

func (c *FixNewlines) ArrayComp(p pass.ASTPass, arrayComp *ast.ArrayComp, ctx pass.Context)

ArrayComp handles this type of node

func (*FixNewlines) Local

func (c *FixNewlines) Local(p pass.ASTPass, local *ast.Local, ctx pass.Context)

Local handles this type of node

func (*FixNewlines) Object

func (c *FixNewlines) Object(p pass.ASTPass, object *ast.Object, ctx pass.Context)

Object handles this type of node

func (*FixNewlines) ObjectComp

func (c *FixNewlines) ObjectComp(p pass.ASTPass, objectComp *ast.ObjectComp, ctx pass.Context)

ObjectComp handles this type of node

func (*FixNewlines) Parameters

func (c *FixNewlines) Parameters(p pass.ASTPass, l *ast.Fodder, params *[]ast.Parameter, r *ast.Fodder, ctx pass.Context)

Parameters handles parameters Example2:

f(1, 2,
  3)

Should be expanded to:

f(1,
  2,
  3)

And:

foo(
    1, 2, 3)

Should be expanded to:

foo(
    1, 2, 3
)

func (*FixNewlines) Parens

func (c *FixNewlines) Parens(p pass.ASTPass, parens *ast.Parens, ctx pass.Context)

Parens handles this type of node

type FixParens

type FixParens struct {
	pass.Base
}

FixParens is a formatter pass that replaces ((e)) with (e).

func (*FixParens) Parens

func (c *FixParens) Parens(p pass.ASTPass, node *ast.Parens, ctx pass.Context)

Parens handles that type of node

type FixTrailingCommas

type FixTrailingCommas struct {
	pass.Base
}

FixTrailingCommas is a formatter pass that ensures trailing commas are present when a list is split over several lines.

func (*FixTrailingCommas) Array

func (c *FixTrailingCommas) Array(p pass.ASTPass, node *ast.Array, ctx pass.Context)

Array handles that type of node

func (*FixTrailingCommas) ArrayComp

func (c *FixTrailingCommas) ArrayComp(p pass.ASTPass, node *ast.ArrayComp, ctx pass.Context)

ArrayComp handles that type of node

func (*FixTrailingCommas) Object

func (c *FixTrailingCommas) Object(p pass.ASTPass, node *ast.Object, ctx pass.Context)

Object handles that type of node

func (*FixTrailingCommas) ObjectComp

func (c *FixTrailingCommas) ObjectComp(p pass.ASTPass, node *ast.ObjectComp, ctx pass.Context)

ObjectComp handles that type of node

type NoRedundantSliceColon

type NoRedundantSliceColon struct {
	pass.Base
}

NoRedundantSliceColon is a formatter pass that preserves fodder in the case of arr[1::] being formatted as arr[1:]

func (*NoRedundantSliceColon) Slice

func (c *NoRedundantSliceColon) Slice(p pass.ASTPass, slice *ast.Slice, ctx pass.Context)

Slice implements this pass.

type Options

type Options struct {
	// Indent is the number of spaces for each level of indenation.
	Indent int
	// MaxBlankLines is the max allowed number of consecutive blank lines.
	MaxBlankLines int
	StringStyle   StringStyle
	CommentStyle  CommentStyle
	// PrettyFieldNames causes fields to only be wrapped in ” when needed.
	PrettyFieldNames bool
	// PadArrays causes arrays to be written like [ this ] instead of [this].
	PadArrays bool
	// PadObjects causes arrays to be written like { this } instead of {this}.
	PadObjects bool
	// SortImports causes imports at the top of the file to be sorted in groups
	// by filename.
	SortImports bool
	// UseImplicitPlus removes plus sign where it is not required.
	UseImplicitPlus bool

	StripEverything     bool
	StripComments       bool
	StripAllButComments bool
}

Options is a set of parameters that control the reformatter's behaviour.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns the recommended formatter behaviour.

type PrettyFieldNames

type PrettyFieldNames struct {
	pass.Base
}

PrettyFieldNames forces minimal syntax with field lookups and definitions

func (*PrettyFieldNames) Index

func (c *PrettyFieldNames) Index(p pass.ASTPass, index *ast.Index, ctx pass.Context)

Index prettifies the definitions.

func (*PrettyFieldNames) ObjectField

func (c *PrettyFieldNames) ObjectField(p pass.ASTPass, field *ast.ObjectField, ctx pass.Context)

ObjectField prettifies the definitions.

type RemovePlusObject

type RemovePlusObject struct {
	pass.Base
}

RemovePlusObject is a formatter pass that replaces ((e)) with (e).

func (*RemovePlusObject) Visit

func (c *RemovePlusObject) Visit(p pass.ASTPass, node *ast.Node, ctx pass.Context)

Visit replaces e + { ... } with an ApplyBrace in some situations.

type StringStyle

type StringStyle int

StringStyle controls how the reformatter rewrites string literals. Strings that contain a ' or a " use the optimal syntax to avoid escaping those characters.

const (
	// StringStyleDouble means "this".
	StringStyleDouble StringStyle = iota
	// StringStyleSingle means 'this'.
	StringStyleSingle
	// StringStyleLeave means strings are left how they were found.
	StringStyleLeave
)

type StripAllButComments

type StripAllButComments struct {
	pass.Base
	// contains filtered or unexported fields
}

StripAllButComments removes all comments and newlines

func (*StripAllButComments) File

func (c *StripAllButComments) File(p pass.ASTPass, node *ast.Node, finalFodder *ast.Fodder)

File replaces the entire file with the remembered comments.

func (*StripAllButComments) Fodder

func (c *StripAllButComments) Fodder(p pass.ASTPass, fodder *ast.Fodder, ctx pass.Context)

Fodder remembers all the fodder in c.comments

type StripComments

type StripComments struct {
	pass.Base
}

StripComments removes all comments

func (*StripComments) Fodder

func (c *StripComments) Fodder(p pass.ASTPass, fodder *ast.Fodder, ctx pass.Context)

Fodder implements this pass.

type StripEverything

type StripEverything struct {
	pass.Base
}

StripEverything removes all comments and newlines

func (*StripEverything) Fodder

func (c *StripEverything) Fodder(p pass.ASTPass, fodder *ast.Fodder, ctx pass.Context)

Fodder implements this pass.

Jump to

Keyboard shortcuts

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