Class: Goodcheck::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/goodcheck/analyzer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rule:, trigger:, buffer:) ⇒ Analyzer

Returns a new instance of Analyzer.



7
8
9
10
11
# File 'lib/goodcheck/analyzer.rb', line 7

def initialize(rule:, trigger:, buffer:)
  @rule = rule
  @trigger = trigger
  @buffer = buffer
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



5
6
7
# File 'lib/goodcheck/analyzer.rb', line 5

def buffer
  @buffer
end

#ruleObject (readonly)

Returns the value of attribute rule.



3
4
5
# File 'lib/goodcheck/analyzer.rb', line 3

def rule
  @rule
end

#triggerObject (readonly)

Returns the value of attribute trigger.



4
5
6
# File 'lib/goodcheck/analyzer.rb', line 4

def trigger
  @trigger
end

Instance Method Details

#scan(&block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/goodcheck/analyzer.rb', line 13

def scan(&block)
  if block_given?
    if trigger.patterns.empty?
      yield Issue.new(buffer: buffer, rule: rule)
    else
      var_pats, novar_pats = trigger.patterns.partition {|pat|
        pat.is_a?(Pattern::Token) && !pat.variables.empty?
      }

      unless var_pats.empty?
        var_pats.each do |pat|
          scan_var pat, &block
        end
      end

      unless novar_pats.empty?
        regexp = Regexp.union(*novar_pats.map(&:regexp))
        scan_simple regexp, &block
      end
    end
  else
    enum_for(:scan)
  end
end

#scan_simple(regexp, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/goodcheck/analyzer.rb', line 38

def scan_simple(regexp, &block)
  unless trigger.negated?
    issues = []

    scanner = StringScanner.new(buffer.content)

    while true
      case
      when scanner.scan_until(regexp)
        issues << new_issue_with_matched(scanner)
      else
        break
      end
    end

    issues.each(&block)
  else
    unless regexp =~ buffer.content
      yield Issue.new(buffer: buffer, rule: rule)
    end
  end
end

#scan_var(pat) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/goodcheck/analyzer.rb', line 61

def scan_var(pat)
  scanner = StringScanner.new(buffer.content)

  unless trigger.negated?
    while true
      case
      when scanner.scan_until(pat.regexp)
        if pat.test_variables(scanner)
          yield new_issue_with_matched(scanner)
        end
      else
        break
      end
    end
  else
    while true
      case
      when scanner.scan_until(pat.regexp)
        if pat.test(scanner)
          break
        end
      else
        yield Issue.new(buffer: buffer, rule: rule)
        break
      end
    end
  end
end