nit

package module
v0.6.5 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2020 License: MIT Imports: 8 Imported by: 0

README

nit

Go Report Card Circle CI coverage

nit is an opinionated Code Organization linter for Go.

It defines a collection of rules to determine how each Go file should be organized internally.

Rules

  1. imports is the first section
    • Requires parenthesized declaration,
    • One maximum, and
    • Separated in 3 blocks: standard, external and same package (local).
  2. type is the second section
    • Requires parenthesized declaration,
    • Section must be sorted: exported first, then unexported; and
    • Supports //- comment for separating groups
  3. const is the third section
    • Requires parenthesized declaration,
    • Multiple allowed, and
    • Section must be sorted: exported first, then unexported.
  4. var is the fourth section
    • Requires parenthesized declaration, and
    • Section must be sorted: exported first, then unexported.
  5. func is the fifth section
    • Must be sorted, exported first, then unexported, and
    • Supports //- comment for separating groups.
  6. func method, is the sixth section
    • Must be sorted by type, exported first, then unexported; and
    • Supports //- comment for separating groups.

Fancy State Machine explaining the rules above:

code

Installing

  • Using go (>= 1.13): go get github.com/MarioCarrion/nit/cmd/nit@v0.6.0, for installing v0.6.0 for example; see the releases for other versions.
  • Using it as a dependency with the tools.go paradigm:
// +build tools

package tools

import (
	_ "github.com/MarioCarrion/nit/cmd/nit"
)

Using

After installing you can use:

nit -pkg <base local package> $(go list ./...)

Please use nit -h for other available arguments.

Development requirements

Go >= 1.14.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BreakComments added in v0.2.0

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

BreakComments defines all the found break-like comments in the file.

func NewBreakComments added in v0.2.0

func NewBreakComments(fset *token.FileSet, comments []*ast.CommentGroup) *BreakComments

NewBreakComments returns all the valid break-like comments.

func (*BreakComments) HasGeneratedCode added in v0.4.0

func (c *BreakComments) HasGeneratedCode() bool

HasGeneratedCode indicates whether the current file contains a "code generated expression".

func (*BreakComments) HasNoLintNit added in v0.6.4

func (c *BreakComments) HasNoLintNit() bool

HasNoLintNit indicates whether the current file contains a "do not lint nit" expression.

func (*BreakComments) MoveTo added in v0.2.0

func (c *BreakComments) MoveTo(line int)

Moves current line cursor to the received line.

func (*BreakComments) Next added in v0.2.0

func (c *BreakComments) Next() int

Returns the next break line if any, when no more left it returns -1.

type ConstsValidator added in v0.1.3

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

ConstsValidator defines the type including the rules used for validating the `const` sections.

func (*ConstsValidator) Validate added in v0.1.3

func (c *ConstsValidator) Validate(v *ast.GenDecl, fset *token.FileSet) error

Validate makes sure the implemented `const` declaration satisfies the following rules: * Group declaration is parenthesized * Declarations are sorted

type FileSection added in v0.3.0

type FileSection uint8

FileSection represents a code section.

const (
	// FileSectionImports defines the `imports` state.
	FileSectionImports FileSection = iota // FIXME kill

	// FileSectionTypes defines the `type` state.
	FileSectionTypes

	// FileSectionConsts defines the `const` state.
	FileSectionConsts

	// FileSectionVars defines the `var` state.
	FileSectionVars

	// FileSectionFuncs defines the `func` state.
	FileSectionFuncs

	// SectionMethods defines the `method` state.
	FileSectionMethods
)

func NewFuncDeclFileSection added in v0.3.0

func NewFuncDeclFileSection(decl *ast.FuncDecl) (FileSection, error)

NewFuncDeclFileSection returns a new State that matches the decl type.

func NewGenDeclFileSection added in v0.3.0

func NewGenDeclFileSection(decl *ast.GenDecl) (FileSection, error)

NewGenDeclFileSection returns a new State that matches the decl type.

type FileSectionMachine added in v0.3.0

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

FileSectionMachine represents the state machine determining the right order for the sections in file.

func NewFileSectionMachine added in v0.3.0

func NewFileSectionMachine(start FileSection) (*FileSectionMachine, error)

NewFileSectionTransition returns a new transition corresponding to the received value.

func (*FileSectionMachine) Transition added in v0.3.0

func (v *FileSectionMachine) Transition(next FileSection) error

Transition updates the internal state.

type FileSectionTransition added in v0.3.0

type FileSectionTransition interface {
	Imports() (FileSectionTransition, error)
	Types() (FileSectionTransition, error)
	Consts() (FileSectionTransition, error)
	Vars() (FileSectionTransition, error)
	Funcs() (FileSectionTransition, error)
	Methods() (FileSectionTransition, error)
}

FileSectionTransition represents one of the 6 valid sections in the current file, it defines the State Machine transition rules for that concrete section, the order to be expected is:

"Imports" -> "Types" -> "Consts" -> "Vars" -> "Funcs" -> "Methods"

func NewFileSectionTransition added in v0.3.0

func NewFileSectionTransition(s FileSection) (FileSectionTransition, error)

NewFileSectionTransition returns a new transition corresponding to the received value.

type FuncsValidator added in v0.2.0

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

FuncsValidator defines the type including the rules used for validating functions.

func NewFuncsValidator added in v0.3.0

