linter

package
v0.5.3 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyRuleFixes

func ApplyRuleFixes[M LintMessage](code string, diagnostics []M) (string, []M, bool)

func LintSingleFile added in v0.5.2

func LintSingleFile(opts LintSingleFileOptions)

LintSingleFile runs lint rules against a single file in a single program. Designed for IDE / LSP per-keystroke usage. Does not run type-check.

func RunLinterInProgram added in v0.1.9

func RunLinterInProgram(
	program *compiler.Program,
	allowFiles []string,
	allowDirs []string,
	skipFiles []string,
	getRulesForFile RuleHandler,
	typeCheck bool,
	onDiagnostic DiagnosticHandler,
	typeInfoFiles map[string]struct{},
	fileFilter func(string) bool,
) int32

RunLinterInProgram is a backwards-compatible test adapter for the now-internal runLintRulesInProgram. New code should use RunLinter or LintSingleFile.

When typeCheck is true the adapter routes through RunLinter (single program) so callers retain the program-level tsc-aligned semantics; otherwise it bypasses Phase 2 entirely. lintedFileCount preserves the historical return value of files actually visited by lint rules.

Types

type ConfiguredRule

type ConfiguredRule struct {
	Name             string
	Settings         map[string]interface{}
	Severity         rule.DiagnosticSeverity
	RequiresTypeInfo bool
	Run              func(ctx rule.RuleContext) rule.RuleListeners
}

func FilterNonTypeAwareRules added in v0.4.0

func FilterNonTypeAwareRules(rules []ConfiguredRule) []ConfiguredRule

type DiagnosticHandler added in v0.1.9

type DiagnosticHandler = func(diagnostic rule.RuleDiagnostic)

type FileFilter added in v0.5.2

type FileFilter func(absPath string) bool

FileFilter is a generic "should this file be processed" predicate. nil means "everything passes".

type FileScope added in v0.5.2

type FileScope struct {
	Files []string
	Dirs  []string
}

FileScope describes user-supplied "lint targets" (CLI args).

Both fields are independently nullable:

  • nil slice → that dimension does not constrain (e.g. Files=nil means "no per-file restriction").
  • empty slice (len 0, non-nil) → that dimension matches NOTHING. This is how the CLI distinguishes "no files arg supplied" from "files arg supplied but empty".
  • both nil → all program files pass scope.
  • both empty → no program files pass scope (lint phase is silent).

FileScope only restricts the lint-rule phase. Type-check (Phase 2 of RunLinter) ignores FileScope and reports diagnostics for every file the TypeScript program loaded — see RunLinterOptions for details.

type LintMessage

type LintMessage interface {
	Fixes() []rule.RuleFix
}

type LintResult added in v0.5.0

type LintResult struct {
	LintedFileCount int32
	ExecutedRules   map[string]struct{}
}

LintResult holds the outcome of a RunLinter invocation.

func RunLinter

func RunLinter(opts RunLinterOptions) (*LintResult, error)

RunLinter runs all configured lint rules across the given programs in parallel, then optionally collects program-level type-check diagnostics aligned with `tsc --noEmit` semantics.

Phase 1 — lint rules: each program is processed via runLintRulesInProgram, with files filtered through opts.ExcludePaths, opts.Scope, opts.PerProgramFilter and the program's own owned-file set. When opts.GetRulesForFile is nil, Phase 1 is skipped entirely — no work group is created, no per-program goroutines are spawned, and no owned-file sets are built. This is how callers run a pure type-check pass (--type-check-only) without paying lint-side setup cost.

Phase 2 — type-check (skipped when opts.TypeCheck is false): each non-skipped program is handed to runTypeCheckAcrossPrograms, which aggregates diagnostics through collectNoEmitDiagnostics — a helper that mirrors compiler.GetDiagnosticsOfAnyProgram(file=nil) but enforces `tsc --noEmit` semantics regardless of whether the user's tsconfig sets noEmit. Type-check is NOT constrained by Scope / PerProgramFilter / ExcludePaths — it covers the full program just like tsc.

See RunLinterOptions for each field's zero-value semantics.

type LintSingleFileOptions added in v0.5.2

type LintSingleFileOptions struct {
	Program         *compiler.Program
	File            string
	GetRulesForFile RuleHandler
	ExcludePaths    []string
	OnDiagnostic    DiagnosticHandler
}

LintSingleFileOptions configures a single-file, single-program lint pass. Designed for IDE/LSP per-keystroke usage. Does not run type-check.

type RuleHandler added in v0.1.9

type RuleHandler = func(sourceFile *ast.SourceFile) []ConfiguredRule

type RunLinterOptions added in v0.5.2

type RunLinterOptions struct {
	Programs       []*compiler.Program
	SingleThreaded bool

	Scope            FileScope
	ExcludePaths     []string
	PerProgramFilter []FileFilter

	GetRulesForFile RuleHandler
	TypeInfoFiles   map[string]struct{}

	TypeCheck             bool
	SkipTypeCheckPrograms []bool

	OnDiagnostic DiagnosticHandler
}

RunLinterOptions configures a multi-program lint (and optional type-check) pass.

Zero-value semantics:

  • SingleThreaded=false → use the default parallel work group
  • Scope.{Files,Dirs}=nil → process all program files
  • ExcludePaths=nil → fall back to the linter default (substring match against utils.ExcludePaths). Pass an explicit empty slice to disable the default.
  • PerProgramFilter=nil → no per-program ad-hoc filter (multi-config ownership / config `ignores`). Entries within the slice may be nil individually.
  • GetRulesForFile=nil → no lint rules executed
  • TypeInfoFiles=nil → no gap-file distinction (all files may run type-aware rules)
  • TypeCheck=false → skip the type-check phase
  • SkipTypeCheckPrograms=nil → every program participates in type-check. When non-nil, must be parallel to Programs; entries set to true mark the corresponding program to be skipped (typically the gap-file fallback program with synthesized CompilerOptions).
  • OnDiagnostic=nil → diagnostics are dropped

Thread-safety: OnDiagnostic is invoked from multiple goroutines concurrently — Phase 1 fans out per program, Phase 2 (type-check) does the same. Callers MUST make their handler safe for concurrent calls (channel send, mutex-guarded slice append, sync.Map, etc.).

Jump to

Keyboard shortcuts

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