Skip to content

Commit

Permalink
Godoc improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Tit Petric committed Dec 16, 2024
1 parent f4a589d commit 6edb7e2
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 10 deletions.
2 changes: 2 additions & 0 deletions cmd/go-fsck/example/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Example package doc.
package example
6 changes: 4 additions & 2 deletions cmd/go-fsck/example/go-fsck.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"ImportPath": "github.com/TykTechnologies/exp/cmd/go-fsck/example",
"Path": ".",
"TestPackage": false,
"Doc": null,
"Doc": "Example package doc.",
"Imports": {
"example.go": [
"\"net/http\""
Expand Down Expand Up @@ -70,6 +70,7 @@
"Kind": "func",
"File": "example.go",
"SelfContained": false,
"Doc": "Global func comment",
"Name": "GlobalFunc",
"Returns": [
"error"
Expand Down Expand Up @@ -140,7 +141,7 @@
"ImportPath": "github.com/TykTechnologies/exp/cmd/go-fsck/example_test",
"Path": ".",
"TestPackage": false,
"Doc": null,
"Doc": "Example package doc.",
"Imports": {
"example.go": [
"\"net/http\""
Expand Down Expand Up @@ -206,6 +207,7 @@
"Kind": "func",
"File": "example.go",
"SelfContained": false,
"Doc": "Global func comment",
"Name": "GlobalFunc",
"Returns": [
"error"
Expand Down
6 changes: 6 additions & 0 deletions cmd/go-fsck/extract/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ func getDefinitions(cfg *options) ([]*model.Definition, error) {
}
}

if !cfg.includeTests {
for _, def := range defs {
def.ClearTestFiles()
}
}

return defs, nil
}

Expand Down
8 changes: 7 additions & 1 deletion cmd/go-fsck/internal/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (v *collector) Visit(node ast.Node, push bool, stack []ast.Node) bool {
}

if file.Doc != nil {
pkg.Doc.Add(filename, v.getSource(file, file.Doc.List))
pkg.Doc = strings.TrimSpace(v.getSource(file, file.Doc))
}

switch node := node.(type) {
Expand Down Expand Up @@ -285,6 +285,7 @@ func (v *collector) collectFuncDeclaration(file *ast.File, decl *ast.FuncDecl, f
args, returns := v.functionBindings(decl)

declaration := &Declaration{
Doc: strings.TrimSpace(v.getSource(file, decl.Doc)),
Kind: model.FuncKind,
File: filepath.Base(filename),
Name: decl.Name.Name,
Expand All @@ -303,9 +304,14 @@ func (v *collector) collectFuncDeclaration(file *ast.File, decl *ast.FuncDecl, f
}

func (p *collector) getSource(file *ast.File, node any) string {
if commentGroup, ok := node.(*ast.CommentGroup); ok {
return commentGroup.Text()
}

var buf strings.Builder
err := PrintSource(&buf, p.fset, CommentedNode(file, node))
if err != nil {
fmt.Printf("Error printing source: %v\n", err)
return ""
}
return buf.String()
Expand Down
2 changes: 2 additions & 0 deletions cmd/go-fsck/model/declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type Declaration struct {

References map[string][]string `json:",omitempty"`

Doc string `json:",omitempty"`

Name string `json:",omitempty"`
Names []string `json:",omitempty"`
Receiver string `json:",omitempty"`
Expand Down
12 changes: 12 additions & 0 deletions cmd/go-fsck/model/declaration_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package model
import (
"go/ast"
"sort"
"strings"
)

type DeclarationList []*Declaration
Expand Down Expand Up @@ -43,6 +44,17 @@ func (p *DeclarationList) ClearSource() {
}
}

func (p *DeclarationList) ClearTestFiles() {
result := DeclarationList{}
for _, decl := range *p {
if strings.HasSuffix(decl.File, "_test.go") {
continue
}
result.Append(decl)
}
*p = result
}

func (p *DeclarationList) Sort() {
sort.Slice(*p, func(i, j int) bool {
a, b := (*p)[i], (*p)[j]
Expand Down
17 changes: 16 additions & 1 deletion cmd/go-fsck/model/definition.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package model

import "strings"

type Definition struct {
Package

Doc StringSet
Doc string

Imports StringSet
Types DeclarationList
Consts DeclarationList
Expand Down Expand Up @@ -37,6 +40,18 @@ func (d *Definition) ClearSource() {
d.Funcs.ClearSource()
}

func (d *Definition) ClearTestFiles() {
for filename, _ := range d.Imports {
if strings.HasSuffix(filename, "_test.go") {
delete(d.Imports, filename)
}
}
d.Types.ClearTestFiles()
d.Vars.ClearTestFiles()
d.Consts.ClearTestFiles()
d.Funcs.ClearTestFiles()
}

func (d *Definition) Fill() {
for _, decl := range d.Order() {
decl.Imports = d.getImports(decl)
Expand Down
2 changes: 2 additions & 0 deletions cmd/go-fsck/model/restored/declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type Declaration struct {

References map[string][]string `json:",omitempty"`

Doc string `json:",omitempty"`

Name string `json:",omitempty"`
Names []string `json:",omitempty"`
Receiver string `json:",omitempty"`
Expand Down
12 changes: 12 additions & 0 deletions cmd/go-fsck/model/restored/declaration_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package model
import (
"go/ast"
"sort"
"strings"
)

type DeclarationList []*Declaration
Expand Down Expand Up @@ -34,6 +35,17 @@ func (p *DeclarationList) ClearSource() {
}
}

func (p *DeclarationList) ClearTestFiles() {
result := DeclarationList{}
for _, decl := range *p {
if strings.HasSuffix(decl.File, "_test.go") {
continue
}
result.Append(decl)
}
*p = result
}

func (p *DeclarationList) Sort() {
sort.Slice(*p, func(i, j int) bool {
a, b := (*p)[i], (*p)[j]
Expand Down
3 changes: 2 additions & 1 deletion cmd/go-fsck/model/restored/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package model
type Definition struct {
Package

Doc StringSet
Doc string

Imports StringSet
Types DeclarationList
Consts DeclarationList
Expand Down
29 changes: 24 additions & 5 deletions cmd/go-fsck/model/restored/go-fsck.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
"ImportPath": "github.com/TykTechnologies/exp/cmd/go-fsck/model",
"Path": ".",
"TestPackage": false,
"Doc": null,
"Doc": "",
"Imports": {
"declaration.go": [
"\"strings\""
],
"declaration_list.go": [
"\"go/ast\"",
"\"sort\""
"\"sort\"",
"\"strings\""
],
"definition.go": [
"\"strings\""
],
"package.go": [
"\"fmt\""
Expand All @@ -30,7 +34,7 @@
"File": "declaration.go",
"SelfContained": true,
"Name": "Declaration",
"Source": "type Declaration struct {\n\tKind\tDeclarationKind\n\tFile\tstring\n\n\tSelfContained\tbool\n\n\tImports\t[]string\t`json:\",omitempty\"`\n\n\tReferences\tmap[string][]string\t`json:\",omitempty\"`\n\n\tName\t\tstring\t\t`json:\",omitempty\"`\n\tNames\t\t[]string\t`json:\",omitempty\"`\n\tReceiver\tstring\t\t`json:\",omitempty\"`\n\n\tArguments\t[]string\t`json:\",omitempty\"`\n\tReturns\t\t[]string\t`json:\",omitempty\"`\n\n\tSignature\tstring\t`json:\",omitempty\"`\n\tSource\t\tstring\t`json:\",omitempty\"`\n}"
"Source": "type Declaration struct {\n\tKind\tDeclarationKind\n\tFile\tstring\n\n\tSelfContained\tbool\n\n\tImports\t[]string\t`json:\",omitempty\"`\n\n\tReferences\tmap[string][]string\t`json:\",omitempty\"`\n\n\tDoc\tstring\t`json:\",omitempty\"`\n\n\tName\t\tstring\t\t`json:\",omitempty\"`\n\tNames\t\t[]string\t`json:\",omitempty\"`\n\tReceiver\tstring\t\t`json:\",omitempty\"`\n\n\tArguments\t[]string\t`json:\",omitempty\"`\n\tReturns\t\t[]string\t`json:\",omitempty\"`\n\n\tSignature\tstring\t`json:\",omitempty\"`\n\tSource\t\tstring\t`json:\",omitempty\"`\n}"
},
{
"Kind": "type",
Expand All @@ -51,7 +55,7 @@
"File": "definition.go",
"SelfContained": true,
"Name": "Definition",
"Source": "type Definition struct {\n\tPackage\n\n\tDoc\tStringSet\n\tImports\tStringSet\n\tTypes\tDeclarationList\n\tConsts\tDeclarationList\n\tVars\tDeclarationList\n\tFuncs\tDeclarationList\n}"
"Source": "type Definition struct {\n\tPackage\n\n\tDoc\tstring\n\n\tImports\tStringSet\n\tTypes\tDeclarationList\n\tConsts\tDeclarationList\n\tVars\tDeclarationList\n\tFuncs\tDeclarationList\n}"
},
{
"Kind": "type",
Expand Down Expand Up @@ -85,7 +89,7 @@
"Source": "const (\n\tStructKind\tDeclarationKind\t= \"struct\"\n\tImportKind\t\t\t= \"import\"\n\tConstKind\t\t\t= \"const\"\n\tTypeKind\t\t\t= \"type\"\n\tFuncKind\t\t\t= \"func\"\n\tVarKind\t\t\t\t= \"var\"\n\tCommentKind\t\t\t= \"comment\"\n)"
}
],
"Vars": null,
"Vars": [],
"Funcs": [
{
"Kind": "func",
Expand Down Expand Up @@ -152,6 +156,20 @@
"Signature": "ClearSource ()",
"Source": "func (p *DeclarationList) ClearSource() {\n\tfor _, decl := range *p {\n\t\tdecl.Source = \"\"\n\t}\n}"
},
{
"Kind": "func",
"File": "declaration_list.go",
"SelfContained": false,
"References": {
"strings": [
"HasSuffix"
]
},
"Name": "ClearTestFiles",
"Receiver": "*DeclarationList",
"Signature": "ClearTestFiles ()",
"Source": "func (p *DeclarationList) ClearTestFiles() {\n\tresult := DeclarationList{}\n\tfor _, decl := range *p {\n\t\tif strings.HasSuffix(decl.File, \"_test.go\") {\n\t\t\tcontinue\n\t\t}\n\t\tresult.Append(decl)\n\t}\n\t*p = result\n}"
},
{
"Kind": "func",
"File": "declaration_list.go",
Expand Down Expand Up @@ -311,6 +329,7 @@
"HasSuffix"
]
},
"Doc": "Map returns a map with the short package name as the key\nand the full import path as the value.",
"Name": "Map",
"Receiver": "StringSet",
"Returns": [
Expand Down

0 comments on commit 6edb7e2

Please sign in to comment.