rule

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: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenerOnAllowPattern

func ListenerOnAllowPattern(kind ast.Kind) ast.Kind

TODO(port): better name

func ListenerOnExit

func ListenerOnExit(kind ast.Kind) ast.Kind

func ListenerOnNotAllowPattern

func ListenerOnNotAllowPattern(kind ast.Kind) ast.Kind

func ReportNodeWithFixesOrSuggestions

func ReportNodeWithFixesOrSuggestions(ctx RuleContext, node *ast.Node, fix bool, msg RuleMessage, suggestionMsg RuleMessage, fixes ...RuleFix)

Types

type DiagnosticSeverity

type DiagnosticSeverity int

DiagnosticSeverity represents the severity level of a diagnostic

const (
	SeverityError DiagnosticSeverity = iota
	SeverityWarning
	SeverityOff
)

func ParseSeverity

func ParseSeverity(level string) DiagnosticSeverity

ParseSeverity converts a string to DiagnosticSeverity

func (DiagnosticSeverity) Int

func (s DiagnosticSeverity) Int() int

String returns the string representation of the severity

func (DiagnosticSeverity) String

func (s DiagnosticSeverity) String() string

String returns the string representation of the severity

type DisableManager

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

DisableManager tracks which rules are disabled at different locations in a file

func NewDisableManager

func NewDisableManager(sourceFile *ast.SourceFile, comments []*ast.CommentRange) *DisableManager

NewDisableManager creates a new DisableManager for the given source file

func (*DisableManager) IsRuleDisabled

func (dm *DisableManager) IsRuleDisabled(ruleName string, pos int) bool

IsRuleDisabled checks if a rule is disabled at the given position

type Rule

type Rule struct {
	Name             string
	RequiresTypeInfo bool
	Run              func(ctx RuleContext, options any) RuleListeners
}

func CreateRule

func CreateRule(r Rule) Rule

type RuleContext

type RuleContext struct {
	SourceFile                 *ast.SourceFile
	Settings                   map[string]interface{}
	Program                    *compiler.Program
	TypeChecker                *checker.Checker
	DisableManager             *DisableManager
	ReportRange                func(textRange core.TextRange, msg RuleMessage)
	ReportRangeWithFixes       func(textRange core.TextRange, msg RuleMessage, fixes ...RuleFix)
	ReportRangeWithSuggestions func(textRange core.TextRange, msg RuleMessage, suggestions ...RuleSuggestion)
	ReportNode                 func(node *ast.Node, msg RuleMessage)
	ReportNodeWithFixes        func(node *ast.Node, msg RuleMessage, fixes ...RuleFix)
	ReportNodeWithSuggestions  func(node *ast.Node, msg RuleMessage, suggestions ...RuleSuggestion)
	// ReportNodeWithFixesAndSuggestions emits a single diagnostic carrying
	// BOTH an autofix and one or more suggestions. Used by rules that follow
	// upstream's "promote first suggestion to fix while keeping the
	// suggestion" pattern (e.g., ESLint's
	// `enableDangerousAutofixThisMayCauseInfiniteLoops` in
	// react-hooks/exhaustive-deps).
	ReportNodeWithFixesAndSuggestions func(node *ast.Node, msg RuleMessage, fixes []RuleFix, suggestions []RuleSuggestion)
	// ReportRangeWithFixesAndSuggestions is the range-keyed twin of
	// ReportNodeWithFixesAndSuggestions. Same semantics, anchors the
	// diagnostic at an explicit TextRange instead of a node's trimmed
	// range.
	ReportRangeWithFixesAndSuggestions func(textRange core.TextRange, msg RuleMessage, fixes []RuleFix, suggestions []RuleSuggestion)
}

type RuleDiagnostic

type RuleDiagnostic struct {
	Range    core.TextRange
	RuleName string
	Message  RuleMessage
	// nil if no fixes were provided
	FixesPtr *[]RuleFix
	// nil if no suggestions were provided
	Suggestions *[]RuleSuggestion
	SourceFile  *ast.SourceFile
	Severity    DiagnosticSeverity
	// PreFormatted indicates that Message.Description already contains
	// structured formatting (e.g. indented continuation lines from tsc diagnostics).
	// The renderer will use a simple 2-space indent instead of the │ border style.
	PreFormatted bool
}

func (RuleDiagnostic) Fixes

func (d RuleDiagnostic) Fixes() []RuleFix

type RuleFix

type RuleFix struct {
	Text  string
	Range core.TextRange
}

func RuleFixInsertAfter

func RuleFixInsertAfter(node *ast.Node, text string) RuleFix

func RuleFixInsertBefore

func RuleFixInsertBefore(file *ast.SourceFile, node *ast.Node, text string) RuleFix

func RuleFixRemove

func RuleFixRemove(file *ast.SourceFile, node *ast.Node) RuleFix

func RuleFixRemoveRange

func RuleFixRemoveRange(textRange core.TextRange) RuleFix

func RuleFixReplace

func RuleFixReplace(file *ast.SourceFile, node *ast.Node, text string) RuleFix

func RuleFixReplaceRange

func RuleFixReplaceRange(textRange core.TextRange, text string) RuleFix

type RuleListeners

type RuleListeners map[ast.Kind](func(node *ast.Node))

type RuleMessage

type RuleMessage struct {
	Id          string
	Description string
	// Data exposes the placeholder values that were substituted into
	// Description (e.g. ESLint's `report({ data: { type } })`). Downstream
	// reporters / IDE clients can use this for structured access. Optional —
	// rules that don't carry placeholders may leave it nil.
	Data map[string]string
}

type RuleSuggestion

type RuleSuggestion struct {
	Message  RuleMessage
	FixesArr []RuleFix
}

func (RuleSuggestion) Fixes

func (s RuleSuggestion) Fixes() []RuleFix

Jump to

Keyboard shortcuts

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