typecover

package module
v0.9.8 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2022 License: MIT Imports: 8 Imported by: 0

README

typecover

typecover is a go linter that checks if a code block is assigning to all exported fields and calling all exported methods of a struct, or calling all exported methods of an interface.

It is useful in cases where code wants to be aware of any newly added members.

Install

go get -u github.com/kisunji/typecover/cmd/typecover

Usage

Using the CLI

typecover [package/file]

Comment directives

# Local Type
// typecover:TypeName

# Imported Type
// typecover:pkg.TypeName 

# Excluding members
// typecover:TypeName -exclude Field3,Field4,Field5

typecover:YourType will check for the existence of all exported members of YourType in the comment's associated code block (for details on how comments are associated with code, see here).

Examples

Struct assignment
type MyStruct struct {
	MyField1 string
	MyField2 string
	myField3 string
}

// typecover:MyStruct
m := MyStruct{ 
    MyField1: "hello",
}
Type example.MyStruct is missing MyField2
Covering a code block

The typecover annotation can be placed at a higher level (func, if-stmt, for-loop) to cover the whole block.

// typecover:MyStruct
func example() {
    m := MyStruct{}
    m.MyField2 = "world"    
}
Type example.MyStruct is missing MyField1
Using imported types
// typecover:flag.Flag
f := flag.Flag{ 
    Name:  "test",
    Usage: "usage instructions",
    Value: nil,
}
Type flag.Flag is missing DefValue
Copying fields from exported type
//typecover:flag.Flag
func cloneFlag(f flag.Flag) MyFlag {
    return MyFlag{
        Name: f.Name,
        Usage: f.Usage,
        Value: f.Value,
    }
}
Type flag.Flag is missing DefValue
Enforcing all methods in a Builder interface
type ExampleBuilder interface {
	Option1() ExampleBuilder
	Option2() ExampleBuilder
	Build() Example
}

//typecover:ExampleBuilder
func MakeExample() Example {
	b := NewExampleBuilder().Option2()
	return b.Build()
}
Type example.ExampleBuilder is missing Option1
Exclude a member from being checked
type ExampleBuilder interface {
	Option1() ExampleBuilder
	Option2() ExampleBuilder
	Build() Example
}

//typecover:ExampleBuilder -exclude Option1
func MakeExample() Example {
	b := NewExampleBuilder().Option2()
	return b.Build()
}
(passes!)

Credits

https://github.com/mbilski/exhaustivestruct

https://github.com/reillywatson/enumcover

Documentation

Index

Constants

View Source
const Doc = `` /* 131-byte string literal not displayed */

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Doc:      Doc,
	Name:     "typecover",
	Run:      run,
	Requires: []*analysis.Analyzer{inspect.Analyzer},
}

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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