custos

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package custos is the Go API for rules written in Go. A program that imports it and calls Main is a complete custos binary (analyse, fix, explain, rules, the language server and the MCP server) that also runs its own rules:

package main

import (
	"github.com/janalis/custos/pkg/custos"
	"github.com/janalis/custos/pkg/custos/ast"
)

func main() {
	noEval := custos.MustRule(custos.Info{ID: "NoEval", Summary: "eval runs arbitrary code."},
		[]ast.NodeKind{ast.KEval},
		func(ctx *custos.Context, n ast.Node) { ctx.ReportNode(n, "Remove eval") })
	custos.Main("my-custos", noEval)
}

Rules can also be declared as patterns, as in custos.json (Pattern).

The API is unstable until custos 1.0: it exposes the engine's own types.

Index

Constants

View Source
const (
	SeverityError   = diagnostic.SeverityError
	SeverityWarning = diagnostic.SeverityWarning
	SeverityInfo    = diagnostic.SeverityInfo
)

Severities.

View Source
const Group = pattern.Group

Group is the rule group of every rule outside the built-in catalogue.

Variables

This section is empty.

Functions

func Main

func Main(version string, rules ...Rule)

Main runs the custos command line on the process arguments with rules added to the built-in ones, and exits with its status.

func Run

func Run(version string, args []string, stdin io.Reader, stdout, stderr io.Writer, rules ...Rule) int

Run runs the custos command line with args (without the program name) and rules added to the built-in ones, and returns its exit status.

Types

type Context

type Context = analysis.Context

Context gives a rule the file under analysis, name resolution, type inference and reporting.

type Described

type Described = analysis.Described

Described is implemented by rules carrying their own facts, as every rule outside the built-in catalogue must.

type Description

type Description = meta.Description

Description is a rule's documentation.

type Facts

type Facts = meta.Rule

Facts are a rule's catalogue facts.

type FileRule

type FileRule = analysis.FileRule

FileRule is implemented by rules that inspect a file as a whole.

type Fix

type Fix = diagnostic.Fix

Fix is a quick-fix: a title and lazily built edits.

type Info

type Info struct {
	ID       string   // an uppercase letter, then letters and digits
	Severity Severity // default SeverityWarning
	Disabled bool     // off unless enabled in custos.json or run with --rule
	Summary  string   // shown by custos explain and in SARIF output
	HasFix   bool     // the rule offers quick-fixes
	PHPMin   string   // minimum target PHP version, documentation only
	// PartialCalls has the rule also check calls that only build a closure:
	// first-class callables and partial applications (`f(...)`, `f(?, 1)`,
	// PHP 8.6), whose ArgList.Partial() is true and whose arguments may be
	// ArgPlaceholder or VariadicPlaceholder nodes. Without it, such calls
	// never reach the rule.
	PartialCalls bool
}

Info describes a rule written in Go.

type PatternDefinition

type PatternDefinition = pattern.Definition

PatternDefinition declares a pattern rule, as in custos.json.

type Patterns

type Patterns = pattern.Patterns

Patterns is one pattern or a list of them.

type Rule

type Rule = analysis.Rule

Rule is one inspection. Implementations must be stateless and safe for concurrent use; per-file state lives in Context.

func MustRule

func MustRule(info Info, kinds []ast.NodeKind, check func(ctx *Context, n ast.Node)) Rule

MustRule is NewRule for rules declared at startup: it panics on an invalid rule.

func NewRule

func NewRule(info Info, kinds []ast.NodeKind, check func(ctx *Context, n ast.Node)) (Rule, error)

NewRule returns a rule calling check for each node of the given kinds.

func Pattern

func Pattern(d PatternDefinition) (Rule, error)

Pattern compiles a rule declared as a code pattern (see the custom rules guide).

type SemanticRule

type SemanticRule = analysis.SemanticRule

SemanticRule marks rules that need the project's declarations (other files, dependencies) indexed before analysis.

type Severity

type Severity = diagnostic.Severity

Severity is a finding's level.

type TextEdit

type TextEdit = diagnostic.TextEdit

TextEdit replaces a byte range of the source.

type Where

type Where = pattern.Where

Where constrains a pattern metavariable.

Directories

Path Synopsis
Package ast exposes the PHP syntax tree that custom rules written in Go inspect: node types and kinds, token kinds and navigation helpers.
Package ast exposes the PHP syntax tree that custom rules written in Go inspect: node types and kinds, token kinds and navigation helpers.

Jump to

Keyboard shortcuts

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