Class: SimpleCov::Directive

Inherits:
Object
  • Object
show all
Defined in:
lib/simplecov/directive.rb

Overview

Parses # simplecov:disable / # simplecov:enable directive comments, in both the block form (the directive is the entire comment on its own line, opening a region that runs until the matching enable) and the inline form (the directive trails real code, affecting that line alone).

Categories are :line, :branch, and :method, combinable with commas. Omitting categories targets all three. Any text after the directive is a free-form reason and is discarded, so an unrecognised category name silently falls into the reason bucket and disables everything: a deliberate over-disable, so the typo is visible in the report rather than silently disabling nothing.

Comment extraction goes through Ripper.lex so directive markers inside string literals or heredocs are correctly ignored.

Constant Summary collapse

CATEGORIES =
%i[line branch method].freeze
CATEGORY_PATTERN =
"(?:#{CATEGORIES.join("|")})".freeze
CATEGORIES_PATTERN =
"(?:#{CATEGORY_PATTERN}(?:\\s*,\\s*#{CATEGORY_PATTERN})*)".freeze
PATTERN =
/
  \#\s*simplecov\s*:\s*
  (?<mode>disable|enable)\b
  (?:\s+(?<categories>#{CATEGORIES_PATTERN})\b)?
  .*?
  \s*\z
/x

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line_number:, mode:, categories:, inline:) ⇒ Directive

Returns a new instance of Directive.



122
123
124
125
126
127
# File 'lib/simplecov/directive.rb', line 122

def initialize(line_number:, mode:, categories:, inline:)
  @line_number = line_number
  @mode = mode
  @categories = categories
  @inline = inline
end

Instance Attribute Details

#categories ⇒ Object (readonly)

Returns the value of attribute categories.



33
34
35
# File 'lib/simplecov/directive.rb', line 33

def categories
  @categories
end

#line_number ⇒ Object (readonly)

Returns the value of attribute line_number.



33
34
35
# File 'lib/simplecov/directive.rb', line 33

def line_number
  @line_number
end

#mode ⇒ Object (readonly)

Returns the value of attribute mode.



33
34
35
# File 'lib/simplecov/directive.rb', line 33

def mode
  @mode
end

Class Method Details

.disabled_ranges(lines) ⇒ Object

The disabled line ranges per category. An unclosed disable block extends to the end of the file.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/simplecov/directive.rb', line 37

def self.disabled_ranges(lines)
  ranges = CATEGORIES.to_h do |category|
    empty = [] # : Array[Range[Integer]]
    [category, empty]
  end
  open_starts = {} # : Hash[Symbol, Integer]

  directives_in(lines).each { |directive| directive.apply(ranges, open_starts) }
  open_starts.each { |category, start| ranges.fetch(category) << (start..lines.size) }

  ranges
end

Instance Method Details

#apply(ranges, open_starts) ⇒ Object

Inline directives mark just their line; block disables open a region; block enables close one. Re-opening an already-open block is a no-op.



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/simplecov/directive.rb', line 139

def apply(ranges, open_starts)
  categories.each do |category|
    if inline?
      ranges.fetch(category) << (line_number..line_number) if disabled?
    elsif disabled?
      open_starts[category] ||= line_number
    elsif (start = open_starts.delete(category))
      ranges.fetch(category) << (start..line_number)
    end
  end
end

#disabled? ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/simplecov/directive.rb', line 129

def disabled?
  mode.equal?(:disable)
end

#inline? ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/simplecov/directive.rb', line 133

def inline?
  @inline
end