Skip to content

Commit

Permalink
refactor(fixer.go): streamline function lookup logic for clarity and …
Browse files Browse the repository at this point in the history
…efficiency

feat(fixer_test.go): update tests to reflect new file structure and paths
feat(testdata/main.go): consolidate test data into a single main.go for simplicity
feat(testdata/output.json): add output.json to track function metadata
chore: remove old testdata files (reachable.go, stringer.go) to clean up project structure
  • Loading branch information
yyoshiki41 committed May 5, 2024
1 parent dc79d3f commit 6135a87
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 58 deletions.
33 changes: 15 additions & 18 deletions fixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,26 @@ func lookup(filename, functionName string) (int, int, error) {
ast.Inspect(node, func(n ast.Node) bool {
switch t := n.(type) {
case *ast.FuncDecl:
if len(funcParts) == 2 {
if len(funcParts) == 2 && t.Recv != nil {
// 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
}
}
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] &&
t.Name.Name == funcParts[1] {
start = fset.Position(t.Pos()).Line
end = fset.Position(t.End()).Line
return false
}
}
} else {
// handle function
if t.Name.Name == functionName {
if t.Name.Name == functionName && t.Recv == nil {
start = fset.Position(t.Pos()).Line
end = fset.Position(t.End()).Line
return false
Expand Down
6 changes: 3 additions & 3 deletions fixer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ func TestLookup(t *testing.T) {
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},
{"testdata/main.go", "Reachable", 14, 16, nil},
{"testdata/main.go", "myString.String", 33, 35, nil},
{"testdata/main.go", "myString.Unreachable", 41, 43, nil},
}

for _, tt := range tests {
Expand Down
33 changes: 33 additions & 0 deletions testdata/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

import "fmt"

func main() {
Reachable()
}
Expand All @@ -8,3 +10,34 @@ func init() {
s := myString{Value: "hello"}
s.Reachable()
}

func Reachable() {
fmt.Println("reachable")
}

func Unreachable() {
fmt.Println("unreachable")
}

func ReachableByTest() {
fmt.Println("reachableByTest")
}

var _ fmt.Stringer = myString{}

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
}
35 changes: 35 additions & 0 deletions testdata/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
{
"Name": "main",
"Path": "github.com/yyoshiki41/rm-deadcode/testdata",
"Funcs": [
{
"Name": "Unreachable",
"Position": {
"File": "main.go",
"Line": 18,
"Col": 6
},
"Generated": false
},
{
"Name": "myString.String",
"Position": {
"File": "main.go",
"Line": 33,
"Col": 19
},
"Generated": false
},
{
"Name": "myString.Unreachable",
"Position": {
"File": "main.go",
"Line": 41,
"Col": 20
},
"Generated": false
}
]
}
]
15 changes: 0 additions & 15 deletions testdata/reachable.go

This file was deleted.

22 changes: 0 additions & 22 deletions testdata/stringer.go

This file was deleted.

0 comments on commit 6135a87

Please sign in to comment.