Class: Pronto::Json::Runner

Inherits:
Runner
  • Object
show all
Defined in:
lib/pronto/json/runner.rb

Instance Method Summary collapse

Instance Method Details

#detect_line(candidate_lines, error_line_no) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/pronto/json/runner.rb', line 29

def detect_line(candidate_lines, error_line_no)
  candidate_lines.sort do |x, y|
    sort_result = (line_number(x) - error_line_no).abs <=> (line_number(y) - error_line_no).abs
    if sort_result.zero? # prefer addition line
      sort_result = x.addition? ? -1 : 1
    end
    sort_result
  end.first
end

#inspect(patch) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pronto/json/runner.rb', line 16

def inspect(patch)
  begin
    Oj.load_file(patch.new_file_full_path.to_s)
  rescue Oj::ParseError => e
    error = oj_error(e)
  end

  return unless error

  candidate_lines = patch.lines.select { |line| line.addition? || line.deletion? }
  new_message(error[:reason], detect_line(candidate_lines, error[:line]))
end

#json_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/pronto/json/runner.rb', line 46

def json_file?(path)
  File.extname(path) == '.json'
end

#line_number(line) ⇒ Object



62
63
64
# File 'lib/pronto/json/runner.rb', line 62

def line_number(line)
  line.addition? ? line.new_lineno : line.old_lineno
end

#new_message(error, line) ⇒ Object



39
40
41
42
43
44
# File 'lib/pronto/json/runner.rb', line 39

def new_message(error, line)
  path = line.patch.delta.new_file[:path]
  level = :error

  Message.new(path, line, level, error, nil, self.class)
end

#oj_error(error) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pronto/json/runner.rb', line 50

def oj_error(error)
  # ... at line 38, column 1
  error_message = error.message
  if match = error_message.match(/at line (\d+), column (\d+)/i)
    line, _column = match.captures
  else
    line = 1
  end

  { line: line.to_i, reason: error_message }
end

#runObject



7
8
9
10
11
12
13
14
# File 'lib/pronto/json/runner.rb', line 7

def run
  return [] unless @patches

  @patches
    .select { |patch| patch.delta.status != :deleted }
    .select { |patch| json_file?(patch.new_file_full_path) }
    .map { |patch| inspect(patch) }
end