...
1
16
17
18
19 package warn
20
21 import (
22 "fmt"
23
24 "github.com/bazelbuild/buildtools/build"
25 "github.com/bazelbuild/buildtools/labels"
26 )
27
28 func checkDeprecatedFunction(stmt build.Expr, loadedSymbols *map[string]*build.Ident, fullLabel string) *LinterFinding {
29 def, ok := stmt.(*build.DefStmt)
30 if !ok {
31 return nil
32 }
33 node, ok := (*loadedSymbols)[def.Name]
34 if !ok {
35 return nil
36 }
37 docstring, ok := getDocstring(def.Body)
38 if !ok {
39 return nil
40 }
41 str, ok := (*docstring).(*build.StringExpr)
42 if !ok {
43 return nil
44 }
45 docstringInfo := parseFunctionDocstring(str)
46 if !docstringInfo.deprecated {
47 return nil
48 }
49
50 return makeLinterFinding(node, fmt.Sprintf("The function %q defined in %q is deprecated.", def.Name, fullLabel))
51 }
52
53 func deprecatedFunctionWarning(f *build.File, fileReader *FileReader) []*LinterFinding {
54 if fileReader == nil {
55 return nil
56 }
57
58 findings := []*LinterFinding{}
59 for _, stmt := range f.Stmt {
60 load, ok := stmt.(*build.LoadStmt)
61 if !ok {
62 continue
63 }
64 label := labels.ParseRelative(load.Module.Value, f.Pkg)
65 if label.Repository != "" || label.Target == "" {
66 continue
67 }
68 loadedFile := fileReader.GetFile(label.Package, label.Target)
69 if loadedFile == nil {
70 continue
71 }
72 loadedSymbols := make(map[string]*build.Ident)
73 for _, from := range load.From {
74 loadedSymbols[from.Name] = from
75 }
76
77 for _, stmt := range loadedFile.Stmt {
78 if finding := checkDeprecatedFunction(stmt, &loadedSymbols, loadedFile.CanonicalPath()); finding != nil {
79 findings = append(findings, finding)
80 }
81 }
82
83 }
84 return findings
85 }
86
View as plain text