gopkg.in/alecthomas/gometalinter.v3@v3.0.0/_linters/src/github.com/securego/gosec/rule.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  
    13  package gosec
    14  
    15  import (
    16  	"go/ast"
    17  	"reflect"
    18  )
    19  
    20  // The Rule interface used by all rules supported by gosec.
    21  type Rule interface {
    22  	ID() string
    23  	Match(ast.Node, *Context) (*Issue, error)
    24  }
    25  
    26  // RuleBuilder is used to register a rule definition with the analyzer
    27  type RuleBuilder func(id string, c Config) (Rule, []ast.Node)
    28  
    29  // A RuleSet maps lists of rules to the type of AST node they should be run on.
    30  // The anaylzer will only invoke rules contained in the list associated with the
    31  // type of AST node it is currently visiting.
    32  type RuleSet map[reflect.Type][]Rule
    33  
    34  // NewRuleSet constructs a new RuleSet
    35  func NewRuleSet() RuleSet {
    36  	return make(RuleSet)
    37  }
    38  
    39  // Register adds a trigger for the supplied rule for the the
    40  // specified ast nodes.
    41  func (r RuleSet) Register(rule Rule, nodes ...ast.Node) {
    42  	for _, n := range nodes {
    43  		t := reflect.TypeOf(n)
    44  		if rules, ok := r[t]; ok {
    45  			r[t] = append(rules, rule)
    46  		} else {
    47  			r[t] = []Rule{rule}
    48  		}
    49  	}
    50  }
    51  
    52  // RegisteredFor will return all rules that are registered for a
    53  // specified ast node.
    54  func (r RuleSet) RegisteredFor(n ast.Node) []Rule {
    55  	if rules, found := r[reflect.TypeOf(n)]; found {
    56  		return rules
    57  	}
    58  	return []Rule{}
    59  }