internal

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package internal contains the internals of gosrt.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FilterFileComments

func FilterFileComments(
	file *ast.File,
	filter func(*ast.CommentGroup) bool,
) (removed []*ast.CommentGroup)

FilterFileComments removes each comment from a file’s AST for which the supplied filter returns true. The removed comments are returned.

func FilterFileDecls

func FilterFileDecls(file *ast.File, filter func(ast.Decl) bool)

FilterFileDecls removes each declaration from a file’s AST for which the supplied filter returns true.

func Within

func Within(inner, outer ast.Node) bool

Within returns true if an AST node is within an other node.

Types

type BaseFile

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

BaseFile has common fields and methods for file-like things.

func (*BaseFile) After

func (file *BaseFile) After() []byte

After returns the file’s contents after it is transformed.

func (*BaseFile) Changed

func (file *BaseFile) Changed() bool

Changed returns true if the file changed when it was transformed.

func (*BaseFile) Diff

func (file *BaseFile) Diff(ctx context.Context, now time.Time) (string, error)

Diff uses diff(1) to print the differences between the original file and the same file transformed.

Example
if err := os.Setenv("TZ", "UTC0"); err != nil {
	panic(err)
}

file := &BaseFile{
	after: []byte(`foo
baz
`),
	before: []byte(`foo
bar
`),
	mtime: time.Unix(0, 0),
	name:  "example",
}

diff, err := file.Diff(context.Background(), file.mtime)
if err != nil {
	panic(err)
}

fmt.Println(diff)

//nolint:godot // Come on!
Output:

diff -u example.orig example
--- example.orig	1970-01-01 00:00:00.000000000 +0000
+++ example	1970-01-01 00:00:00.000000000 +0000
@@ -1,2 +1,2 @@
 foo
-bar
+baz

func (*BaseFile) Print

func (file *BaseFile) Print() error

Print prints the transformed file’s contents.

func (*BaseFile) String

func (file *BaseFile) String() string

func (*BaseFile) Transform

func (file *BaseFile) Transform(
	_ context.Context,
	transformer Transformer,
) error

Transform changes the file’s contents.

type File

type File interface {
	fmt.Stringer

	After() []byte
	Backup(suffix string) error
	Changed() bool
	Compare(other File) int
	Diff(ctx context.Context, now time.Time) (string, error)
	Print() error
	Transform(ctx context.Context, transformer Transformer) error
	Write() error
}

File is something file-like.

func AppendFile

func AppendFile(a []File, b File) []File

AppendFile inserts a File into its sorted position in a slice of Files.

func NewFile

func NewFile(name string) (file File, err error)

NewFile returns a new file based on a string. "-" is an StdFile, all other strings are paths for [FsFile]s.

type FsFile

type FsFile struct {
	BaseFile
	// contains filtered or unexported fields
}

FsFile is a file in a file system.

func NewFsFile

func NewFsFile(path string) (*FsFile, error)

NewFsFile returns a new FsFile for a path.

func (*FsFile) Backup

func (file *FsFile) Backup(suffix string) error

Backup makes a copy of a file with the specified suffix.

func (*FsFile) Compare

func (file *FsFile) Compare(other File) int

Compare compares an FsFile to an other File for sorting.

func (*FsFile) Write

func (file *FsFile) Write() error

type Sorter

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

Sorter can sort a Go file.

func NewSorter

func NewSorter(source interface{}) (*Sorter, error)

NewSorter returns a new Sorter for some source code. The source is passed on to go/parser.ParseFile.

func (*Sorter) Sort

func (s *Sorter) Sort() ([]byte, error)

Sort sorts the file and returns the sorted contents.

type StdFile

type StdFile struct {
	BaseFile
}

StdFile is a ‘file’ that is read from STDIN and printed to STDOUT.

func NewStdFile

func NewStdFile() (*StdFile, error)

NewStdFile returns a new StdFile whose contents are read from STDIN.

func (*StdFile) Backup

func (file *StdFile) Backup(string) error

Backup does nothing.

func (*StdFile) Compare

func (file *StdFile) Compare(other File) int

Compare compares this StdFile to an other File for sorting. It returns 0 for other [StdFiles] and -1 for all other files.

func (*StdFile) Write

func (file *StdFile) Write() error

type Transformer

type Transformer func([]byte) ([]byte, error)

Transformer is a function that takes bytes, transforms them and returns them.

Jump to

Keyboard shortcuts

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