analyzer

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package analyzer implements the quasi-enum type safety analyzer.

Package analyzer implements the quasi-enum type safety analyzer.

Package analyzer implements the quasi-enum type safety analyzer.

Package analyzer implements the quasi-enum type safety analyzer.

Package analyzer implements the quasi-enum type safety analyzer.

Package analyzer implements the quasi-enum type safety analyzer.

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name:     "enumsafety",
	Doc:      "check that quasi-enum types are only assigned their defined constants and satisfy definition constraints",
	URL:      "https://github.com/Djarvur/go-enumsafety",
	Requires: []*analysis.Analyzer{inspect.Analyzer},
	Run:      run,
}

Analyzer is the quasi-enum type safety analyzer.

Functions

This section is empty.

Types

type ConstraintConfig

type ConstraintConfig struct {
	MinConstantsEnabled   bool
	SameConstBlockEnabled bool
	SameFileEnabled       bool
	ExclusiveBlockEnabled bool
	ProximityEnabled      bool
}

ConstraintConfig holds configuration for definition constraints.

func NewConstraintConfig

func NewConstraintConfig() *ConstraintConfig

NewConstraintConfig creates a new ConstraintConfig with defaults.

type DefinitionConstraint

type DefinitionConstraint int

DefinitionConstraint represents a constraint that quasi-enum definitions must satisfy.

const (
	DC001MinConstants DefinitionConstraint = iota
	DC002SameConstBlock
	DC003SameFile
	DC004ExclusiveConstBlock
	DC005Proximity
)

func (DefinitionConstraint) String

func (dc DefinitionConstraint) String() string

type DetectionConfig

type DetectionConfig struct {
	ConstantsDetectionEnabled        bool
	SuffixDetectionEnabled           bool
	InlineCommentDetectionEnabled    bool
	PrecedingCommentDetectionEnabled bool
	NamedCommentDetectionEnabled     bool
}

DetectionConfig holds configuration for detection techniques.

func NewDetectionConfig

func NewDetectionConfig() *DetectionConfig

NewDetectionConfig creates a new DetectionConfig with defaults.

type DetectionTechnique

type DetectionTechnique int

DetectionTechnique represents the technique used to identify a quasi-enum type.

const (
	DT001ConstantsBased DetectionTechnique = iota
	DT002NameSuffix
	DT003InlineComment
	DT004PrecedingComment
	DT005NamedComment
)

func (DetectionTechnique) String

func (dt DetectionTechnique) String() string

type EnumConstant

type EnumConstant struct {
	Name          string
	Value         constant.Value
	QuasiEnumType *types.Named
	Position      token.Pos
	IsIota        bool
	Expression    string
	ConstBlock    *ast.GenDecl
}

EnumConstant represents a valid constant value for a quasi-enum type.

type QuasiEnumRegistry

type QuasiEnumRegistry struct {
	QuasiEnums       map[*types.Named]*QuasiEnumType
	ConstantLookup   map[*types.Named]map[string]*EnumConstant
	Packages         map[string][]*QuasiEnumType
	DetectionConfig  *DetectionConfig
	ConstraintConfig *ConstraintConfig
}

QuasiEnumRegistry is the central registry of all quasi-enum types.

func NewQuasiEnumRegistry

func NewQuasiEnumRegistry(detectionConfig *DetectionConfig, constraintConfig *ConstraintConfig) *QuasiEnumRegistry

NewQuasiEnumRegistry creates a new registry.

func (*QuasiEnumRegistry) GetEnumConstants

func (r *QuasiEnumRegistry) GetEnumConstants(t *types.Named) []EnumConstant

GetEnumConstants returns the valid constants for a quasi-enum type.

func (*QuasiEnumRegistry) IsQuasiEnumType

func (r *QuasiEnumRegistry) IsQuasiEnumType(t types.Type) bool

IsQuasiEnumType checks if a type is a quasi-enum.

func (*QuasiEnumRegistry) RegisterQuasiEnum

func (r *QuasiEnumRegistry) RegisterQuasiEnum(qe *QuasiEnumType)

RegisterQuasiEnum adds a quasi-enum to the registry.

type QuasiEnumType

type QuasiEnumType struct {
	Type           *types.Named    // The named type
	TypeDef        *types.TypeName // Type definition object
	UnderlyingType types.BasicKind
	PackagePath    string
	Constants      []EnumConstant
	Position       token.Pos
	DetectedBy     []DetectionTechnique
	TypeDecl       *ast.GenDecl // Type declaration node (for constraint validation)
	ConstBlock     *ast.GenDecl // Const block node (for constraint validation)
	File           *ast.File    // File containing the type (for constraint validation)

	// Helper method tracking (US5, US6)
	HasStringMethod        bool // Whether type has String() string method
	HasUnmarshalTextMethod bool // Whether type has UnmarshalText([]byte) error method

	// FR-047: Suggest adding enum comment when detected only by constants-based
	SuggestEnumComment bool
}

QuasiEnumType represents a detected quasi-enum type with its constants and metadata.

func (*QuasiEnumType) ValidateConstraints

func (qe *QuasiEnumType) ValidateConstraints(
	config *ConstraintConfig,
	fset *token.FileSet,
	typeDecl *ast.GenDecl,
	constDecl *ast.GenDecl,
	file *ast.File,
	info *types.Info,
) []DefinitionConstraint

ValidateConstraints validates all enabled constraints for a quasi-enum type. Returns a slice of constraint violations.

type Violation

type Violation struct {
	Type          ViolationType
	Position      token.Pos
	QuasiEnumType *types.Named
	InvalidValue  ast.Expr
	Constraint    *DefinitionConstraint
	Context       ViolationContext
	SuggestedFix  string
}

Violation represents a detected violation.

type ViolationContext

type ViolationContext struct {
	VariableName      string
	FunctionName      string
	ParameterName     string
	Statement         string
	LineNumber        int
	ConstraintDetails string
}

ViolationContext provides additional context for a violation.

type ViolationType

type ViolationType int

ViolationType represents the category of violation.

const (
	// Usage violations
	VTLiteralAssignment ViolationType = iota
	VTLiteralConversion
	VTLiteralArgument
	VTLiteralCompositeField
	VTUntypedConstant
	VTVariableConversion

	// Constraint violation
	VTConstraint
)

func (ViolationType) String

func (vt ViolationType) String() string

Jump to

Keyboard shortcuts

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