Module: MetzScan::Commands::Scan::OffenseExtractor

Defined in:
lib/metz_scan/commands/scan/offense_extractor.rb

Class Method Summary collapse

Class Method Details

.base_offense(path, offense) ⇒ Object



22
23
24
25
26
# File 'lib/metz_scan/commands/scan/offense_extractor.rb', line 22

def base_offense(path, offense)
  { path: path, cop_name: offense["cop_name"], severity: offense["severity"],
    message: offense["message"], why_it_matters: offense["why_it_matters"],
    project_analyzer: offense["project_analyzer"] }
end

.file_offenses(file) ⇒ Object



13
14
15
# File 'lib/metz_scan/commands/scan/offense_extractor.rb', line 13

def file_offenses(file)
  Array(file["offenses"]).map { |o| offense_struct(file["path"], o) }
end

.location_fields(location) ⇒ Object



28
29
30
31
# File 'lib/metz_scan/commands/scan/offense_extractor.rb', line 28

def location_fields(location)
  { line: location_value(location, "start_line", "line"),
    column: location_value(location, "start_column", "column") }
end

.location_value(location, primary_key, fallback_key) ⇒ Object

Raises:

  • (KeyError)


33
34
35
36
37
38
# File 'lib/metz_scan/commands/scan/offense_extractor.rb', line 33

def location_value(location, primary_key, fallback_key)
  return location[primary_key] if location.key?(primary_key)
  return location[fallback_key] if location.key?(fallback_key)

  raise KeyError, "RuboCop JSON location missing #{primary_key}/#{fallback_key}"
end

.offense_struct(path, offense) ⇒ Object



17
18
19
20
# File 'lib/metz_scan/commands/scan/offense_extractor.rb', line 17

def offense_struct(path, offense)
  loc = offense["location"] || {}
  base_offense(path, offense).merge(location_fields(loc))
end

.offenses(parsed) ⇒ Object



9
10
11
# File 'lib/metz_scan/commands/scan/offense_extractor.rb', line 9

def offenses(parsed)
  Array(parsed["files"]).flat_map { |file| file_offenses(file) }
end