filter

package
v0.0.0-...-6ba5456 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package filter applies file and symbol filters to skip noisy error sites.

Index

Constants

This section is empty.

Variables

View Source
var DefaultSymbolGlobs = []string{

	"fmt.Print*",
	"fmt.Fprint*",
	"fmt.Sprint*",
	"fmt.Scan*",

	"log.Print*",
	"log.Output",

	"strings.Builder.Write*",
	"bytes.Buffer.Write*",
	"bytes.Buffer.Read*",

	"io.WriteString",
}

DefaultSymbolGlobs defines a list of function signatures that are commonly ignored in Go programs and thus excluded by default to reduce noise.

This list includes printing functions and memory buffer writes which, while returning errors, are rarely handled in application logic unless strict reliability determines 100% check coverage.

Functions

func GetDefaults

func GetDefaults() []string

GetDefaults returns the default symbol globs.

func GetTestingParamName

func GetTestingParamName(decl *ast.FuncDecl) string

GetTestingParamName returns the name of the testing parameter (e.g., "t", "b", "f") if the function is a valid test handler.

This is useful for refactoring tools that need to inject "t.Fatal(err)" calls instead of returns.

decl: The function declaration.

Returns the identifier name of the first parameter if it is a testing type, or empty string.

func IsTestHandler

func IsTestHandler(decl *ast.FuncDecl) bool

IsTestHandler checks if the provided function declaration matches a standard Go testing entry point signature.

It detects:

  • Unit Tests: func TestXxx(t *testing.T)
  • Benchmarks: func BenchmarkXxx(b *testing.B)
  • Fuzz Tests: func FuzzXxx(f *testing.F)
  • Test Main: func TestMain(m *testing.M)
  • Examples: func ExampleXxx()

These functions are critical because their signatures cannot be modified (e.g., adding an error return) without breaking the 'go test' runner.

decl: The function declaration to inspect.

Returns true if the function is a testing entry point.

func IsTestHelper

func IsTestHelper(decl *ast.FuncDecl) (bool, string)

IsTestHelper determines if the function declaration behaves as a test helper. A function is considered a test helper if it calls the `Helper()` method on a parameter of type `*testing.T`, `*testing.B`, or `*testing.F`.

This detection allows refactoring tools to choose `t.Fatal(err)` injection over signature modification, preserving the helper's signature (which often cannot return errors without breaking test suite patterns).

decl: The function declaration to inspect.

Returns true if `Helper()` is called, and the name of the testing parameter (e.g. "t").

Types

type Filter

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

Filter determines whether specific files or symbols should be excluded from analysis. It uses glob patterns for file paths and symbol names, and inspects file headers for generated code markers.

func New

func New(fileGlobs, symbolGlobs []string) *Filter

New creates a new Filter with the provided glob patterns.

fileGlobs: A list of patterns to match against file paths (e.g., "*_test.go", "vendor/*"). symbolGlobs: A list of patterns to match against fully qualified symbol names (e.g., "fmt.*", "github.com/pkg/errors.Wrap").

func (*Filter) MatchesFile

func (f *Filter) MatchesFile(fset *token.FileSet, pos token.Pos) (bool, error)

MatchesFile checks if the file corresponding to the provided token.Pos is excluded. It excludes files if: 1. The filename matches a configured exclude glob. 2. The file content contains a standard "Code generated ... DO NOT EDIT." header in the first 20 lines.

fset: The file set containing the position. pos: The position within the file to check.

func (*Filter) MatchesSymbol

func (f *Filter) MatchesSymbol(fn *types.Func) bool

MatchesSymbol checks if the provided function symbol is excluded. It constructs the fully qualified name (<package-path>.<function-name>) and checks against symbol globs.

fn: The function object to check.

Jump to

Keyboard shortcuts

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