lint: fix several lll, wrapcheck, and copy-paste errors
This commit is contained in:
@@ -2,6 +2,7 @@ package modelgenerate
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/printer"
|
||||
@@ -65,14 +66,14 @@ func FprintWithComments(w io.Writer, file *ast.File) error {
|
||||
var buf bytes.Buffer
|
||||
fset := token.NewFileSet()
|
||||
if err := printer.Fprint(&buf, fset, file); err != nil {
|
||||
return err
|
||||
return fmt.Errorf("initial pretty-printing to get positioning: %w", err)
|
||||
}
|
||||
|
||||
// Re-parse to get real positions (ParseComments preserves doc comments)
|
||||
fset = token.NewFileSet()
|
||||
parsed, err := parser.ParseFile(fset, "", buf.Bytes(), parser.ParseComments)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("re-parsing pretty-print: %w", err)
|
||||
}
|
||||
|
||||
// Convert the tree-of-nodes into a slice-of-nodes
|
||||
@@ -98,8 +99,8 @@ func FprintWithComments(w io.Writer, file *ast.File) error {
|
||||
origNodes := collectNodes(file)
|
||||
reparsedNodes := collectNodes(parsed)
|
||||
for i, orig := range origNodes {
|
||||
text, ok := TrailingComments[orig]
|
||||
if !ok {
|
||||
text, isOk := TrailingComments[orig]
|
||||
if !isOk {
|
||||
continue
|
||||
}
|
||||
reparsed := reparsedNodes[i]
|
||||
@@ -110,40 +111,43 @@ func FprintWithComments(w io.Writer, file *ast.File) error {
|
||||
}
|
||||
|
||||
extractCommentMarker := func(stmt ast.Stmt) (string, bool) {
|
||||
expr, ok := stmt.(*ast.ExprStmt)
|
||||
if !ok {
|
||||
expr, isOk := stmt.(*ast.ExprStmt)
|
||||
if !isOk {
|
||||
return "", false
|
||||
}
|
||||
call, ok := expr.X.(*ast.CallExpr)
|
||||
if !ok {
|
||||
call, isOk := expr.X.(*ast.CallExpr)
|
||||
if !isOk {
|
||||
return "", false
|
||||
}
|
||||
ident, ok := call.Fun.(*ast.Ident)
|
||||
if !ok || ident.Name != commentMarker {
|
||||
ident, isOk := call.Fun.(*ast.Ident)
|
||||
if !isOk || ident.Name != commentMarker {
|
||||
return "", false
|
||||
}
|
||||
lit, isOk := call.Args[0].(*ast.BasicLit)
|
||||
if !isOk {
|
||||
return "", false
|
||||
}
|
||||
lit := call.Args[0].(*ast.BasicLit)
|
||||
return lit.Value[1 : len(lit.Value)-1], true
|
||||
}
|
||||
|
||||
isBlankLineMarker := func(stmt ast.Stmt) bool {
|
||||
expr, ok := stmt.(*ast.ExprStmt)
|
||||
if !ok {
|
||||
expr, isOk := stmt.(*ast.ExprStmt)
|
||||
if !isOk {
|
||||
return false
|
||||
}
|
||||
call, ok := expr.X.(*ast.CallExpr)
|
||||
if !ok {
|
||||
call, isOk := expr.X.(*ast.CallExpr)
|
||||
if !isOk {
|
||||
return false
|
||||
}
|
||||
ident, ok := call.Fun.(*ast.Ident)
|
||||
return ok && ident.Name == blankLineMarker
|
||||
ident, isOk := call.Fun.(*ast.Ident)
|
||||
return isOk && ident.Name == blankLineMarker
|
||||
}
|
||||
|
||||
// Convert comment and blank-line markers
|
||||
ast.Inspect(parsed, func(n ast.Node) bool {
|
||||
// We only care about Block nodes
|
||||
block, ok := n.(*ast.BlockStmt)
|
||||
if !ok {
|
||||
block, isOk := n.(*ast.BlockStmt)
|
||||
if !isOk {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -151,7 +155,7 @@ func FprintWithComments(w io.Writer, file *ast.File) error {
|
||||
// in the block statement's body with actual ones
|
||||
filtered := block.List[:0]
|
||||
for _, stmt := range block.List {
|
||||
if text, ok := extractCommentMarker(stmt); ok {
|
||||
if text, isOk := extractCommentMarker(stmt); isOk {
|
||||
// If it's a comment, add it to the fileset's list of Comments
|
||||
parsed.Comments = append(parsed.Comments, &ast.CommentGroup{
|
||||
List: []*ast.Comment{{Slash: stmt.Pos(), Text: "// " + text}},
|
||||
@@ -176,5 +180,9 @@ func FprintWithComments(w io.Writer, file *ast.File) error {
|
||||
delete(TrailingComments, k)
|
||||
}
|
||||
|
||||
return printer.Fprint(w, fset, parsed)
|
||||
err = printer.Fprint(w, fset, parsed)
|
||||
if err != nil {
|
||||
return fmt.Errorf("re-pretty-printing: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user