Class: SimpleCov::Directive

Inherits:
Object
  • Object
show all
Defined in:
lib/simplecov/directive.rb,
lib/simplecov/directive/erb.rb,
lib/simplecov/directive/haml.rb,
lib/simplecov/directive/slim.rb,
lib/simplecov/directive/template.rb,
lib/simplecov/directive/indented_template.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.

Defined Under Namespace

Modules: Erb, Haml, IndentedTemplate, Slim, Template

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.



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

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

Instance Attribute Details

#categoriesObject (readonly)

Returns the value of attribute categories.



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

def categories
  @categories
end

#line_numberObject (readonly)

Returns the value of attribute line_number.



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

def line_number
  @line_number
end

#modeObject (readonly)

Returns the value of attribute mode.



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

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.



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

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.



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

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)


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

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

#inline?Boolean

Returns:

  • (Boolean)


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

def inline?
  @inline
end