lint

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2025 License: MIT Imports: 23 Imported by: 38

Documentation

Overview

Package lint implements the linting machinery.

Index

Constants

View Source
const (
	// SeverityWarning declares failures of type warning
	SeverityWarning = "warning"
	// SeverityError declares failures of type error.
	SeverityError = "error"
)

Variables

This section is empty.

Functions

func Name

func Name(name string, allowlist, blocklist []string) (should string)

Name returns a different name if it should be different.

Types

type Arguments

type Arguments = []any

Arguments is type used for the arguments of a rule.

type Config

type Config struct {
	IgnoreGeneratedHeader bool `toml:"ignoreGeneratedHeader"`
	Confidence            float64
	Severity              Severity
	EnableAllRules        bool             `toml:"enableAllRules"`
	Rules                 RulesConfig      `toml:"rule"`
	ErrorCode             int              `toml:"errorCode"`
	WarningCode           int              `toml:"warningCode"`
	Directives            DirectivesConfig `toml:"directive"`
	Exclude               []string         `toml:"exclude"`
	// If set, overrides the go language version specified in go.mod of
	// packages being linted, and assumes this specific language version.
	GoVersion *goversion.Version
}

Config defines the config of the linter.

type ConfigurableRule added in v1.6.0

type ConfigurableRule interface {
	Configure(Arguments) error
}

ConfigurableRule defines an abstract configurable rule interface.

type DirectiveConfig

type DirectiveConfig struct {
	Severity Severity
}

DirectiveConfig is type used for the linter directive configuration.

type DirectivesConfig

type DirectivesConfig = map[string]DirectiveConfig

DirectivesConfig defines the config for all directives.

type DisabledInterval

type DisabledInterval struct {
	From     token.Position
	To       token.Position
	RuleName string
}

DisabledInterval contains a single disabled interval and the associated rule name.

type Failure

type Failure struct {
	Failure    string
	RuleName   string
	Category   FailureCategory
	Position   FailurePosition
	Node       ast.Node `json:"-"`
	Confidence float64
	// For future use
	ReplacementLine string
}

Failure defines a struct for a linting failure.

func NewInternalFailure added in v1.6.0

func NewInternalFailure(message string) Failure

NewInternalFailure yields an internal failure with the given message as failure message.

func (*Failure) GetFilename

func (f *Failure) GetFilename() string

GetFilename returns the filename.

func (*Failure) IsInternal added in v1.6.0

func (f *Failure) IsInternal() bool

IsInternal returns true if this failure is internal, false otherwise.

type FailureCategory added in v1.6.0

type FailureCategory string

FailureCategory is the type for the failure categories.

const (
	// FailureCategoryArgOrder indicates argument order issues.
	FailureCategoryArgOrder FailureCategory = "arg-order"
	// FailureCategoryBadPractice indicates bad practice issues.
	FailureCategoryBadPractice FailureCategory = "bad practice"
	// FailureCategoryCodeStyle indicates code style issues.
	FailureCategoryCodeStyle FailureCategory = "code-style"
	// FailureCategoryComments indicates comment issues.
	FailureCategoryComments FailureCategory = "comments"
	// FailureCategoryComplexity indicates complexity issues.
	FailureCategoryComplexity FailureCategory = "complexity"
	// FailureCategoryContent indicates content issues.
	FailureCategoryContent FailureCategory = "content"
	// FailureCategoryErrors indicates error handling issues.
	FailureCategoryErrors FailureCategory = "errors"
	// FailureCategoryImports indicates import issues.
	FailureCategoryImports FailureCategory = "imports"
	// FailureCategoryLogic indicates logic issues.
	FailureCategoryLogic FailureCategory = "logic"
	// FailureCategoryMaintenance indicates maintenance issues.
	FailureCategoryMaintenance FailureCategory = "maintenance"
	// FailureCategoryNaming indicates naming issues.
	FailureCategoryNaming FailureCategory = "naming"
	// FailureCategoryOptimization indicates optimization issues.
	FailureCategoryOptimization FailureCategory = "optimization"
	// FailureCategoryStyle indicates style issues.
	FailureCategoryStyle FailureCategory = "style"
	// FailureCategoryTime indicates time-related issues.
	FailureCategoryTime FailureCategory = "time"
	// FailureCategoryTypeInference indicates type inference issues.
	FailureCategoryTypeInference FailureCategory = "type-inference"
	// FailureCategoryUnaryOp indicates unary operation issues.
	FailureCategoryUnaryOp FailureCategory = "unary-op"
	// FailureCategoryUnexportedTypeInAPI indicates unexported type in API issues.
	FailureCategoryUnexportedTypeInAPI FailureCategory = "unexported-type-in-api"
	// FailureCategoryZeroValue indicates zero value issues.
	FailureCategoryZeroValue FailureCategory = "zero-value"
)

type FailurePosition

type FailurePosition struct {
	Start token.Position
	End   token.Position
}

FailurePosition returns the failure position

func ToFailurePosition

func ToFailurePosition(start, end token.Pos, file *File) FailurePosition

ToFailurePosition returns the failure position.

type File

type File struct {
	Name string
	Pkg  *Package

	AST *ast.File
	// contains filtered or unexported fields
}

File abstraction used for representing files.

