History

Force exported functions to have comments

Harshavardhana committed 11 years ago

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

Unified · Split

buildscripts/deadcode.go+31−9
@@ -73,7 +73,7 @@ func doDir(name string) { · 72 unmodified lines
7373 return false
7474 }
7575 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))
7777 if err != nil {
7878 errorf("%s", err)
7979 return
@@ -84,20 +84,23 @@ func doDir(name string) { · 4 unmodified lines
8484 }
8585
8686 type Package struct {
87− p *ast.Package
88− fs *token.FileSet
89− decl map[string]ast.Node
90− used map[string]bool
87+ p *ast.Package
88+ fs *token.FileSet
89+ decl map[string]ast.Node
90+ missingcomments map[string]ast.Node
91+ used map[string]bool
9192 }
9293
9394 func doPackage(fs *token.FileSet, pkg *ast.Package) {
9495 p := &Package{
95− p: pkg,
96− fs: fs,
97− decl: make(map[string]ast.Node),
98− used: make(map[string]bool),
96+ p: pkg,
97+ fs: fs,
98+ decl: make(map[string]ast.Node),
99+ missingcomments: make(map[string]ast.Node),
100+ used: make(map[string]bool),
99101 }
100102 for _, file := range pkg.Files {
103+ cmap := ast.NewCommentMap(fs, file, file.Comments)
101104 for _, decl := range file.Decls {
102105 switch n := decl.(type) {
103106 case *ast.GenDecl:
@@ -115,6 +118,20 @@ func doPackage(fs *token.FileSet, pkg *ast.Package) { · 11 unmodified lines
115118 }
116119 }
117120 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+
118135 // function declarations
119136 // TODO(remy): do methods
120137 if n.Recv == nil {
@@ -140,6 +157,7 @@ func doPackage(fs *token.FileSet, pkg *ast.Package) { · 19 unmodified lines
140157 // walk file looking for used nodes.
141158 ast.Walk(p, file)
142159 }
160+
143161 // reports.
144162 reports := Reports(nil)
145163 for name, node := range p.decl {
@@ -151,6 +169,10 @@ func doPackage(fs *token.FileSet, pkg *ast.Package) { · 5 unmodified lines
151169 for _, report := range reports {
152170 errorf("%s: %s is unused", fs.Position(report.pos), report.name)
153171 }
172+
173+ for name, node := range p.missingcomments {
174+ errorf("%s: comment is missing for 'func %s'", fs.Position(node.Pos()), name)
175+ }
154176 }
155177
156178 type 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