History

Force exported functions to have comments

Harshavardhana committed 11 years ago

Commit 721ec9e1 parent 3d158b11 file · +31 −9Browse files

Showing the changes to buildscripts (1 of 1 file). Show all

Unified · Split

buildscripts/deadcode.go+31−9
@@ -73,7 +73,7 @@ func doDir(name string) { · 72 unmodified lines
73 return false73 return false
74 }74 }
75 fs := token.NewFileSet()75 fs := token.NewFileSet()
76 pkgs, err := parser.ParseDir(fs, name, notests, parser.Mode(0))76 pkgs, err := parser.ParseDir(fs, name, notests, parser.ParseComments|parser.Mode(0))
77 if err != nil {77 if err != nil {
78 errorf("%s", err)78 errorf("%s", err)
79 return79 return
@@ -84,20 +84,23 @@ func doDir(name string) { · 4 unmodified lines
84}84}
85 85
86type Package struct {86type Package struct {
87 p *ast.Package87 p *ast.Package
88 fs *token.FileSet88 fs *token.FileSet
89 decl map[string]ast.Node89 decl map[string]ast.Node
90 used map[string]bool90 missingcomments map[string]ast.Node
91 used map[string]bool
91}92}
92 93
93func doPackage(fs *token.FileSet, pkg *ast.Package) {94func doPackage(fs *token.FileSet, pkg *ast.Package) {
94 p := &Package{95 p := &Package{
95 p: pkg,96 p: pkg,
96 fs: fs,97 fs: fs,
97 decl: make(map[string]ast.Node),98 decl: make(map[string]ast.Node),
98 used: make(map[string]bool),99 missingcomments: make(map[string]ast.Node),
100 used: make(map[string]bool),
99 }101 }
100 for _, file := range pkg.Files {102 for _, file := range pkg.Files {
103 cmap := ast.NewCommentMap(fs, file, file.Comments)
101 for _, decl := range file.Decls {104 for _, decl := range file.Decls {
102 switch n := decl.(type) {105 switch n := decl.(type) {
103 case *ast.GenDecl:106 case *ast.GenDecl:
@@ -115,6 +118,20 @@ func doPackage(fs *token.FileSet, pkg *ast.Package) { · 11 unmodified lines
115 }118 }
116 }119 }
117 case *ast.FuncDecl:120 case *ast.FuncDecl:
121 // Skip if function is 'main'
122 if n.Name.Name == "main" {
123 continue
124 }
125 // Skip non-exported functions
126 if !ast.IsExported(n.Name.Name) {
127 continue
128 }
129 // Check if comments are missing from exported functions
130 _, ok := cmap[n]
131 if ok == false {
132 p.missingcomments[n.Name.Name] = n
133 }
134
118 // function declarations135 // function declarations
119 // TODO(remy): do methods136 // TODO(remy): do methods
120 if n.Recv == nil {137 if n.Recv == nil {
@@ -140,6 +157,7 @@ func doPackage(fs *token.FileSet, pkg *ast.Package) { · 19 unmodified lines
140 // walk file looking for used nodes.157 // walk file looking for used nodes.
141 ast.Walk(p, file)158 ast.Walk(p, file)
142 }159 }
160
143 // reports.161 // reports.
144 reports := Reports(nil)162 reports := Reports(nil)
145 for name, node := range p.decl {163 for name, node := range p.decl {
@@ -151,6 +169,10 @@ func doPackage(fs *token.FileSet, pkg *ast.Package) { · 5 unmodified lines
151 for _, report := range reports {169 for _, report := range reports {
152 errorf("%s: %s is unused", fs.Position(report.pos), report.name)170 errorf("%s: %s is unused", fs.Position(report.pos), report.name)
153 }171 }
172
173 for name, node := range p.missingcomments {
174 errorf("%s: comment is missing for 'func %s'", fs.Position(node.Pos()), name)
175 }
154}176}
155 177
156type Report struct {178type Report struct {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t