fix

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: May 24, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Source added in v0.13.0

func Source(opts SourceOptions) ([]byte, error)

Source applies every fixable rule allowed by the effective config and returns the resulting bytes. The returned bytes equal the input when no rule produced an edit.

func SourceWithRules added in v0.13.0

func SourceWithRules(opts SourceOptions, names []string) ([]byte, error)

SourceWithRules is like Source but only the named rules are applied. An empty names slice produces no fixes.

Types

type Fixer

type Fixer struct {
	Config           *config.Config
	Rules            []rule.Rule
	StripFrontMatter bool
	Logger           *vlog.Logger
	// RootDir is the project root directory (parent of .mdsmith.yml).
	// Used by rules that need to read files relative to the project root.
	RootDir string
	// MaxInputBytes is the maximum file size in bytes before a file is
	// skipped with an error. Zero or negative means unlimited.
	MaxInputBytes int64
	// Explain, when true, attaches per-leaf rule provenance to each
	// remaining diagnostic so output formatters can render an
	// explanation trailer.
	Explain bool
	// DryRun, when true, runs the full fix pipeline but skips the
	// write back to disk. Modified is left empty (nothing was
	// written); the per-rule would-fix tally is recorded on the
	// Result's WouldFix and WouldFixFiles fields so callers can
	// preview what a real run would change.
	DryRun bool
	// SourceFS, when non-nil, overrides the per-file dirFS that
	// prepareFile would otherwise derive from filepath.Dir(path).
	// Used by Source / SourceWithRules so callers can pass a
	// workspace-relative path for config matching while still giving
	// include/catalog/cross-file rules a real filesystem rooted at
	// the document's actual directory. Disk-based Fix() (path-based)
	// leaves this nil and continues to derive dirFS from each file's
	// absolute path.
	SourceFS fs.FS

	// WriteFile, when non-nil, replaces atomicWriteFile for the
	// final on-disk write step. Tests inject an error-returning
	// function to exercise write-failure branches without OS-level
	// read-only tricks. Production callers leave it nil.
	WriteFile func(path string, data []byte, perm os.FileMode) error
	// contains filtered or unexported fields
}

Fixer applies auto-fixes for fixable rules and reports remaining diagnostics.

func (*Fixer) Fix

func (f *Fixer) Fix(paths []string) *Result

Fix applies auto-fixes to the files at the given paths and returns a Result containing remaining diagnostics, modified file paths, and any errors.

type Result

type Result struct {
	// FilesChecked is the number of files processed (after ignore filtering).
	FilesChecked int
	// Failures is the number of diagnostics found before attempting fixes.
	Failures int
	// Diagnostics contains remaining diagnostics after fixing (from non-fixable
	// rules and any violations that could not be auto-fixed).
	Diagnostics []lint.Diagnostic
	// Modified lists file paths that were written back to disk. Always
	// empty when Fixer.DryRun is true.
	Modified []string
	// WouldFix is the total number of diagnostics across all files
	// that would be resolved by a real fix run. Equals the sum of
	// WouldFixFiles[i].Count. Populated only when Fixer.DryRun is
	// true.
	WouldFix int
	// WouldFixFiles is the per-file preview produced by a dry run.
	// Each entry corresponds to one file whose diagnostic count
	// would decrease or whose bytes would change. Populated only
	// when Fixer.DryRun is true.
	WouldFixFiles []WouldFixFile
	// Errors contains any errors encountered during the fix process.
	Errors []error
}

Result holds the outcome of a fix run.

type RuleFixCount added in v0.24.0

type RuleFixCount struct {
	RuleID string
	Count  int
}

RuleFixCount records one rule's would-fix tally inside a WouldFixFile.

type SourceOptions added in v0.13.0

type SourceOptions struct {
	Config           *config.Config
	Rules            []rule.Rule
	Path             string
	Source           []byte
	RootDir          string
	StripFrontMatter bool
	// MaxInputBytes caps Source size before any fix runs. Semantics
	// match lint.ReadFileLimited: <= 0 or math.MaxInt64 means
	// unlimited; > 0 means reject buffers larger than that many
	// bytes. Callers that want the default 2 MB cap must pass
	// lint.DefaultMaxInputBytes explicitly.
	MaxInputBytes int64
	// SourceFS, when non-nil, is the filesystem the fixable rules
	// (include/catalog/cross-file) see for the buffer. Callers that
	// pass a workspace-relative Path (for config glob matching) MUST
	// also supply a SourceFS rooted at the document's real
	// directory; otherwise the dirFS derived from the relative path
	// would be resolved against the process CWD, breaking
	// neighbour-file lookups when the editor is launched from
	// elsewhere.
	SourceFS fs.FS
}

SourceOptions configures an in-memory fix run. The fix functions (Source, SourceWithRules) do not touch disk; they reuse the same prep/apply machinery the path-based Fixer uses on a buffer.

type WouldFixFile added in v0.24.0

type WouldFixFile struct {
	// Path is the file path the preview was computed for.
	Path string
	// Count is the total number of diagnostics that would be
	// resolved by a real fix run. May be zero when the file's bytes
	// would change without any rule's diagnostic count decreasing
	// (e.g. a generated section regenerating without firing a
	// diagnostic before).
	Count int
	// Rules lists per-rule fix tallies, sorted by RuleID.
	Rules []RuleFixCount
}

WouldFixFile is a per-file preview entry returned by a dry run.

Jump to

Keyboard shortcuts

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