engine

package
v0.7.6 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package engine discovers files, runs rules in parallel, applies inline suppression, and applies fixes or renders diffs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyEdits

func ApplyEdits(src []byte, edits []rule.TextEdit) ([]byte, error)

ApplyEdits returns src with every edit applied. Edits must not overlap; they are applied last-to-first so earlier offsets stay valid during splicing.

func Format

func Format(doc *document.Document) []byte

Format applies the full deterministic, idempotent formatting pass using the built-in default registry. See FormatWith for details on the individual steps.

func FormatWith added in v0.7.0

func FormatWith(doc *document.Document, reg *FormatRegistry) []byte

FormatWith runs the core formatting steps followed by every pass in reg. The core steps are:

  1. Apply always-safe structural fixes (blank lines around fences/lists/ headings, ATX-heading space, details blank line, trailing spaces).
  2. Collapse consecutive blank lines outside fenced code to at most one.
  3. Ensure a single trailing newline.

Registered passes then run in order, each receiving the previous pass's output. Built-in passes (registered by DefaultFormatRegistry) add:

  1. table-align — aligns GFM table column widths
  2. shortcode-indent — re-indents Hugo shortcode tag lines by nesting depth

Every step is idempotent; the structural fixes include no-trailing-spaces' targeted strip of a single stray trailing space or whitespace-only line — a deliberate two-space hard line break is never touched and an ambiguous 3+ run is left for a human.

func UnifiedDiff

func UnifiedDiff(path string, before, after []byte) string

UnifiedDiff renders a unified diff of before vs after for one file.

Types

type Engine

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

Engine holds the resolved active rule set and config.

func New

func New(cfg *config.Config, reg *rule.Registry) (*Engine, error)

New resolves the active rules: built-ins filtered by enable/disable/default, plus compiled declarative rules from the config `custom:` block.

func (*Engine) Fix

func (e *Engine) Fix(ctx context.Context, paths []string, unsafe, dryRun bool) ([]string, error)

Fix lints, applies safe (and optionally unsafe) fixes per file, and either writes the files (dryRun=false) or returns them without writing. It returns the list of changed paths.

func (*Engine) MarkdownFiles

func (e *Engine) MarkdownFiles(paths []string) ([]string, error)

MarkdownFiles returns the discovered markdown file paths under paths.

func (*Engine) Restrict added in v0.5.0

func (e *Engine) Restrict(names []string) error

Restrict limits the active rules to those named, so `lint --rule X` reports or fixes only X. It errors on a name that is not an active rule (unknown, or disabled by config). Custom rule ids are matched too.

func (*Engine) Run

func (e *Engine) Run(ctx context.Context, paths []string) (*Result, error)

Run lints every discovered file under paths and returns sorted findings.

func (*Engine) UseCache added in v0.4.0

func (e *Engine) UseCache(c *cache.Cache, version, configHash string)

UseCache enables per-file finding caching for plain lint runs. version and configHash join the per-file content hash in the cache key, so a new doclint build or an edited config invalidates stale entries.

type FormatPass added in v0.7.0

type FormatPass interface {
	// Name returns the unique identifier for this pass, used in diagnostics.
	Name() string
	// Apply transforms src and returns the result.
	Apply(src []byte) []byte
}

FormatPass is a single idempotent byte-level formatting pass. Passes run after the core formatter (structural fixes, blank-run collapse, trailing-newline normalisation) in registration order; each pass receives the output of the previous one.

Implementations must be idempotent: Apply(Apply(src)) == Apply(src). src is always valid UTF-8 markdown ending with exactly one trailing newline.

type FormatRegistry added in v0.7.0

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

FormatRegistry holds the ordered sequence of FormatPass implementations applied after the core formatter. Register passes in the order you want them to run; each pass sees the previous pass's output.

func DefaultFormatRegistry added in v0.7.0

func DefaultFormatRegistry() *FormatRegistry

DefaultFormatRegistry returns a FormatRegistry pre-loaded with the built-in passes in their canonical order:

  1. table-align — aligns GFM table column widths
  2. shortcode-indent — re-indents Hugo shortcode tag lines by nesting depth

To add a new built-in pass, implement FormatPass in its own file and add a Register call here. To use a custom set, start with NewFormatRegistry.

func FormatRegistryFor added in v0.7.5

func FormatRegistryFor(cfg *config.Config) *FormatRegistry

FormatRegistryFor builds the format registry honoring cfg's `fmt` options: table-align always runs; shortcode-indent runs only when enabled, using the configured indent width and per-shortcode exclude list. A nil cfg yields the default registry.

func NewFormatRegistry added in v0.7.0

func NewFormatRegistry() *FormatRegistry

NewFormatRegistry returns an empty registry.

func (*FormatRegistry) All added in v0.7.0

func (r *FormatRegistry) All() []FormatPass

All returns the registered passes in registration order.

func (*FormatRegistry) Register added in v0.7.0

func (r *FormatRegistry) Register(p FormatPass)

Register appends a pass. Panics on duplicate names (programmer error).

type Result

type Result struct {
	Findings []rule.Finding
}

Result is the outcome of a Run.

func (*Result) ExitCode

func (r *Result) ExitCode() int

ExitCode is 1 when any Error-severity finding is present, else 0.

type ShortcodeIndentPass added in v0.7.0

type ShortcodeIndentPass struct {
	IndentWidth int
	Exclude     map[string]bool
}

ShortcodeIndentPass is a FormatPass that fixes shortcode indentation inside list items. Standalone shortcodes are emitted verbatim (no tree indentation).

IndentWidth is the number of spaces added per shortcode nesting level inside a list-inline block (0 → the default of 2). Exclude names shortcodes whose whole subtree (opener through matching closer) is emitted verbatim — useful for shortcodes with carefully hand-aligned internals (e.g. uplatnica payment forms).

func (ShortcodeIndentPass) Apply added in v0.7.0

func (p ShortcodeIndentPass) Apply(src []byte) []byte

func (ShortcodeIndentPass) Name added in v0.7.0

func (ShortcodeIndentPass) Name() string

type Suppressor

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

Suppressor matches findings against inline doclint-disable directives and tracks which directives went unused.

func NewSuppressor

func NewSuppressor(doc *document.Document) *Suppressor

NewSuppressor scans a document for suppression directives.

func (*Suppressor) Suppressed

func (s *Suppressor) Suppressed(f rule.Finding) bool

Suppressed reports whether f is silenced, marking the matching directive used.

func (*Suppressor) Unused

func (s *Suppressor) Unused() []rule.Finding

Unused returns findings describing directives that matched nothing.

type TableAlignPass added in v0.7.0

type TableAlignPass struct{}

TableAlignPass is a FormatPass that aligns the column widths of well-formed GFM tables. Malformed tables (row cell-count mismatch) are left untouched.

func (TableAlignPass) Apply added in v0.7.0

func (TableAlignPass) Apply(src []byte) []byte

func (TableAlignPass) Name added in v0.7.0

func (TableAlignPass) Name() string

Jump to

Keyboard shortcuts

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