Skip to content

Commit

Permalink
feat: implement autofix to delete entire line of .Finish() (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrywiranto committed Nov 24, 2023
1 parent 555d4aa commit 0e7b5cf
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

test:
@go test -race -v ./...

build:
@go build -o gomocklinter main.go
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,30 @@ $ go build -o gomocklinter main.go
$ go vet -vettool=./gomocklinter ./...
```

### Autofix

Autofix is supported that will delete the entire line with `.Finish()` call to `gomock.Controller`

```
$ ./gomocklinter -fix ./...
```

Please note that, there may be some adjustments need to be done by hand after this action

```
// Before
func TestFoo(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish() // will be deleted entirely
}
// After
func TestFoo(t *testing.T) {
ctrl := gomock.NewController(t)
}
```


## Motivation
As highlighted in https://pkg.go.dev/github.com/golang/mock/gomock#NewController

Expand All @@ -32,12 +56,12 @@ As highlighted in https://pkg.go.dev/github.com/golang/mock/gomock#NewController
```
// Bad
func TestFoo(t *testing.T) {
mock := gomock.NewController(t)
defer mock.Finish() // no need to call this since go1.14
ctrl := gomock.NewController(t)
defer ctrl.Finish() // no need to call this since go1.14
}
// Good
func TestFoo(t *testing.T) {
mock := gomock.NewController(t)
ctrl := gomock.NewController(t)
}
```
35 changes: 34 additions & 1 deletion pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,40 @@ func run(pass *analysis.Pass) (interface{}, error) {

// check for unnecessary call to gomock.Controller.Finish()
if isValidType(pass.TypesInfo.TypeOf(selIdent).String()) && selectorExpr.Sel.Name == finish {
pass.Reportf(callExpr.Pos(), reportMsg)
// Get the token.File associated with the callExpr
f := pass.Fset.File(callExpr.Pos())

// Get the line number of the callExpr
line := f.Line(callExpr.Pos())

// Get the start position of the line
lineStartPos := f.LineStart(line)

// Get the start position of the next line
nextLineStartPos := f.LineStart(line + 1)

// Calculate the end position of the line
lineEndPos := nextLineStartPos - 1

// Create a fix to delete the line
fix := analysis.SuggestedFix{
Message: "Remove unnecessary call to Finish",
TextEdits: []analysis.TextEdit{
{
Pos: lineStartPos,
End: lineEndPos,
NewText: []byte(""),
},
},
}

// Add the fix to the diagnostic report
diagnostic := analysis.Diagnostic{
Pos: callExpr.Pos(),
Message: reportMsg,
SuggestedFixes: []analysis.SuggestedFix{fix},
}
pass.Report(diagnostic)
}
})

Expand Down

0 comments on commit 0e7b5cf

Please sign in to comment.