linter

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package linter provides functions to check and fix files for trailing whitespaces and blank lines. It offers three main components:

  • Manager, which wraps a file and provides a simple interface for opening, closing and reading lines.
  • Replacer, which wraps two Manager instances and provides a simple interface for safely replacing a file with another. The intention is that the replacer is used by in-place formatters.
  • Linter, which wraps a Manager and a list of checkers and provides a simple interface for linting and fixing

Index

Constants

This section is empty.

Variables

View Source
var ErrNoCheckers = errors.New("no checkers configured")

ErrNoCheckers is returned when no checkers have been configured.

Functions

This section is empty.

Types

type Checker

type Checker interface {
	Analyze(string, int)
	Finalize()
	Results() ([]int, error)
	Fix(string) string
	Stop() int
}

Checker represents a line analyser.

type Linter

type Linter struct {
	// Name contains the name of file to lint.
	Name string
	// Checkers contains the checkers to be used.
	Checkers []Checker
	// Error contains the error, if any.
	Error error
	// Touched is a flag whether the file has been touched.
	Touched bool
}

Linter represents a file linter.

func NewLinter added in v0.1.0

func NewLinter(name string) *Linter

NewLinter creates a new linter, with the default checkers.

func (*Linter) Fix added in v0.1.0

func (l *Linter) Fix(file Writeable) (err error)

Fix fixes the file by removing trailing whitespaces and blank lines.

func (*Linter) GetIssues added in v0.1.0

func (l *Linter) GetIssues() (rows [][]int, errs []error)

GetIssues returns a list of rows with issues and a list of errors.

func (*Linter) HasCheckers

func (l *Linter) HasCheckers() bool

HasCheckers returns true if the linter has checkers configured.

func (*Linter) HasIssues

func (l *Linter) HasIssues() bool

HasIssues takes a list of errors and returns false if all errors are nil.

func (*Linter) InsertChecker

func (l *Linter) InsertChecker(c Checker)

InsertChecker adds a checker to the list of checkers in use.

func (*Linter) Lint added in v0.1.0

func (l *Linter) Lint(file Readable) (err error)

Lint checks the file.

func (*Linter) Summary

func (l *Linter) Summary() (ok bool)

Summary prints a summary of the file.

type Main added in v0.1.0

type Main = *Reader

Main is an alias for Reader, to be used as a base for other types.

type ReadStringer added in v0.1.0

type ReadStringer interface {
	ReadString(delim byte) (string, error)
}

ReadStringer is a simplified version of bufio.Reader.

type ReadWriteSeekerCloser added in v0.1.0

type ReadWriteSeekerCloser interface {
	io.ReadWriteSeeker
	io.Closer
}

ReadWriteSeekerCloser is a combination of io.ReadWriteSeeker and io.Closer.

type Readable added in v0.1.0

type Readable interface {
	Open() error
	Close() error
	HasLines() bool
	Next() (string, error)
	Load(...string) error
}

Readable describes a file that can be read from.

type Reader added in v0.1.0

type Reader struct {
	// Name is the name of the file to open. Should be a full or relative path.
	Name string
	// contains filtered or unexported fields
}

Reader represents a wrapped management of file handling. Given a name, it can open, close and read lines from the file, until EOF.

func CreateShadow added in v0.1.0

func CreateShadow(name string) (shadow *Reader, err error)

CreateShadow creates a new shadow file for the given file.

func NewReader added in v0.1.0

func NewReader(name string) (*Reader, error)

NewReader opens a file for reading (and writing).

func (*Reader) Close added in v0.1.0

func (f *Reader) Close() error

Close closes the file. Returns nil if the file is already closed.

func (*Reader) HasLines added in v0.1.0

func (f *Reader) HasLines() bool

HasLines returns true if there are lines available to read.

func (*Reader) Load added in v0.1.0

func (f *Reader) Load(filename ...string) error

Load opens the file. It allows for reusing the same file manager for a new file. If no filename is provided, it equates to calling Open().

func (*Reader) Next added in v0.1.0

func (f *Reader) Next() (line string, err error)

Next reads the next line from the file.

func (*Reader) Open added in v0.1.0

func (f *Reader) Open() (err error)

Open the file for reading. Returns an error if the file doesn't exist.

func (*Reader) Rename added in v0.1.0

func (f *Reader) Rename(name string) error

Rename renames the file to the given string.

func (*Reader) ReplaceWith added in v0.1.0

func (f *Reader) ReplaceWith(replacement *Reader) (err error)

ReplaceWith replaces the current file with the given file.

func (*Reader) Reset added in v0.1.0

func (f *Reader) Reset() error

Reset resets the file to the beginning and assigns a fresh reader.

func (*Reader) Save added in v0.1.0

func (f *Reader) Save() error

Save simply closes the file, since all writes are done in place.

func (*Reader) Write added in v0.1.0

func (f *Reader) Write(lines ...string) error

Write writes a line to the file.

type Writeable added in v0.1.0

type Writeable interface {
	Readable
	Write(...string) error
	Save() error
}

Writeable describes a file that can be written to.

type Writer added in v0.1.0

type Writer struct {
	Main
	Shadow *Reader
}

Writer enables formatting of a file, by first writing to a temporary file. When the formatting is done, the original file is replaced with the temporary file.

func NewWriter added in v0.1.0

func NewWriter(file *Reader) (formatter *Writer, err error)

NewWriter creates a new formatter for the given file.

func (*Writer) Close added in v0.1.0

func (f *Writer) Close() (err error)

Close closes both files.

func (*Writer) Load added in v0.1.0

func (f *Writer) Load(filename ...string) (err error)

Load loads both files.

func (*Writer) Open added in v0.1.0

func (f *Writer) Open() (err error)

Open opens both files.

func (*Writer) Save added in v0.1.0

func (f *Writer) Save() error

Save applies the changes to the original file.

func (*Writer) Write added in v0.1.0

func (f *Writer) Write(line ...string) error

Write writes a line to the Shadow file.

Jump to

Keyboard shortcuts

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