func NewFuncsValidator(c *BreakComments) *FuncsValidator

NewFuncsValidator returns a correctly initialized FuncsValidator.

func (*FuncsValidator) Validate added in v0.2.0

func (f *FuncsValidator) Validate(v *ast.FuncDecl, fset *token.FileSet) error

Validate makes sure the implemented function satisfies the following rules considering all previous declared functions: * Sorted exported functions are declared first, * Sorted unexported functions are declared next, and * Both groups can declare their own sorted subgroups,

type ImportsSection

type ImportsSection uint8

ImportsSection represents one of the 3 valid `imports` sections.

const (
	// ImportsSectionStd represents the Standard Library Packages `imports` section.
	ImportsSectionStd ImportsSection = iota

	// ImportsSectionExternal represents the External Packages `imports` section.
	ImportsSectionExternal

	// ImportsSectionLocal represents the local Packages `imports` section.
	ImportsSectionLocal
)

func NewImportsSection

func NewImportsSection(path, localPathPrefix string) ImportsSection

NewImportsSection returns the value representing the corresponding Imports section.

type ImportsSectionMachine

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

ImportsSectionMachine represents the `imports` code organization state machine.

func NewImportsSectionMachine

func NewImportsSectionMachine(start ImportsSection) (*ImportsSectionMachine, error)

NewImportsSectionMachine returns a new ImportsSectionMachine with the initial state as `start`.

func (*ImportsSectionMachine) Current

Current returns the current state.

func (*ImportsSectionMachine) Previous

Previous returns the previous state.

func (*ImportsSectionMachine) Transition

func (s *ImportsSectionMachine) Transition(next ImportsSection) error

Transition updates the internal state.

type ImportsTransition added in v0.3.0

type ImportsTransition interface {
	External() (ImportsTransition, error)
	Local() (ImportsTransition, error)
	Standard() (ImportsTransition, error)
}

ImportsTransition represents one of the 3 valid `imports` sections, it defines the State Machine transition rules for that concrete section, the order to be expected is: "Standard" -> "External" -> "Local".

func NewImportsTransition added in v0.3.0

func NewImportsTransition(s ImportsSection) (ImportsTransition, error)

NewImportsTransition returns a new transition corresponding to the received value.

type ImportsValidator

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

ImportsValidator defines the type including the rules used for validating the `imports` section as a whole.

func NewImportsValidator added in v0.3.0

func NewImportsValidator(localPath string) ImportsValidator

NewImportsValidator returns a new instalce of ImporstValidator with the local path prefix set.

func (*ImportsValidator) Validate

func (i *ImportsValidator) Validate(v *ast.GenDecl, fset *token.FileSet) error

Validate makes sure the implemented `imports` declaration satisfies the following rules: * Group declaration is parenthesized * Packages are separated by a breaking line like this:

  • First standard packages,
  • Next external packages, and
  • Finally local packages

type MethodsValidator added in v0.3.0

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

MethodsValidator defines the type including the rules used for validating methods.

func NewMethodsValidator added in v0.3.0

func NewMethodsValidator(c *BreakComments, t TypesFound) (*MethodsValidator, error)

NewMethodsValidator returns a correctly initialized MethodsValidator.

func (*MethodsValidator) Validate added in v0.3.0

func (m *MethodsValidator) Validate(v *ast.FuncDecl, fset *token.FileSet) error

Validate makes sure the implemented methods satisfies the following rules considering all previous declared methods: * Methods for exported types are declared first, then unexported ones, * Sorted exported methods are declared first, * Sorted unexported methods are declared next, and * Both groups can declare their own sorted subgroups.

type Nitpicker

type Nitpicker struct {
	LocalPath         string
	SkipGeneratedFile bool
	NoLint            bool
	// contains filtered or unexported fields
}

Nitpicker defines the linter.

func (*Nitpicker) Validate

func (v *Nitpicker) Validate(filename string) error

Validate nitpicks the filename.

type TypesFound added in v0.3.0

type TypesFound interface {
	Types() []string
}

TypesFound defines the type returning the types found in the file.

type TypesValidator added in v0.1.3

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

TypesValidator defines the type including the rules used for validating the `type` sections.

func NewTypesValidator added in v0.5.0

func NewTypesValidator(c *BreakComments) *TypesValidator

NewTypesValidator returns a correctly initialized TypesValidator.

func (*TypesValidator) Types added in v0.3.0

func (tv *TypesValidator) Types() []string

Types returns the names of all found types.

func (*TypesValidator) Validate added in v0.1.3

func (tv *TypesValidator) Validate(v *ast.GenDecl, fset *token.FileSet) error

Validate makes sure the implemented `type` declaration satisfies the following rules: * Group declaration is parenthesized * Sorted exported types are declared first, and * Sorted unexported types are declared next

type VarsValidator added in v0.1.3

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

VarsValidator defines the type including the rules used for validating the `var` sections.

func (*VarsValidator) Validate added in v0.1.3

func (c *VarsValidator) Validate(v *ast.GenDecl, fset *token.FileSet) error

Validate makes sure the implemented `var` declaration satisfies the following rules: * Group declaration is parenthesized * Sorted exported vars are declared first, and * Sorted unexported vars are declared next

Directories

Path Synopsis
cmd
nit

Jump to

Keyboard shortcuts

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