Skip to content

Commit

Permalink
Allow missing self and out arguments (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange-42 authored Jan 28, 2025
1 parent f703349 commit f6d9203
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Features

* Adds flag/config entry `report-missing` to report missing docstrings and coverage (#136)
* Adds flag/config entry `report-missing` to report missing docstrings and coverage (#136, #137)

### Documentation

Expand Down
2 changes: 1 addition & 1 deletion document/doctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (proc *Processor) extractDocTestsMarkdown(baseDir string, build bool) error
return err
}
if proc.Config.TestOutput != "" {
fmt.Printf("Extracted %d test(s) from Markdown files.\n", len(proc.docTests))
err = proc.writeDocTests(proc.Config.TestOutput)
if err != nil {
return err
Expand Down Expand Up @@ -104,7 +105,6 @@ func (proc *Processor) writeDocTests(dir string) error {
return err
}
}
fmt.Printf("Extracted %d tests.\n", len(proc.docTests))
return nil
}

Expand Down
11 changes: 10 additions & 1 deletion document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ type Package struct {
}

func (p *Package) CheckMissing(path string, stats *missingStats) (missing []missingDocs) {
newPath := fmt.Sprintf("%s.%s", path, p.Name)
newPath := p.Name
if len(path) > 0 {
newPath = fmt.Sprintf("%s.%s", path, p.Name)
}
missing = p.MemberSummary.CheckMissing(newPath, stats)
for _, e := range p.Packages {
missing = append(missing, e.CheckMissing(newPath, stats)...)
Expand Down Expand Up @@ -241,6 +244,12 @@ type Arg struct {
}

func (a *Arg) CheckMissing(path string, stats *missingStats) (missing []missingDocs) {
if a.Name == "self" && a.Type == "Self" {
return nil
}
if a.Convention == "out" {
return nil
}
if a.Description == "" {
missing = append(missing, missingDocs{fmt.Sprintf("%s.%s", path, a.Name), "description"})
stats.Missing++
Expand Down
2 changes: 2 additions & 0 deletions document/processor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package document

import (
"fmt"
"os"
"path"
"strings"
Expand Down Expand Up @@ -84,6 +85,7 @@ func (proc *Processor) ExtractTests(subdir string) error {
return err
}
if proc.Config.TestOutput != "" {
fmt.Printf("Extracted %d test(s) from package %s.\n", len(proc.docTests), proc.Docs.Decl.Name)
outPath := path.Join(proc.Config.TestOutput, subdir, proc.Docs.Decl.Name)
err = proc.writeDocTests(outPath)
if err != nil {
Expand Down
10 changes: 4 additions & 6 deletions document/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,11 @@ func renderWith(config *Config, proc *Processor, subdir string) error {
if err := proc.Formatter.WriteAuxiliary(proc.ExportDocs.Decl, outPath, proc); err != nil {
return err
}

if config.ReportMissing {
if err := reportMissing(missing, stats, config.Strict); err != nil {
if err := reportMissing(proc.Docs.Decl.Name, missing, stats, config.Strict); err != nil {
return err
}
}

return nil
}

Expand Down Expand Up @@ -201,15 +199,15 @@ func linkAndWrite(text string, dir []string, modElems int, kind string, proc *Pr
return proc.WriteFile(outFile, text)
}

func reportMissing(missing []missingDocs, stats missingStats, strict bool) error {
func reportMissing(pkg string, missing []missingDocs, stats missingStats, strict bool) error {
if len(missing) == 0 {
fmt.Println("Docstring coverage: 100%")
fmt.Printf("Docstring coverage of package %s: 100%%\n", pkg)
return nil
}
for _, m := range missing {
fmt.Printf("WARNING: missing %s in %s\n", m.What, m.Who)
}
fmt.Printf("Docstring coverage: %.1f%%\n", 100.0*float64(stats.Total-stats.Missing)/float64(stats.Total))
fmt.Printf("Docstring coverage package %s: %.1f%%\n", pkg, 100.0*float64(stats.Total-stats.Missing)/float64(stats.Total))
if strict {
return fmt.Errorf("missing docstrings in strict mode")
}
Expand Down

0 comments on commit f6d9203

Please sign in to comment.