mirror of
https://github.com/Crocmagnon/fatcontext.git
synced 2024-11-22 07:58:05 +01:00
fix: remove panic
This commit is contained in:
parent
95e882dd7d
commit
a2187f17e7
1 changed files with 21 additions and 17 deletions
|
@ -3,6 +3,7 @@ package analyzer
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/printer"
|
"go/printer"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
@ -59,23 +60,26 @@ func run(pass *analysis.Pass) (interface{}, error) {
|
||||||
Tok: token.DEFINE,
|
Tok: token.DEFINE,
|
||||||
Rhs: assignStmt.Rhs,
|
Rhs: assignStmt.Rhs,
|
||||||
}
|
}
|
||||||
suggested := render(pass.Fset, &suggestedStmt)
|
suggested, err := render(pass.Fset, &suggestedStmt)
|
||||||
|
|
||||||
pass.Report(analysis.Diagnostic{
|
var fixes []analysis.SuggestedFix
|
||||||
Pos: assignStmt.Pos(),
|
if err == nil {
|
||||||
Message: "nested context in loop",
|
fixes = append(fixes, analysis.SuggestedFix{
|
||||||
SuggestedFixes: []analysis.SuggestedFix{
|
Message: "replace `=` with `:=`",
|
||||||
{
|
TextEdits: []analysis.TextEdit{
|
||||||
Message: "replace `=` with `:=`",
|
{
|
||||||
TextEdits: []analysis.TextEdit{
|
Pos: assignStmt.Pos(),
|
||||||
{
|
End: assignStmt.End(),
|
||||||
Pos: assignStmt.Pos(),
|
NewText: []byte(suggested),
|
||||||
End: assignStmt.End(),
|
|
||||||
NewText: []byte(suggested),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pass.Report(analysis.Diagnostic{
|
||||||
|
Pos: assignStmt.Pos(),
|
||||||
|
Message: "nested context in loop",
|
||||||
|
SuggestedFixes: fixes,
|
||||||
})
|
})
|
||||||
|
|
||||||
break
|
break
|
||||||
|
@ -100,10 +104,10 @@ func getBody(node ast.Node) (*ast.BlockStmt, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// render returns the pretty-print of the given node
|
// render returns the pretty-print of the given node
|
||||||
func render(fset *token.FileSet, x interface{}) string {
|
func render(fset *token.FileSet, x interface{}) (string, error) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
if err := printer.Fprint(&buf, fset, x); err != nil {
|
if err := printer.Fprint(&buf, fset, x); err != nil {
|
||||||
panic(err)
|
return "", fmt.Errorf("printing node: %w", err)
|
||||||
}
|
}
|
||||||
return buf.String()
|
return buf.String(), nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue