Skip to content

Commit

Permalink
works
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolaydubina committed Jan 6, 2023
1 parent b1b6fc2 commit 5db0363
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -5,6 +5,23 @@
> As in newspaper articles, we expect the most important concepts to come first, and we expect them to be expressed with the least amount of polluting detail. We expect the low-level details to come last. This allows us to skim source files, getting the gist from the frist few functions, without having to immerge ourselves in the details.
> — Clean Code, Chapter 5, p84, Robert C. Martin, 2009
![](./doc/code-dep-viz.png)

```go
go install github.com/nikolaydubina/verfn@latest
```

```bash
verfn --verbose ./...
```

## False Positives

* same function names but different classes
* same functino names but from different packages
* functions passed as arguments
* functions passed as arguments and renamed to match existing functions

## Appendix A: Canonical Java Example

Clean Code, Chapter 5, code example `WikiPageResponder.java`
Expand Down
22 changes: 12 additions & 10 deletions analysis/vertfn/analyzer.go
Expand Up @@ -32,19 +32,19 @@ func init() {

func fnameFromFuncDecl(n *ast.FuncDecl) string { return n.Name.Name }

func fnameFromCallExpr(n *ast.CallExpr) string {
func fnameFromCallExpr(n *ast.CallExpr) []string {
if ind, ok := n.Fun.(*ast.Ident); ok && ind != nil {
return ind.Name
return []string{ind.Name}
}
if sel, ok := n.Fun.(*ast.SelectorExpr); ok && sel != nil {
// TODO: utilize type/package information
// TODO: differentiate same method name but on different classes

// TODO: chains of selectors
// TODO: chains of methods
fmt.Printf("%#v | %#v\n", sel.X, sel.Sel)
var fnames []string
fnames = append(fnames, sel.Sel.Name)
if call, ok := sel.X.(*ast.CallExpr); ok && call != nil {
fnames = append(fnames, fnameFromCallExpr(call)...)
}
return fnames
}
return ""
return nil
}

func run(pass *analysis.Pass) (interface{}, error) {
Expand Down Expand Up @@ -72,7 +72,9 @@ func run(pass *analysis.Pass) (interface{}, error) {
}

if call, ok := n.(*ast.CallExpr); ok && call != nil {
fnCall[fnameFromCallExpr(call)] = append(fnCall[fnameFromCallExpr(call)], call)
for _, fname := range fnameFromCallExpr(call) {
fnCall[fname] = append(fnCall[fname], call)
}
}
})

Expand Down
12 changes: 11 additions & 1 deletion analysis/vertfn/internal/example/simple.go
Expand Up @@ -12,20 +12,30 @@ func mixFlour() {
}

func MakePancake() {
readRecipe()

visitStore()

mixFlour()

FindOven().WarmUp().Bake()
FindOven().
WarmUp().
WarmUp().
Bake()

Enjoy("pancake")
}

func readRecipe() {
log.Println("reading recepie")
}

func buyFlour() {
log.Println("bought flour")
}

func visitStore() {
readRecipe()
buyFlour()
}

Expand Down
Binary file added doc/code-dep-viz.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5db0363

Please sign in to comment.