...

Source file src/github.com/bazelbuild/buildtools/warn/warn_deprecated.go

Documentation: github.com/bazelbuild/buildtools/warn

     1  /*
     2  Copyright 2020 Google LLC
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      https://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Warnings for using deprecated functions
    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