goconst

package module
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2024 License: MIT Imports: 9 Imported by: 55

README

goconst

Find repeated strings that could be replaced by a constant.

Motivation

There are obvious benefits to using constants instead of repeating strings, mostly to ease maintenance. Cannot argue against changing a single constant versus many strings.

While this could be considered a beginner mistake, across time, multiple packages and large codebases, some repetition could have slipped in.

Get Started
$ go get github.com/jgautheron/goconst/cmd/goconst
$ goconst ./...
Usage
Usage:

  goconst ARGS <directory>

Flags:

  -ignore            exclude files matching the given regular expression
  -ignore-strings    exclude strings matching the given regular expression  
  -ignore-tests      exclude tests from the search (default: true)
  -min-occurrences   report from how many occurrences (default: 2)
  -min-length        only report strings with the minimum given length (default: 3)
  -match-constant    look for existing constants matching the values
  -numbers           search also for duplicated numbers
  -min          	   minimum value, only works with -numbers
  -max          	   maximum value, only works with -numbers
  -output            output formatting (text or json)
  -set-exit-status   Set exit status to 2 if any issues are found

Examples:

  goconst ./...
  goconst -ignore "yacc|\.pb\." $GOPATH/src/github.com/cockroachdb/cockroach/...
  goconst -min-occurrences 3 -output json $GOPATH/src/github.com/cockroachdb/cockroach
  goconst -numbers -min 60 -max 512 .
Other static analysis tools
  • gogetimports: Get a JSON-formatted list of imports.
  • usedexports: Find exported variables that could be unexported.
License

MIT

Documentation

Overview

Package goconst finds repeated strings that could be replaced by a constant.

There are obvious benefits to using constants instead of repeating strings, mostly to ease maintenance. Cannot argue against changing a single constant versus many strings. While this could be considered a beginner mistake, across time, multiple packages and large codebases, some repetition could have slipped in.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	IgnoreStrings      string
	IgnoreTests        bool
	MatchWithConstants bool
	MinStringLength    int
	MinOccurrences     int
	ParseNumbers       bool
	NumberMin          int
	NumberMax          int
	ExcludeTypes       map[Type]bool
}

type ConstType

type ConstType struct {
	token.Position
	Name string
	// contains filtered or unexported fields
}

type Constants

type Constants map[string]ConstType

type ExtendedPos

type ExtendedPos struct {
	token.Position
	// contains filtered or unexported fields
}

type Issue

type Issue struct {
	Pos              token.Position
	OccurrencesCount int
	Str              string
	MatchingConst    string
}

func Run

func Run(files []*ast.File, fset *token.FileSet, cfg *Config) ([]Issue, error)

type Parser

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

func New

func New(path, ignore, ignoreStrings string, ignoreTests, matchConstant, numbers bool, numberMin, numberMax, minLength, minOccurrences int, excludeTypes map[Type]bool) *Parser

New creates a new instance of the parser. This is your entry point if you'd like to use goconst as an API.

func (*Parser) ParseTree

func (p *Parser) ParseTree() (Strings, Constants, error)

ParseTree will search the given path for occurrences that could be moved into constants. If "..." is appended, the search will be recursive.

func (*Parser) ProcessResults

func (p *Parser) ProcessResults()

ProcessResults post-processes the raw results.

type Strings

type Strings map[string][]ExtendedPos

type Type

type Type int
const (
	Assignment Type = iota
	Binary
	Case
	Return
	Call
)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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