Class: Ruby::Merge::BlockDirectiveDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/merge/block_directive_detector.rb

Overview

Detects Ruby comment block directive pairs in source lines.

This is Ruby-specific substrate behavior shared by Ruby parser providers. Parser-specific gems may consume the spans and project them into their own node types, but directive token semantics should live here.

Defined Under Namespace

Classes: Span

Constant Summary collapse

NOCOV_TOKEN =
':nocov:'
SIMPLECOV_DISABLE_RE =
/\A\s*#\s*simplecov\s*:\s*disable\b/i
SIMPLECOV_ENABLE_RE =
/\A\s*#\s*simplecov\s*:\s*enable\b/i
DIRECTIVE_CONTENT_RE =
/\A(?::nocov:|[\w-]+:(?:freeze|unfreeze))\z/i

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines, freeze_token: nil, nocov_token: NOCOV_TOKEN, source_label: nil) ⇒ BlockDirectiveDetector

Returns a new instance of BlockDirectiveDetector.



44
45
46
47
48
49
# File 'lib/ruby/merge/block_directive_detector.rb', line 44

def initialize(lines, freeze_token: nil, nocov_token: NOCOV_TOKEN, source_label: nil)
  @lines = lines
  @freeze_token = freeze_token
  @nocov_token = nocov_token
  @source_label = source_label
end

Class Method Details

.coverage_directive_line?(line, nocov_token: NOCOV_TOKEN) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
# File 'lib/ruby/merge/block_directive_detector.rb', line 19

def coverage_directive_line?(line, nocov_token: NOCOV_TOKEN)
  stripped = line.to_s.chomp
  stripped.match?(simplecov_disable_re) ||
    stripped.match?(simplecov_enable_re) ||
    stripped.match?(nocov_re(nocov_token))
end

.directive_content?(content) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/ruby/merge/block_directive_detector.rb', line 26

def directive_content?(content)
  DIRECTIVE_CONTENT_RE.match?(content.to_s.strip) ||
    coverage_directive_line?("# #{content}")
end

.nocov_re(nocov_token = NOCOV_TOKEN) ⇒ Object



39
40
41
# File 'lib/ruby/merge/block_directive_detector.rb', line 39

def nocov_re(nocov_token = NOCOV_TOKEN)
  /\A\s*#\s?#{Regexp.escape(nocov_token)}\s*\z/i
end

.simplecov_disable_reObject



31
32
33
# File 'lib/ruby/merge/block_directive_detector.rb', line 31

def simplecov_disable_re
  SIMPLECOV_DISABLE_RE
end

.simplecov_enable_reObject



35
36
37
# File 'lib/ruby/merge/block_directive_detector.rb', line 35

def simplecov_enable_re
  SIMPLECOV_ENABLE_RE
end

Instance Method Details

#detect_spansObject



51
52
53
54
55
56
57
# File 'lib/ruby/merge/block_directive_detector.rb', line 51

def detect_spans
  raw_spans = []
  raw_spans.concat(detect_freeze_spans) if @freeze_token
  raw_spans.concat(detect_nocov_spans)

  validate_no_crossing(raw_spans.sort_by(&:start_line))
end