Module: ArchSpec::Analyzer::SuppressionParser

Extended by:
SuppressionParser
Included in:
SuppressionParser
Defined in:
lib/archspec/analyzer.rb

Constant Summary collapse

DISABLE_PATTERN =
/\Aarchspec:disable(?:-(next-line|line))?(?:\s+([a-z0-9_.-]+|\*))?(?:\s+--\s*(.+))?\z/i
ENABLE_PATTERN =
/\Aarchspec:enable(?:\s+([a-z0-9_.-]+|\*))?\z/i

Instance Method Summary collapse

Instance Method Details

#parse(comments) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/archspec/analyzer.rb', line 92

def parse(comments)
  suppressions = []
  active = Hash.new { |hash, key| hash[key] = [] }

  sorted_comments(comments).each do |comment|
    text = comment.slice.sub(/\A#\s?/, "").strip
    line = comment.location.start_line

    if (match = text.match(DISABLE_PATTERN))
      mode, rule, reason = match.captures
      rule = normalize_rule(rule)

      case mode
      when "line"
        suppressions << Suppression.new(rule, line, line, reason)
      when "next-line"
        suppressions << Suppression.new(rule, line + 1, line + 1, reason)
      else
        active[rule] << [line + 1, reason]
      end
    elsif (match = text.match(ENABLE_PATTERN))
      rule = normalize_rule(match[1])
      if active[rule].any?
        start_line, reason = active[rule].pop
        suppressions << Suppression.new(rule, start_line, [line - 1, start_line].max, reason)
      end
    end
  end

  active.each do |rule, entries|
    entries.each do |start_line, reason|
      suppressions << Suppression.new(rule, start_line, Float::INFINITY, reason)
    end
  end

  suppressions
end