rule

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package rule defines the Rule interface, severity/fix-safety vocabulary, and the Finding/TextEdit types every rule emits. Fixes are first-class byte-offset edits so lint --fix, fmt, and --diff all consume the same data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DocURL added in v0.3.0

func DocURL(name string) string

DocURL returns the documentation URL for a built-in rule by name.

Types

type DeclSpec

type DeclSpec struct {
	ID         string
	Type       string // required | length | not_equal | match | deny
	Glob       string // optional path scope (doublestar)
	Field      string // for required/length/match/deny
	Fields     []string
	Min, Max   int
	Pattern    string // for match/deny
	SkipDrafts bool
	Severity   Severity
}

DeclSpec is one user-defined rule from the config `custom:` block.

type Example added in v0.5.0

type Example struct {
	Bad  string
	Good string
}

Example is an optional before/after illustration for a rule, rendered into its generated docs page. Bad is the markdown that triggers the rule; Good is the corrected (or autofixed) form.

type Finding

type Finding struct {
	Rule     string     `json:"rule"`
	Path     string     `json:"path"`
	Line     int        `json:"line"`
	Col      int        `json:"col"`
	Message  string     `json:"message"`
	Severity Severity   `json:"severity"`
	Safety   FixSafety  `json:"-"`
	DocURL   string     `json:"-"` // documentation URL (built-in rules only)
	Fixes    []TextEdit `json:"-"`
}

Finding is one reported issue.

type FixSafety

type FixSafety int

FixSafety describes whether a fix preserves meaning.

const (
	NoFix  FixSafety = iota // no automatic fix
	Safe                    // applied by --fix and fmt
	Unsafe                  // applied only with --unsafe-fixes
)

type Meta

type Meta struct {
	Name        string
	Title       string            // human-readable display name, shown in the docs
	Description string            // one line, shown by `list`
	Detail      string            // long help, shown by `explain`
	Severity    Severity          // default; config may override
	Formats     []document.Format // which formats this rule applies to
	Safety      FixSafety         // safety of fixes this rule emits
	Example     Example           // optional before/after for the docs page
}

Meta is a rule's static descriptor.

func (Meta) AppliesTo

func (m Meta) AppliesTo(f document.Format) bool

AppliesTo reports whether the rule runs on the given format.

type Registry

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

Registry holds rules by name in registration order.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty registry.

func (*Registry) All

func (r *Registry) All() []Rule

All returns the rules in registration order.

func (*Registry) Get

func (r *Registry) Get(name string) (Rule, bool)

Get returns the rule with name, if present.

func (*Registry) Register

func (r *Registry) Register(rule Rule)

Register adds a rule; a duplicate name panics (programmer error).

type Rule

type Rule interface {
	Meta() Meta
	Check(doc *document.Document, report func(Finding))
}

Rule inspects a Document and reports findings.

func NewDeclarativeRule

func NewDeclarativeRule(spec DeclSpec) (Rule, error)

NewDeclarativeRule compiles a DeclSpec into a Rule.

type Severity

type Severity int

Severity orders findings from advisory to blocking.

const (
	Info Severity = iota
	Warning
	Error
)

func ParseSeverity

func ParseSeverity(s string) (Severity, error)

ParseSeverity converts a config string into a Severity.

func (Severity) String

func (s Severity) String() string

type TextEdit

type TextEdit struct {
	Start   int
	End     int
	NewText string
}

TextEdit replaces Raw[Start:End] with NewText. Offsets index Document.Raw.

Directories

Path Synopsis
Package builtin holds programmatic (Go) rules.
Package builtin holds programmatic (Go) rules.

Jump to

Keyboard shortcuts

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