2020-03-05 07:51:57 -07:00
|
|
|
// Copyright 2020 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package undeclaredname defines an Analyzer that applies suggested fixes
|
|
|
|
// to errors of the type "undeclared name: %s".
|
|
|
|
package undeclaredname
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
2020-07-13 17:36:26 -06:00
|
|
|
"go/token"
|
|
|
|
"go/types"
|
2020-03-05 07:51:57 -07:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/tools/go/analysis"
|
|
|
|
"golang.org/x/tools/go/ast/astutil"
|
|
|
|
"golang.org/x/tools/internal/analysisinternal"
|
2020-07-23 21:24:36 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2020-03-05 07:51:57 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const Doc = `suggested fixes for "undeclared name: <>"
|
|
|
|
|
|
|
|
This checker provides suggested fixes for type errors of the
|
|
|
|
type "undeclared name: <>". It will insert a new statement:
|
|
|
|
"<> := ".`
|
|
|
|
|
|
|
|
var Analyzer = &analysis.Analyzer{
|
|
|
|
Name: string(analysisinternal.UndeclaredName),
|
|
|
|
Doc: Doc,
|
|
|
|
Requires: []*analysis.Analyzer{},
|
|
|
|
Run: run,
|
|
|
|
RunDespiteErrors: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
const undeclaredNamePrefix = "undeclared name: "
|
|
|
|
|
|
|
|
func run(pass *analysis.Pass) (interface{}, error) {
|
|
|
|
for _, err := range analysisinternal.GetTypeErrors(pass) {
|
2020-03-31 21:53:42 -06:00
|
|
|
if !FixesError(err.Msg) {
|
2020-03-05 07:51:57 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
name := strings.TrimPrefix(err.Msg, undeclaredNamePrefix)
|
|
|
|
var file *ast.File
|
|
|
|
for _, f := range pass.Files {
|
|
|
|
if f.Pos() <= err.Pos && err.Pos < f.End() {
|
|
|
|
file = f
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if file == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the path for the relevant range.
|
|
|
|
path, _ := astutil.PathEnclosingInterval(file, err.Pos, err.Pos)
|
|
|
|
if len(path) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ident, ok := path[0].(*ast.Ident)
|
|
|
|
if !ok || ident.Name != name {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Skip selector expressions because it might be too complex
|
|
|
|
// to try and provide a suggested fix for fields and methods.
|
|
|
|
if _, ok := path[1].(*ast.SelectorExpr); ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-13 17:36:26 -06:00
|
|
|
// TODO(golang.org/issue/34644): Handle call expressions with suggested
|
|
|
|
// fixes to create a function.
|
2020-03-05 07:51:57 -07:00
|
|
|
if _, ok := path[1].(*ast.CallExpr); ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-13 17:36:26 -06:00
|
|
|
tok := pass.Fset.File(file.Pos())
|
|
|
|
if tok == nil {
|
2020-03-05 07:51:57 -07:00
|
|
|
continue
|
|
|
|
}
|
2020-07-13 17:36:26 -06:00
|
|
|
offset := pass.Fset.Position(err.Pos).Offset
|
|
|
|
end := tok.Pos(offset + len(name))
|
2020-03-05 07:51:57 -07:00
|
|
|
pass.Report(analysis.Diagnostic{
|
|
|
|
Pos: err.Pos,
|
2020-07-13 17:36:26 -06:00
|
|
|
End: end,
|
2020-03-05 07:51:57 -07:00
|
|
|
Message: err.Msg,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-07-23 21:24:36 -06:00
|
|
|
func SuggestedFix(fset *token.FileSet, rng span.Range, content []byte, file *ast.File, _ *types.Package, _ *types.Info) (*analysis.SuggestedFix, error) {
|
|
|
|
pos := rng.Start // don't use the end
|
2020-07-13 17:36:26 -06:00
|
|
|
path, _ := astutil.PathEnclosingInterval(file, pos, pos)
|
|
|
|
if len(path) < 2 {
|
|
|
|
return nil, fmt.Errorf("")
|
|
|
|
}
|
|
|
|
ident, ok := path[0].(*ast.Ident)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("")
|
|
|
|
}
|
|
|
|
// Get the place to insert the new statement.
|
|
|
|
insertBeforeStmt := analysisinternal.StmtToInsertVarBefore(path)
|
|
|
|
if insertBeforeStmt == nil {
|
|
|
|
return nil, fmt.Errorf("")
|
|
|
|
}
|
|
|
|
|
|
|
|
insertBefore := fset.Position(insertBeforeStmt.Pos()).Offset
|
|
|
|
|
|
|
|
// Get the indent to add on the line after the new statement.
|
|
|
|
// Since this will have a parse error, we can not use format.Source().
|
|
|
|
contentBeforeStmt, indent := content[:insertBefore], "\n"
|
|
|
|
if nl := bytes.LastIndex(contentBeforeStmt, []byte("\n")); nl != -1 {
|
|
|
|
indent = string(contentBeforeStmt[nl:])
|
|
|
|
}
|
|
|
|
// Create the new local variable statement.
|
|
|
|
newStmt := fmt.Sprintf("%s := %s", ident.Name, indent)
|
|
|
|
return &analysis.SuggestedFix{
|
|
|
|
Message: fmt.Sprintf("Create variable \"%s\"", ident.Name),
|
|
|
|
TextEdits: []analysis.TextEdit{{
|
|
|
|
Pos: insertBeforeStmt.Pos(),
|
|
|
|
End: insertBeforeStmt.Pos(),
|
|
|
|
NewText: []byte(newStmt),
|
|
|
|
}},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-03-31 21:53:42 -06:00
|
|
|
func FixesError(msg string) bool {
|
|
|
|
return strings.HasPrefix(msg, undeclaredNamePrefix)
|
|
|
|
}
|