forked from segmentio/golines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotation.go
99 lines (87 loc) · 2.47 KB
/
annotation.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
package main
import (
"fmt"
"strconv"
"strings"
"github.com/dave/dst"
)
const annotationPrefix = "// __golines:shorten:"
// CreateAnnotation generates the text of a comment that will annotate long lines.
func CreateAnnotation(length int) string {
return fmt.Sprintf(
"%s%d",
annotationPrefix,
length,
)
}
// IsAnnotation determines whether the given line is an annotation created with CreateAnnotation.
func IsAnnotation(line string) bool {
return strings.HasPrefix(
strings.Trim(line, " \t"),
annotationPrefix,
)
}
// HasAnnotation determines whether the given AST node has a line length annotation on it.
func HasAnnotation(node dst.Node) bool {
startDecorations := node.Decorations().Start.All()
return len(startDecorations) > 0 &&
IsAnnotation(startDecorations[len(startDecorations)-1])
}
// HasTailAnnotation determines whether the given AST node has a line length annotation at its
// end. This is needed to catch long function declarations with inline interface definitions.
func HasTailAnnotation(node dst.Node) bool {
endDecorations := node.Decorations().End.All()
return len(endDecorations) > 0 &&
IsAnnotation(endDecorations[len(endDecorations)-1])
}
// HasAnnotationRecursive determines whether the given node or one of its children has a
// golines annotation on it. It's currently implemented for function declarations, fields,
// call expressions, and selector expressions only.
func HasAnnotationRecursive(node dst.Node) bool {
if HasAnnotation(node) {
return true
}
switch n := node.(type) {
case *dst.FuncDecl:
if n.Type != nil && n.Type.Params != nil {
for _, item := range n.Type.Params.List {
if HasAnnotationRecursive(item) {
return true
}
}
}
case *dst.Field:
return HasTailAnnotation(n) || HasAnnotationRecursive(n.Type)
case *dst.SelectorExpr:
return HasAnnotation(n.Sel) || HasAnnotation(n.X)
case *dst.CallExpr:
if HasAnnotationRecursive(n.Fun) {
return true
}
for _, arg := range n.Args {
if HasAnnotation(arg) {
return true
}
}
case *dst.InterfaceType:
for _, field := range n.Methods.List {
if HasAnnotationRecursive(field) {
return true
}
}
}
return false
}
// ParseAnnotation returns the line length encoded in a golines annotation. If none is found,
// it returns -1.
func ParseAnnotation(line string) int {
if IsAnnotation(line) {
components := strings.SplitN(line, ":", 3)
val, err := strconv.Atoi(components[2])
if err != nil {
return -1
}
return val
}
return -1
}