github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/linter/disablerule/command.go (about)

     1  package disablerule
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strings"
     7  )
     8  
     9  type commandType int
    10  
    11  const (
    12  	commandDisable commandType = iota
    13  	commandEnable
    14  	commandDisableNext
    15  	commandDisableThis
    16  )
    17  
    18  // comment prefix
    19  const (
    20  	PrefixDisable     = `protolint:disable`
    21  	PrefixEnable      = `protolint:enable`
    22  	PrefixDisableNext = `protolint:disable:next`
    23  	PrefixDisableThis = `protolint:disable:this`
    24  )
    25  
    26  // comment prefix regexp
    27  var (
    28  	ReDisable     = regexp.MustCompile(PrefixDisable + ` (.*)`)
    29  	ReEnable      = regexp.MustCompile(PrefixEnable + ` (.*)`)
    30  	ReDisableNext = regexp.MustCompile(PrefixDisableNext + ` (.*)`)
    31  	ReDisableThis = regexp.MustCompile(PrefixDisableThis + ` (.*)`)
    32  )
    33  
    34  type command struct {
    35  	ruleIDs []string
    36  	t       commandType
    37  }
    38  
    39  func newCommand(
    40  	comment string,
    41  ) (command, error) {
    42  	subs := ReDisable.FindStringSubmatch(comment)
    43  	if len(subs) == 2 {
    44  		ruleIDs := strings.Fields(strings.TrimSpace(subs[1]))
    45  		return command{
    46  			ruleIDs: ruleIDs,
    47  			t:       commandDisable,
    48  		}, nil
    49  	}
    50  
    51  	subs = ReEnable.FindStringSubmatch(comment)
    52  	if len(subs) == 2 {
    53  		ruleIDs := strings.Fields(strings.TrimSpace(subs[1]))
    54  		return command{
    55  			ruleIDs: ruleIDs,
    56  			t:       commandEnable,
    57  		}, nil
    58  	}
    59  
    60  	subs = ReDisableNext.FindStringSubmatch(comment)
    61  	if len(subs) == 2 {
    62  		ruleIDs := strings.Fields(strings.TrimSpace(subs[1]))
    63  		return command{
    64  			ruleIDs: ruleIDs,
    65  			t:       commandDisableNext,
    66  		}, nil
    67  	}
    68  
    69  	subs = ReDisableThis.FindStringSubmatch(comment)
    70  	if len(subs) == 2 {
    71  		ruleIDs := strings.Fields(strings.TrimSpace(subs[1]))
    72  		return command{
    73  			ruleIDs: ruleIDs,
    74  			t:       commandDisableThis,
    75  		}, nil
    76  	}
    77  
    78  	return command{}, fmt.Errorf("invalid disabled comments")
    79  }
    80  
    81  func (c command) enabled(
    82  	ruleID string,
    83  ) bool {
    84  	return c.t == commandEnable && c.matchRuleID(ruleID)
    85  }
    86  
    87  func (c command) disabled(
    88  	ruleID string,
    89  ) bool {
    90  	return c.t == commandDisable && c.matchRuleID(ruleID)
    91  }
    92  
    93  func (c command) disabledNext(
    94  	ruleID string,
    95  ) bool {
    96  	return c.t == commandDisableNext && c.matchRuleID(ruleID)
    97  }
    98  
    99  func (c command) disabledThis(
   100  	ruleID string,
   101  ) bool {
   102  	return c.t == commandDisableThis && c.matchRuleID(ruleID)
   103  }
   104  
   105  func (c command) matchRuleID(
   106  	ruleID string,
   107  ) bool {
   108  	for _, id := range c.ruleIDs {
   109  		if id == ruleID {
   110  			return true
   111  		}
   112  	}
   113  	return false
   114  }