This repository has been archived by the owner on Oct 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
185 lines (153 loc) · 3.87 KB
/
main.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"path/filepath"
"strings"
"unicode"
)
type funcdecl struct {
name string
paramCount int
pos token.Pos
}
type options struct {
excludePatterns []string
excludeTests bool
paramLimitPrivate int
paramLimitPublic int
recursive bool
}
type printer interface {
Print(msgs ...interface{})
}
func main() {
logger := log.New(os.Stdout, "goparamcount: ", 0)
flag.Usage = func() { printUsage(logger) }
flag.Parse()
args := flag.Args()
if len(args) == 0 {
printUsage(logger)
os.Exit(missingArgumentExitCode)
}
issues, err := run(args, logger)
if err != nil {
os.Exit(invalidArgumentExitCode)
} else if len(issues) > 0 {
printAll(logger, issues)
if *flagSetExitStatus || *flagSetExitStatusAlias {
os.Exit(setExitStatusExitCode)
}
}
}
func run(paths []string, logger *log.Logger) (issues []string, err error) {
if !(*flagVerbose || *flagVerboseAlias) {
logger = noopLogger
}
if noLimitIsSet(*flagMax, *flagMaxAlias, *flagPrivateMax, *flagPublicMax) {
*flagMax = defaultParamLimit
}
*flagExcludes += *flagExcludesAlias
excludePatterns := strings.Split(*flagExcludes, ",")
if err := checkPatterns(excludePatterns); err != nil {
logger.Printf("invalid pattern(s): %s", err)
}
baseOptions := &options{
excludePatterns: excludePatterns,
excludeTests: !(*flagTests || *flagTestsAlias),
paramLimitPrivate: min(*flagMax, *flagMaxAlias, *flagPrivateMax),
paramLimitPublic: min(*flagMax, *flagMaxAlias, *flagPublicMax),
}
return runWith(paths, baseOptions, logger)
}
func runWith(
paths []string,
baseOptions *options,
logger *log.Logger,
) (issues []string, err error) {
for _, path := range paths {
absPath, err := filepath.Abs(path)
if err != nil {
return []string{}, fmt.Errorf("invalid path %s", path)
}
root, recursive := checkRecursive(absPath)
baseOptions.recursive = recursive
pathIssues := analyzeWith(root, baseOptions, logger)
issues = append(issues, pathIssues...)
}
return issues, nil
}
func analyzeWith(
path string,
options *options,
logger *log.Logger,
) (issues []string) {
for _, filePath := range getFiles(path, options) {
logger.Printf("analyzing %s", filePath)
fileIssues, err := analyzeFile(filePath, options)
if err != nil {
logger.Printf("error parsing %s", filePath)
} else {
issues = append(issues, fileIssues...)
}
}
return issues
}
func analyzeFile(path string, options *options) (issues []string, err error) {
fileSet := token.NewFileSet()
file, err := parser.ParseFile(fileSet, path, nil, 0)
if err != nil {
return nil, err
}
for _, decl := range checkForParamLimit(file, options) {
issues = append(issues, constructMessage(fileSet, decl))
}
return issues, nil
}
func checkForParamLimit(file *ast.File, options *options) (issues []*funcdecl) {
for _, decl := range file.Decls {
issue := checkDecl(decl, options)
if issue != nil {
issues = append(issues, issue)
}
}
return issues
}
func checkDecl(d ast.Decl, options *options) *funcdecl {
decl, ok := d.(*ast.FuncDecl)
if !ok {
return nil
}
paramLimit := options.paramLimitPrivate
if isPublicFunc(decl) {
paramLimit = options.paramLimitPublic
}
paramCount := getParamCount(decl)
if paramCount <= paramLimit {
return nil
}
return &funcdecl{
name: decl.Name.String(),
paramCount: paramCount,
pos: decl.Pos(),
}
}
func isPublicFunc(decl *ast.FuncDecl) bool {
name := []rune(decl.Name.String())
return unicode.IsUpper(name[0])
}
func getParamCount(decl *ast.FuncDecl) int {
paramCount := 0
for _, param := range decl.Type.Params.List {
// Multiple parameters with the same type, as in `func(a, b int)` will
// appear as a single "param" in the `Params.List`. Therefore, we instead
// count the number of `Names` per "param".
paramCount += len(param.Names)
}
return paramCount
}