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

     1  package disablerule
     2  
     3  import "github.com/yoheimuta/go-protoparser/v4/parser"
     4  
     5  // Interpreter represents an interpreter for disable rule comments.
     6  type Interpreter struct {
     7  	ruleID     string
     8  	isDisabled bool
     9  }
    10  
    11  // NewInterpreter creates an Interpreter.
    12  func NewInterpreter(
    13  	ruleID string,
    14  ) *Interpreter {
    15  	return &Interpreter{
    16  		ruleID: ruleID,
    17  	}
    18  }
    19  
    20  // Interpret interprets comments and returns a bool whether not apply the rule to a next or this element.
    21  func (i *Interpreter) Interpret(
    22  	comments []*parser.Comment,
    23  	inlines ...*parser.Comment,
    24  ) (isDisabled bool) {
    25  	cmds := newCommands(comments)
    26  	inlineCmds := newCommands(inlines)
    27  	allCmds := append(append([]command{}, cmds...), inlineCmds...)
    28  	return i.interpret(allCmds) ||
    29  		i.interpretNext(cmds) ||
    30  		i.interpretThis(inlineCmds) ||
    31  		i.isDisabled
    32  }
    33  
    34  // CallEachIfValid calls a given function each time the line is not disabled.
    35  func (i *Interpreter) CallEachIfValid(
    36  	lines []string,
    37  	f func(index int, line string),
    38  ) {
    39  	shouldSkip := false
    40  
    41  	for index, line := range lines {
    42  		cmd, err := newCommand(line)
    43  		if err != nil {
    44  			if !i.isDisabled && !shouldSkip {
    45  				f(index, line)
    46  			}
    47  			if shouldSkip {
    48  				shouldSkip = false
    49  			}
    50  			continue
    51  		}
    52  
    53  		if cmd.enabled(i.ruleID) {
    54  			i.isDisabled = false
    55  			f(index, line)
    56  			continue
    57  		}
    58  
    59  		if cmd.disabled(i.ruleID) {
    60  			i.isDisabled = true
    61  			continue
    62  		}
    63  
    64  		if cmd.disabledThis(i.ruleID) {
    65  			continue
    66  		}
    67  
    68  		if cmd.disabledNext(i.ruleID) {
    69  			shouldSkip = true
    70  			f(index, line)
    71  			continue
    72  		}
    73  
    74  		if shouldSkip {
    75  			shouldSkip = false
    76  			continue
    77  		}
    78  		if i.isDisabled {
    79  			continue
    80  		}
    81  
    82  		f(index, line)
    83  	}
    84  }
    85  
    86  func (i *Interpreter) interpret(
    87  	cmds commands,
    88  ) bool {
    89  	id := i.ruleID
    90  	if cmds.enabled(id) {
    91  		i.isDisabled = false
    92  		return false
    93  	}
    94  	if cmds.disabled(id) {
    95  		i.isDisabled = true
    96  		return true
    97  	}
    98  	return false
    99  }
   100  
   101  func (i *Interpreter) interpretNext(
   102  	cmds commands,
   103  ) bool {
   104  	id := i.ruleID
   105  	if cmds.disabledNext(id) {
   106  		return true
   107  	}
   108  	return false
   109  }
   110  
   111  func (i *Interpreter) interpretThis(
   112  	cmds commands,
   113  ) bool {
   114  	id := i.ruleID
   115  	if cmds.disabledThis(id) {
   116  		return true
   117  	}
   118  	return false
   119  }