Documentation
¶
Overview ¶
Package goxlint is the rule-authoring API for goxlint.
A rule declares metadata and a Create function, an AnalyzePackage function, or both. Create runs once per file and returns a Visitor: a set of node handlers that the engine calls during a single shared AST walk. Rules report problems through the Context.
var NoFoo = &goxlint.Rule{
Meta: goxlint.Meta{
Name: "no-foo",
Category: goxlint.Suspicious,
Messages: map[string]string{"foo": "Avoid `{{name}}`."},
},
Create: func(ctx *goxlint.Context) goxlint.Visitor {
return goxlint.Visit(
goxlint.On(func(n *ast.Ident) {
if n.Name == "foo" {
ctx.Report(goxlint.Diagnostic{Node: n, MessageID: "foo", Data: goxlint.Data{"name": n.Name}})
}
}),
)
},
}
Index ¶
- Variables
- func Interpolate(tmpl string, data Data) string
- type Category
- type Context
- func (c *Context) Ancestors() []ast.Node
- func (c *Context) CalleeFunc(call *ast.CallExpr) *types.Func
- func (c *Context) CommentsBefore(pos token.Pos) *ast.CommentGroup
- func (c *Context) IsPkgFunc(call *ast.CallExpr, pkgPath string, names ...string) bool
- func (c *Context) IsTestFile() bool
- func (c *Context) Line(pos token.Pos) int
- func (c *Context) ObjectOf(id *ast.Ident) types.Object
- func (c *Context) Options(v any) bool
- func (c *Context) OwnLine(pos token.Pos) bool
- func (c *Context) Parent() ast.Node
- func (c *Context) Report(d Diagnostic)
- func (c *Context) Snippet(node ast.Node) string
- func (c *Context) Text(node ast.Node) string
- func (c *Context) TrailingComment(pos token.Pos) *ast.CommentGroup
- func (c *Context) TypeOf(expr ast.Expr) types.Type
- type Data
- type Diagnostic
- type Edit
- type Fix
- type FixKind
- type Handler
- type Meta
- type PackageContext
- type Plugin
- type Related
- type Rule
- type Visitor
Constants ¶
This section is empty.
Variables ¶
var Categories = []Category{Correctness, Suspicious, Pedantic, Style, Restriction, Nursery}
Categories lists every category in display order.
Functions ¶
func Interpolate ¶
Interpolate replaces {{key}} placeholders in tmpl with values from data.
Types ¶
type Category ¶
type Category string
Category groups rules by intent, mirroring oxlint's categories.
const ( // Correctness rules catch code that is wrong or useless. Enabled by default. Correctness Category = "correctness" // Suspicious rules catch code that is most likely wrong. Suspicious Category = "suspicious" // Pedantic rules are strict and may have false positives. Pedantic Category = "pedantic" // Style rules enforce idiomatic or readable code. Style Category = "style" // Restriction rules ban patterns that are valid but unwanted by policy. Restriction Category = "restriction" // Nursery rules are under development. Nursery Category = "nursery" )
type Context ¶
type Context struct {
// Enabled is false for package-analysis evidence files where the rule is
// disabled. Reports from those contexts are ignored by the engine.
Enabled bool
Rule *Rule
RuleID string // "<plugin>/<rule>"
Fset *token.FileSet
File *ast.File
Filename string
Source []byte
// Pkg and TypesInfo are nil unless the rule's Meta.NeedsTypes is set.
Pkg *types.Package
TypesInfo *types.Info
// contains filtered or unexported fields
}
Context is a rule's view of the file being linted.
func NewContext ¶
func NewContext(rule *Rule, ruleID string, fset *token.FileSet, file *ast.File, filename string, src []byte, pkg *types.Package, info *types.Info, options json.RawMessage, stack *[]ast.Node, report func(Diagnostic)) *Context
NewContext is used by the engine. Rules receive a ready Context from Create.
func (*Context) CalleeFunc ¶
CalleeFunc returns the function or method a call statically invokes, or nil.
func (*Context) CommentsBefore ¶
func (c *Context) CommentsBefore(pos token.Pos) *ast.CommentGroup
CommentsBefore returns the comment group that ends on the line directly above pos, or nil. A blank line between the comment and pos detaches it.
func (*Context) IsPkgFunc ¶
IsPkgFunc reports whether call invokes the package-level function pkgPath.name.
func (*Context) IsTestFile ¶
IsTestFile reports whether the file is a _test.go file.
func (*Context) Options ¶
Options decodes the rule's configured options into v. It leaves v unchanged when no options are configured, so callers set defaults before calling. Invalid options are reported as an error diagnostic and Options returns false; the rule should then return a nil Visitor.
func (*Context) Parent ¶
Parent returns the parent of the node currently being visited, or nil at the root.
func (*Context) Report ¶
func (c *Context) Report(d Diagnostic)
Report records a diagnostic. It panics when the rule itself is broken (an unknown message ID, or a fix more dangerous than Meta.Fixable); the engine recovers and reports the rule as failed.
func (*Context) Snippet ¶
Snippet returns node's source collapsed to one line and shortened, for quoting code in messages.
func (*Context) TrailingComment ¶
func (c *Context) TrailingComment(pos token.Pos) *ast.CommentGroup
TrailingComment returns the comment group that starts on the same line as pos, after it, or nil.
type Diagnostic ¶
type Diagnostic struct {
// Node locates the problem. Pos and End may be set instead.
Node ast.Node
Pos, End token.Pos
// MessageID selects a template from Meta.Messages. Message may be set instead.
MessageID string
Message string
Data Data
// Help is an optional hint printed under the diagnostic.
Help string
// Related contains supporting locations in loaded files of the same package.
// They do not change the primary location used for suppressions and fixes.
Related []Related
// Fix is an optional fix. Its kind must not exceed Meta.Fixable.
Fix *Fix
}
Diagnostic is a problem reported by a rule.
type Edit ¶
Edit replaces the source between Pos and End with NewText.
func InsertBefore ¶
InsertBefore returns an edit that inserts text at pos.
type FixKind ¶
type FixKind int
FixKind describes how safe a rule's fixes are to apply automatically.
const ( // FixNone means the rule provides no fixes. FixNone FixKind = iota // FixSafe fixes never change program behavior. Applied by --fix. FixSafe // FixSuggestion fixes may change behavior and need review. Applied by --fix-suggestions. FixSuggestion // FixDangerous fixes can break code. Applied by --fix-dangerously. FixDangerous )
type Handler ¶
Handler is a callback for one AST node type, on entry or exit.
type Meta ¶
type Meta struct {
// Name is the rule name without the plugin prefix, for example "no-any-parameters".
Name string
// Description is a one-line summary shown by `goxlint rules`.
Description string
Category Category
// Messages maps message IDs to templates. Templates interpolate Data with {{key}}.
Messages map[string]string
// Fixable is the most dangerous kind of fix the rule can produce.
Fixable FixKind
// NeedsTypes requests type information. When no enabled rule needs types,
// goxlint parses files directly without loading packages, which is much faster.
NeedsTypes bool
}
Meta describes a rule.
type PackageContext ¶
PackageContext provides the loaded files in one directory and Go package. Files are ordered by filename, include files where this rule is disabled, and exclude ignored and generated files. A partial selection (such as an explicit file pattern in syntax mode) includes only the loaded files. Treat syntax trees and type information as read-only.
type Related ¶ added in v0.1.0
Related is a supporting location for a diagnostic. Positions use the reporting context's FileSet. Node takes precedence over Pos and End. An omitted End denotes a point at Pos. Message labels the location.
type Rule ¶
type Rule struct {
Meta Meta
// Create runs once per file and returns the handlers to call during the walk.
// It may be nil for a package-only rule.
Create func(ctx *Context) Visitor
// AnalyzePackage runs once per package after file visitors, before suppressions.
// A rule may provide either or both callbacks. Package state must be local
// to this invocation: different packages can be analyzed concurrently.
AnalyzePackage func(ctx *PackageContext)
}
Rule is a lint rule.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package cli implements the goxlint command.
|
Package cli implements the goxlint command. |
|
examples
|
|
|
goxlint
command
Command goxlint demonstrates a linter built from the goxlint engine and example plugins.
|
Command goxlint demonstrates a linter built from the goxlint engine and example plugins. |
|
goxlint/plugins/antislop
Package antislop rejects low-evidence and low-signal Go patterns.
|
Package antislop rejects low-evidence and low-signal Go patterns. |
|
goxlint/plugins/maintainability
Package maintainability provides configurable complexity and duplication checks.
|
Package maintainability provides configurable complexity and duplication checks. |
|
Package goxlintanalysis adapts goxlint rules to go/analysis drivers.
|
Package goxlintanalysis adapts goxlint rules to go/analysis drivers. |
|
Package goxlinttest is the RuleTester for goxlint rules.
|
Package goxlinttest is the RuleTester for goxlint rules. |
|
internal
|
|
|
config
Package config loads .goxlintrc.json and resolves which rules apply to each file.
|
Package config loads .goxlintrc.json and resolves which rules apply to each file. |
|
engine
Package engine runs goxlint rules over parsed files.
|
Package engine runs goxlint rules over parsed files. |
|
output
Package output formats diagnostics.
|
Package output formats diagnostics. |