๐Ÿ“ฆ BurntSushi / go-sumtype

๐Ÿ“„ main.go ยท 75 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75package main

import (
	"log"
	"os"
	"strings"

	"golang.org/x/tools/go/loader"
	"golang.org/x/tools/go/packages"
)

func main() {
	log.SetFlags(0)
	if len(os.Args) < 2 {
		// TODO: Switch this to use golang.org/x/tools/go/packages.
		log.Fatalf("Usage: go-sumtype <args>\n%s", loader.FromArgsUsage)
	}
	args := os.Args[1:]
	pkgs, err := tycheckAll(args)
	if err != nil {
		log.Fatal(err)
	}
	if errs := run(pkgs); len(errs) > 0 {
		var list []string
		for _, err := range errs {
			list = append(list, err.Error())
		}
		log.Fatal(strings.Join(list, "\n"))
	}
}

func run(pkgs []*packages.Package) []error {
	var errs []error

	decls, err := findSumTypeDecls(pkgs)
	if err != nil {
		return []error{err}
	}

	defs, defErrs := findSumTypeDefs(decls)
	errs = append(errs, defErrs...)
	if len(defs) == 0 {
		return errs
	}

	for _, pkg := range pkgs {
		if pkgErrs := check(pkg, defs); pkgErrs != nil {
			errs = append(errs, pkgErrs...)
		}
	}
	return errs
}

func tycheckAll(args []string) ([]*packages.Package, error) {
	conf := &packages.Config{
		Mode: packages.LoadSyntax,
		// Unfortunately, it appears including the test packages in
		// this lint makes it difficult to do exhaustiveness checking.
		// Namely, it appears that compiling the test version of a
		// package introduces distinct types from the normal version
		// of the package, which will always result in inexhaustive
		// errors whenever a package both defines a sum type and has
		// tests. (Specifically, using `package name`. Using `package
		// name_test` is OK.)
		//
		// It's not clear what the best way to fix this is. :-(
		Tests: false,
	}
	pkgs, err := packages.Load(conf, args...)
	if err != nil {
		return nil, err
	}
	return pkgs, nil
}