func NewFile

func NewFile(name string, content []byte, pkg *Package) (*File, error)

NewFile creates a new file

func (*File) CommentMap

func (f *File) CommentMap() ast.CommentMap

CommentMap builds a comment map for the file.

func (*File) Content

func (f *File) Content() []byte

Content returns the file's content.

func (*File) IsTest

func (f *File) IsTest() bool

IsTest returns if the file contains tests.

func (*File) IsUntypedConst

func (f *File) IsUntypedConst(expr ast.Expr) (defType string, ok bool)

IsUntypedConst reports whether expr is an untyped constant, and indicates what its default type is. scope may be nil.

func (*File) Render

func (f *File) Render(x any) string

Render renders a node.

func (*File) ToPosition

func (f *File) ToPosition(pos token.Pos) token.Position

ToPosition returns line and column for given position.

type FileFilter added in v1.3.3

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

FileFilter - file filter to exclude some files for rule supports whole 1. file/dir names : pkg/mypkg/my.go, 2. globs: **/*.pb.go, 3. regexes (~ prefix) ~-tmp\.\d+\.go 4. special test marker `TEST` - treats as `~_test\.go`

func ParseFileFilter added in v1.3.3

func ParseFileFilter(rawFilter string) (*FileFilter, error)

ParseFileFilter - creates FileFilter for given raw filter if empty string, it matches nothing if `*`, or `~`, it matches everything while regexp could be invalid, it could return it's compilation error

func (*FileFilter) MatchFileName added in v1.3.3

func (ff *FileFilter) MatchFileName(name string) bool

MatchFileName - checks if file name matches filter

func (*FileFilter) String added in v1.3.3

func (ff *FileFilter) String() string

type FileFilters added in v1.3.3

type FileFilters = []*FileFilter

FileFilters is type used for modeling file filters to apply to rules.

type Formatter

type Formatter interface {
	Format(<-chan Failure, Config) (string, error)
	Name() string
}

Formatter defines an interface for failure formatters

type FormatterMetadata

type FormatterMetadata struct {
	Name        string
	Description string
	Sample      string
}

FormatterMetadata configuration of a formatter

type Linter

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

Linter is used for linting set of files.

func New

func New(reader ReadFile, maxOpenFiles int) Linter

New creates a new Linter

func (*Linter) Lint

func (l *Linter) Lint(packages [][]string, ruleSet []Rule, config Config) (<-chan Failure, error)

Lint lints a set of files with the specified rule.

type Package

type Package struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Package represents a package in the project.

func (*Package) Files added in v1.2.2

func (p *Package) Files() map[string]*File

Files return package's files.

func (*Package) IsAtLeastGo115 added in v1.6.1

func (p *Package) IsAtLeastGo115() bool

IsAtLeastGo115 returns true if the Go version for this package is 1.15 or higher, false otherwise

func (*Package) IsAtLeastGo121 added in v1.4.0

func (p *Package) IsAtLeastGo121() bool

IsAtLeastGo121 returns true if the Go version for this package is 1.21 or higher, false otherwise

func (*Package) IsAtLeastGo122 added in v1.3.8

func (p *Package) IsAtLeastGo122() bool

IsAtLeastGo122 returns true if the Go version for this package is 1.22 or higher, false otherwise

func (*Package) IsAtLeastGo124 added in v1.7.0

func (p *Package) IsAtLeastGo124() bool

IsAtLeastGo124 returns true if the Go version for this package is 1.24 or higher, false otherwise

func (*Package) IsMain

func (p *Package) IsMain() bool

IsMain returns if that's the main package.

func (*Package) Sortable

func (p *Package) Sortable() map[string]bool

Sortable yields a map of sortable types in this package

func (*Package) TypeCheck

func (p *Package) TypeCheck() error

TypeCheck performs type checking for given package.

func (*Package) TypeOf

func (p *Package) TypeOf(expr ast.Expr) types.Type

TypeOf returns the type of expression.

func (*Package) TypesInfo

func (p *Package) TypesInfo() *types.Info

TypesInfo yields type information of this package identifiers

func (*Package) TypesPkg

func (p *Package) TypesPkg() *types.Package

TypesPkg yields information on this package

type ReadFile

type ReadFile func(path string) (result []byte, err error)

ReadFile defines an abstraction for reading files.

type Rule

type Rule interface {
	Name() string
	Apply(*File, Arguments) []Failure
}

Rule defines an abstract rule interface

type RuleConfig

type RuleConfig struct {
	Arguments Arguments
	Severity  Severity
	Disabled  bool
	// Exclude - rule-level file excludes, TOML related (strings)
	Exclude []string
	// contains filtered or unexported fields
}

RuleConfig is type used for the rule configuration.

func (*RuleConfig) Initialize added in v1.3.3

func (rc *RuleConfig) Initialize() error

Initialize - should be called after reading from TOML file

func (*RuleConfig) MustExclude added in v1.3.3

func (rc *RuleConfig) MustExclude(name string) bool

MustExclude - checks if given filename `name` must be excluded

type RulesConfig

type RulesConfig = map[string]RuleConfig

RulesConfig defines the config for all rules.

type Severity

type Severity string

Severity is the type for the failure types.

Jump to

Keyboard shortcuts

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