| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package deprecated |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"go/ast" |
| 10 |
"go/format" |
| 11 |
"go/token" |
| 12 |
"go/types" |
| 13 |
"strconv" |
| 14 |
"strings" |
| 15 |
|
| 16 |
_ "embed" |
| 17 |
|
| 18 |
"golang.org/x/tools/go/analysis" |
| 19 |
"golang.org/x/tools/go/analysis/passes/inspect" |
| 20 |
"golang.org/x/tools/go/ast/inspector" |
| 21 |
"golang.org/x/tools/internal/analysis/analyzerutil" |
| 22 |
internalastutil "golang.org/x/tools/internal/astutil" |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
var doc string |
| 27 |
|
| 28 |
var Analyzer = &analysis.Analyzer{ |
| 29 |
Name: "deprecated", |
| 30 |
Doc: analyzerutil.MustExtractDoc(doc, "deprecated"), |
| 31 |
Requires: []*analysis.Analyzer{inspect.Analyzer}, |
| 32 |
Run: checkDeprecated, |
| 33 |
FactTypes: []analysis.Fact{(*deprecationFact)(nil)}, |
| 34 |
RunDespiteErrors: true, |
| 35 |
URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/deprecated", |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
func checkDeprecated(pass *analysis.Pass) (any, error) { |
| 40 |
inspector := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 41 |
|
| 42 |
deprs, err := collectDeprecatedNames(pass, inspector) |
| 43 |
if err != nil || (len(deprs.packages) == 0 && len(deprs.objects) == 0) { |
| 44 |
return nil, err |
| 45 |
} |
| 46 |
|
| 47 |
reportDeprecation := func(depr *deprecationFact, node ast.Node) { |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
buf := new(bytes.Buffer) |
| 53 |
if err := format.Node(buf, pass.Fset, node); err != nil { |
| 54 |
|
| 55 |
buf.Reset() |
| 56 |
buf.WriteString("declaration") |
| 57 |
} |
| 58 |
pass.ReportRangef(node, "%s is deprecated: %s", buf, depr.Msg) |
| 59 |
} |
| 60 |
|
| 61 |
nodeFilter := []ast.Node{(*ast.SelectorExpr)(nil)} |
| 62 |
inspector.Preorder(nodeFilter, func(node ast.Node) { |
| 63 |
|
| 64 |
sel, ok := node.(*ast.SelectorExpr) |
| 65 |
if !ok { |
| 66 |
return |
| 67 |
} |
| 68 |
|
| 69 |
obj := pass.TypesInfo.ObjectOf(sel.Sel) |
| 70 |
if fn, ok := obj.(*types.Func); ok { |
| 71 |
obj = fn.Origin() |
| 72 |
} |
| 73 |
if obj == nil || obj.Pkg() == nil { |
| 74 |
|
| 75 |
return |
| 76 |
} |
| 77 |
|
| 78 |
if obj.Pkg() == pass.Pkg { |
| 79 |
|
| 80 |
return |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
if strings.TrimSuffix(pass.Pkg.Path(), "_test") == obj.Pkg().Path() { |
| 88 |
|
| 89 |
return |
| 90 |
} |
| 91 |
if strings.TrimSuffix(pass.Pkg.Path(), ".test") == obj.Pkg().Path() { |
| 92 |
|
| 93 |
return |
| 94 |
} |
| 95 |
if strings.TrimSuffix(pass.Pkg.Path(), ".test") == strings.TrimSuffix(obj.Pkg().Path(), "_test") { |
| 96 |
|
| 97 |
return |
| 98 |
} |
| 99 |
|
| 100 |
if depr, ok := deprs.objects[obj]; ok { |
| 101 |
reportDeprecation(depr, sel) |
| 102 |
} |
| 103 |
}) |
| 104 |
|
| 105 |
for _, f := range pass.Files { |
| 106 |
for _, spec := range f.Imports { |
| 107 |
var imp *types.Package |
| 108 |
var obj types.Object |
| 109 |
if spec.Name != nil { |
| 110 |
obj = pass.TypesInfo.ObjectOf(spec.Name) |
| 111 |
} else { |
| 112 |
obj = pass.TypesInfo.Implicits[spec] |
| 113 |
} |
| 114 |
pkgName, ok := obj.(*types.PkgName) |
| 115 |
if !ok { |
| 116 |
continue |
| 117 |
} |
| 118 |
imp = pkgName.Imported() |
| 119 |
|
| 120 |
path, err := strconv.Unquote(spec.Path.Value) |
| 121 |
if err != nil { |
| 122 |
continue |
| 123 |
} |
| 124 |
pkgPath := pass.Pkg.Path() |
| 125 |
if strings.TrimSuffix(pkgPath, "_test") == path { |
| 126 |
|
| 127 |
continue |
| 128 |
} |
| 129 |
if strings.TrimSuffix(pkgPath, ".test") == path { |
| 130 |
|
| 131 |
continue |
| 132 |
} |
| 133 |
if strings.TrimSuffix(pkgPath, ".test") == strings.TrimSuffix(path, "_test") { |
| 134 |
|
| 135 |
continue |
| 136 |
} |
| 137 |
if depr, ok := deprs.packages[imp]; ok { |
| 138 |
reportDeprecation(depr, spec.Path) |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
return nil, nil |
| 143 |
} |
| 144 |
|
| 145 |
type deprecationFact struct{ Msg string } |
| 146 |
|
| 147 |
func (*deprecationFact) AFact() {} |
| 148 |
func (d *deprecationFact) String() string { return "Deprecated: " + d.Msg } |
| 149 |
|
| 150 |
type deprecatedNames struct { |
| 151 |
objects map[types.Object]*deprecationFact |
| 152 |
packages map[*types.Package]*deprecationFact |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
func collectDeprecatedNames(pass *analysis.Pass, ins *inspector.Inspector) (deprecatedNames, error) { |
| 159 |
doDocs := func(names []*ast.Ident, docs *ast.CommentGroup) { |
| 160 |
alt := strings.TrimPrefix(internalastutil.Deprecation(docs), "Deprecated: ") |
| 161 |
if alt == "" { |
| 162 |
return |
| 163 |
} |
| 164 |
|
| 165 |
for _, name := range names { |
| 166 |
obj := pass.TypesInfo.ObjectOf(name) |
| 167 |
pass.ExportObjectFact(obj, &deprecationFact{alt}) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
if pass.Pkg.Path() != "syscall" { |
| 178 |
for _, f := range pass.Files { |
| 179 |
if depr := internalastutil.Deprecation(f.Doc); depr != "" { |
| 180 |
pass.ExportPackageFact(&deprecationFact{depr}) |
| 181 |
break |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
nodeFilter := []ast.Node{ |
| 187 |
(*ast.GenDecl)(nil), |
| 188 |
(*ast.FuncDecl)(nil), |
| 189 |
(*ast.TypeSpec)(nil), |
| 190 |
(*ast.ValueSpec)(nil), |
| 191 |
(*ast.File)(nil), |
| 192 |
(*ast.StructType)(nil), |
| 193 |
(*ast.InterfaceType)(nil), |
| 194 |
} |
| 195 |
ins.Preorder(nodeFilter, func(node ast.Node) { |
| 196 |
var names []*ast.Ident |
| 197 |
var docs *ast.CommentGroup |
| 198 |
switch node := node.(type) { |
| 199 |
case *ast.GenDecl: |
| 200 |
switch node.Tok { |
| 201 |
case token.TYPE, token.CONST, token.VAR: |
| 202 |
docs = node.Doc |
| 203 |
for i := range node.Specs { |
| 204 |
switch n := node.Specs[i].(type) { |
| 205 |
case *ast.ValueSpec: |
| 206 |
names = append(names, n.Names...) |
| 207 |
case *ast.TypeSpec: |
| 208 |
names = append(names, n.Name) |
| 209 |
} |
| 210 |
} |
| 211 |
default: |
| 212 |
return |
| 213 |
} |
| 214 |
case *ast.FuncDecl: |
| 215 |
docs = node.Doc |
| 216 |
names = []*ast.Ident{node.Name} |
| 217 |
case *ast.TypeSpec: |
| 218 |
docs = node.Doc |
| 219 |
names = []*ast.Ident{node.Name} |
| 220 |
case *ast.ValueSpec: |
| 221 |
docs = node.Doc |
| 222 |
names = node.Names |
| 223 |
case *ast.StructType: |
| 224 |
for _, field := range node.Fields.List { |
| 225 |
doDocs(field.Names, field.Doc) |
| 226 |
} |
| 227 |
case *ast.InterfaceType: |
| 228 |
for _, field := range node.Methods.List { |
| 229 |
doDocs(field.Names, field.Doc) |
| 230 |
} |
| 231 |
} |
| 232 |
if docs != nil && len(names) > 0 { |
| 233 |
doDocs(names, docs) |
| 234 |
} |
| 235 |
}) |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
out := deprecatedNames{ |
| 241 |
objects: map[types.Object]*deprecationFact{}, |
| 242 |
packages: map[*types.Package]*deprecationFact{}, |
| 243 |
} |
| 244 |
for _, fact := range pass.AllObjectFacts() { |
| 245 |
out.objects[fact.Object] = fact.Fact.(*deprecationFact) |
| 246 |
} |
| 247 |
for _, fact := range pass.AllPackageFacts() { |
| 248 |
out.packages[fact.Package] = fact.Fact.(*deprecationFact) |
| 249 |
} |
| 250 |
|
| 251 |
return out, nil |
| 252 |
} |