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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/archspec/analyzer.rb', line 72

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, 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