Skip to content

Commit

Permalink
feat(fixer.go): integrate golang.org/x/tools/imports for better impor…
Browse files Browse the repository at this point in the history
…t management and formatting

test(fixer_test.go): update test cases to use .txt files for better clarity

build(go.mod): update dependencies and organize required modules

fix(testdata/golden.json): update function positions and add new function details

test(testdata/lookup.txt): add new test data file for lookup functionality

feat(testdata/main.go): add UnusedImportStatementRemoval function to test unused import removal

fix(testdata/main.go.golden): update golden file to reflect new import structure
  • Loading branch information
yyoshiki41 committed Jun 1, 2024
1 parent 856f557 commit 829da3b
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 14 deletions.
11 changes: 9 additions & 2 deletions fixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"os"
"path/filepath"
"strings"

"golang.org/x/tools/imports"
)

var (
Expand Down Expand Up @@ -106,8 +108,13 @@ func replaceLines(filename string, start, end int) error {
return err
}

// run gofmt
b, err := format.Source(buf.Bytes())
// run pkg.go.dev/golang.org/x/tools/imports
b, err := imports.Process(filename, buf.Bytes(), nil)
if err != nil {
return err
}
// format source code
b, err = format.Source(b)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions fixer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ func TestLookup(t *testing.T) {
start, end int
err error
}{
{"testdata/lookup.go", "Reachable", 7, 9, nil},
{"testdata/lookup.go", "myString.String", 15, 17, nil},
{"testdata/lookup.go", "myString.Unreachable", 19, 21, nil},
{"testdata/lookup.go", "NotFound", 0, 0, ErrFuncNotFound},
{"testdata/lookup.txt", "Reachable", 7, 9, nil},
{"testdata/lookup.txt", "myString.String", 15, 17, nil},
{"testdata/lookup.txt", "myString.Unreachable", 19, 21, nil},
{"testdata/lookup.txt", "NotFound", 0, 0, ErrFuncNotFound},
}

for _, tt := range tests {
Expand Down
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ module github.com/yyoshiki41/rdeadcode

go 1.22.2

require rsc.io/script v0.0.2
require (
golang.org/x/tools v0.14.0
rsc.io/script v0.0.2
)

require golang.org/x/tools v0.14.0 // indirect
require (
golang.org/x/mod v0.13.0 // indirect
golang.org/x/sys v0.13.0 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc=
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg=
rsc.io/script v0.0.2 h1:eYoG7A3GFC3z1pRx3A2+s/vZ9LA8cxojHyCvslnj4RI=
Expand Down
17 changes: 13 additions & 4 deletions testdata/golden.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
[
{
"Name": "main",
"Path": "github.com/yyoshiki41/rm-deadcode/testdata",
"Path": "github.com/yyoshiki41/rdeadcode",
"Funcs": [
{
"Name": "Unreachable",
"Position": {
"File": "testdata/main.go",
"Line": 18,
"Line": 21,
"Col": 6
},
"Generated": false
},
{
"Name": "UnusedImportStatementRemoval",
"Position": {
"File": "testdata/main.go",
"Line": 29,
"Col": 6
},
"Generated": false
Expand All @@ -16,7 +25,7 @@
"Name": "myString.String",
"Position": {
"File": "testdata/main.go",
"Line": 33,
"Line": 40,
"Col": 19
},
"Generated": false
Expand All @@ -25,7 +34,7 @@
"Name": "myString.Unreachable",
"Position": {
"File": "testdata/main.go",
"Line": 41,
"Line": 48,
"Col": 20
},
"Generated": false
Expand Down
File renamed without changes.
9 changes: 8 additions & 1 deletion testdata/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import "fmt"
import (
"context"
"fmt"
)

func main() {
Reachable()
Expand All @@ -23,6 +26,10 @@ func ReachableByTest() {
fmt.Println("reachableByTest")
}

func UnusedImportStatementRemoval() context.Context {
return context.TODO() // Test unused import statement removal
}

var _ fmt.Stringer = myString{}

type myString struct {
Expand Down
4 changes: 3 additions & 1 deletion testdata/main.go.golden
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import "fmt"
import (
"fmt"
)

func main() {
Reachable()
Expand Down

0 comments on commit 829da3b

Please sign in to comment.