templint

package
v0.35.2 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package templint provides a static linter for .templ files.

It checks for common issues such as inline control flow (which templ silently drops), missing accessibility attributes, and style anti-patterns.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllRuleIDs

func AllRuleIDs() []string

AllRuleIDs returns the sorted IDs of every rule registered with the linter.

func HasErrors

func HasErrors(diags []Diagnostic) bool

HasErrors returns true if any diagnostic has Error severity.

Types

type Config

type Config struct {
	Rules map[string]Severity
}

Config holds the linter configuration. Rules maps a rule ID to the severity it should report at; rules not present in the map are disabled.

func LoadConfig

func LoadConfig(path string) (*Config, error)

LoadConfig reads a hamr.toml file and returns the parsed [lint.templ] Config. Returns nil if the file does not exist or the section is empty.

Each entry is a rule ID mapped to one of "warning", "error", or "off". An entry of "off" is equivalent to omitting the rule. Unknown rule IDs and invalid severities are returned as errors.

type Diagnostic

type Diagnostic struct {
	File     string
	Line     int
	Col      int
	Rule     string
	Severity Severity
	Message  string
}

Diagnostic represents a single lint finding.

func FilterBySeverity

func FilterBySeverity(diags []Diagnostic, minSev Severity) []Diagnostic

FilterBySeverity returns only diagnostics at or above the given severity.

func (Diagnostic) String

func (d Diagnostic) String() string

String returns a formatted diagnostic string.

type Linter

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

Linter applies rules to .templ files and collects diagnostics.

func New

func New(cfg *Config) *Linter

New creates a Linter configured by the given Config. Only rules listed in cfg.Rules are enabled; passing nil or an empty Config yields a linter that reports nothing.

func (*Linter) LintDir

func (l *Linter) LintDir(dir string) ([]Diagnostic, error)

LintDir walks a directory tree and lints all .templ files.

func (*Linter) LintFile

func (l *Linter) LintFile(path string) ([]Diagnostic, error)

LintFile lints a single file and returns diagnostics.

func (*Linter) RuleCount

func (l *Linter) RuleCount() int

RuleCount returns the number of rules registered on this Linter (the number of rules that will run on each LintFile call).

type Rule

type Rule interface {
	ID() string
	Check(filename string, lines []string) []Diagnostic
}

Rule is the interface that all lint rules must implement.

type Severity

type Severity int

Severity indicates the severity level of a diagnostic.

const (
	// Warning indicates a non-critical issue.
	Warning Severity = iota
	// Error indicates a critical issue.
	Error
)

func DefaultSeverity

func DefaultSeverity(id string) Severity

DefaultSeverity returns the recommended default severity for a rule, used when callers need to enable a rule without an explicit severity (e.g. the --rule CLI flag). Returns Warning for unknown rule IDs.

func ParseSeverity

func ParseSeverity(s string) (Severity, error)

ParseSeverity converts a string to a Severity.

func (Severity) String

func (s Severity) String() string

String returns the string representation of a Severity.

Jump to

Keyboard shortcuts

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