Skip to content

Commit

Permalink
feat(fixer.go): enhance lookup function to support methods in additio…
Browse files Browse the repository at this point in the history
…n to functions

feat(fixer_test.go): add unit tests for the enhanced lookup functionality
feat(testdata/stringer.go): add comments and new methods to support testing of new lookup functionality
refactor(fixer.go): use strings package for splitting function names, improving code readability and functionality
  • Loading branch information
yyoshiki41 committed May 5, 2024
1 parent 9ea8391 commit dc79d3f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
37 changes: 32 additions & 5 deletions fixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"go/parser"
"go/token"
"os"
"strings"
)

func fix(filename, functionName string) error {
Expand All @@ -29,14 +30,40 @@ func lookup(filename, functionName string) (int, int, error) {
if err != nil {
return 0, 0, err
}
funcParts := strings.Split(functionName, ".")

var start, end int
ast.Inspect(node, func(n ast.Node) bool {
funcDecl, ok := n.(*ast.FuncDecl)
if ok && funcDecl.Name.Name == functionName {
start = fset.Position(funcDecl.Pos()).Line
end = fset.Position(funcDecl.End()).Line
return false
switch t := n.(type) {
case *ast.FuncDecl:
if len(funcParts) == 2 {
// handle method
if t.Name.Name == funcParts[1] {
if t.Recv != nil {
for _, field := range t.Recv.List {
var ident *ast.Ident
switch field.Type.(type) {
case *ast.Ident:
ident = field.Type.(*ast.Ident)
case *ast.StarExpr:
ident = field.Type.(*ast.StarExpr).X.(*ast.Ident)
}
if ident != nil && ident.Name == funcParts[0] {
start = fset.Position(t.Pos()).Line
end = fset.Position(t.End()).Line
return false
}
}
}
}
} else {
// handle function
if t.Name.Name == functionName {
start = fset.Position(t.Pos()).Line
end = fset.Position(t.End()).Line
return false
}
}
}
return true
})
Expand Down
30 changes: 30 additions & 0 deletions fixer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"testing"
)

func TestLookup(t *testing.T) {
t.Parallel()

tests := []struct {
filename, functionName string
start, end int
err error
}{
{"testdata/reachable.go", "Reachable", 5, 7, nil},
{"testdata/stringer.go", "myString.String", 12, 14, nil},
{"testdata/stringer.go", "myString.Unreachable", 20, 22, nil},
}

for _, tt := range tests {
t.Run(tt.filename+":"+tt.functionName, func(t *testing.T) {
t.Parallel()
start, end, err := lookup(tt.filename, tt.functionName)
if start != tt.start || end != tt.end || err != tt.err {
t.Errorf("lookup(%q, %q) = (%d, %d, %v); want (%d, %d, %v)",
tt.filename, tt.functionName, start, end, err, tt.start, tt.end, tt.err)
}
})
}
}
5 changes: 5 additions & 0 deletions testdata/stringer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ type myString struct {
Value string
}

// NOTE: This function is unreachable but it is neccessary to implement the fmt.Stringer interface
func (s myString) String() string {
return s.Value
}

func (s myString) Reachable() {
return
}

func (s *myString) Unreachable() {
return
}

0 comments on commit dc79d3f

Please sign in to comment.