Skip to content

Commit

Permalink
implement autofix to delete line
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrywiranto committed Nov 23, 2023
1 parent 555d4aa commit 3db9239
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
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
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 3db9239

Please sign in to comment.