Run evaluates the given conditional statement.
(blk nlp.Block, f *core.File, cfg *core.Config)
| 79 | |
| 80 | // Run evaluates the given conditional statement. |
| 81 | func (c Conditional) Run(blk nlp.Block, f *core.File, cfg *core.Config) ([]core.Alert, error) { |
| 82 | alerts := []core.Alert{} |
| 83 | |
| 84 | txt := blk.Text |
| 85 | |
| 86 | // When `Second` has no capture group, the rule is a plain presence check: |
| 87 | // if `First` appears, `Second` must appear somewhere in the same block. If |
| 88 | // it does, there's nothing to flag; otherwise every `First` match is a |
| 89 | // violation. See #1048. |
| 90 | if !c.secondHasGroup { |
| 91 | if c.patterns[0].MatchStringStd(txt) { |
| 92 | return alerts, nil |
| 93 | } |
| 94 | return c.flagAntecedents(txt, cfg) |
| 95 | } |
| 96 | |
| 97 | // We first look for the consequent of the conditional statement. |
| 98 | // For example, if we're ensuring that abbreviations have been defined |
| 99 | // parenthetically, we'd have something like: |
| 100 | // |
| 101 | // "WHO" [antecedent], "World Health Organization (WHO)" [consequent] |
| 102 | // |
| 103 | // In other words: if "WHO" exists, it must also have a definition -- which |
| 104 | // we're currently looking for. |
| 105 | matches := c.patterns[0].FindAllStringSubmatch(txt, -1) |
| 106 | for _, mat := range matches { |
| 107 | if len(mat) > 1 { |
| 108 | // If we find one, we store it in a slice associated with this |
| 109 | // particular file. |
| 110 | for _, m := range mat[1:] { |
| 111 | if len(m) > 0 { |
| 112 | f.Sequences = append(f.Sequences, m) |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Now we look for the antecedent. |
| 119 | locs := c.patterns[1].FindAllStringIndex(txt, -1) |
| 120 | for _, loc := range locs { |
| 121 | s, err := re2Loc(txt, loc) |
| 122 | if err != nil { |
| 123 | return alerts, err |
| 124 | } |
| 125 | |
| 126 | if !core.StringInSlice(s, f.Sequences) && !isMatch(c.exceptRe, s) && !withinPhrase(c.phraseRe, txt, loc) { |
| 127 | // If we've found one (e.g., "WHO") and we haven't marked it as |
| 128 | // being defined previously, send an Alert. |
| 129 | a, erra := makeAlert(c.Definition, loc, txt, cfg) |
| 130 | if erra != nil { |
| 131 | return alerts, erra |
| 132 | } |
| 133 | alerts = append(alerts, a) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return alerts, nil |
| 138 | } |
nothing calls this directly
no test coverage detected