Class: Gnurr::Linter

Inherits:
Object
  • Object
show all
Includes:
CLI, Git, Helper
Defined in:
lib/gnurr/linter.rb

Overview

Base linter class from which each linter extends

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Git

#extract_lines, #file_diffs, #full_file_diff

Methods included from Helper

#array_to_ranges, #escaped_filename, #left_bump, #log_error, #severity_color

Methods included from CLI

#print_messages

Constructor Details

#initialize(options) ⇒ Linter

Returns a new instance of Linter.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gnurr/linter.rb', line 16

def initialize(options)
  @errors = []
  @options = {
    base: 'master',
    expanded: false,
    stdout: false,
    verbose: false,
    path: nil
  }.merge(options)
  raise "Dependency not available for #{type}" unless requirements_met?
rescue => e
  log_error(e)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



14
15
16
# File 'lib/gnurr/linter.rb', line 14

def errors
  @errors
end

Instance Method Details

#executeObject



30
31
32
# File 'lib/gnurr/linter.rb', line 30

def execute
  @options[:stdout] ? print_messages : messages
end

#filesObject



34
35
36
# File 'lib/gnurr/linter.rb', line 34

def files
  @files ||= Hash[full_file_diff.select { |file, _lines| filter(file) }]
end

#filter_messages(messages) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/gnurr/linter.rb', line 38

def filter_messages(messages)
  messages.map do |filename, msgs|
    msgs.reject! do |msg|
      !files[relative_filename(filename)].include?(msg[:line])
    end
    msgs.empty? ? nil : [filename, msgs]
  end.reject(&:nil?)
end

#full_range(filename) ⇒ Object



47
48
49
# File 'lib/gnurr/linter.rb', line 47

def full_range(filename)
  [Range.new(1, `wc -l < #{filename}`.to_i + 1)]
end

#line_diffsObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/gnurr/linter.rb', line 51

def line_diffs
  Hash[
    files.map do |filename, lines|
      [
        filename,
        @options[:expanded] ? full_range(filename) : array_to_ranges(lines)
      ]
    end
  ]
end

#relevant_messagesObject



62
63
64
65
66
67
68
# File 'lib/gnurr/linter.rb', line 62

def relevant_messages
  return {} if files.empty?
  JSON.parse(run_command("#{command} #{escaped_files.join(' ')}"))
rescue => e
  log_error(e)
  {}
end

#violation_countObject



70
71
72
# File 'lib/gnurr/linter.rb', line 70

def violation_count
  messages.map(&:last).flatten.length
end