Class: RubyModKit::CorrectorManager

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_mod_kit/corrector_manager.rb

Overview

the class to manege parse error correctors

Instance Method Summary collapse

Constructor Details

#initialize(features) ⇒ void

Parameters:

rbs:

  • features: Array

  • return: void



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ruby_mod_kit/corrector_manager.rb', line 15

def initialize(features)
  @previous_source = +""
  @correctors_error_map = {}
  features.each do |feature|
    feature.create_correctors.each do |corrector|
      corrector.correctable_error_types.each do |error_type|
        (@correctors_error_map[error_type] ||= []) << corrector
      end
    end
  end
end

Instance Method Details

#check_prev_errors(generation) ⇒ void

This method returns an undefined value.

Parameters:

Raises:

rbs:

  • generation: Generation

  • return: void



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ruby_mod_kit/corrector_manager.rb', line 55

def check_prev_errors(generation)
  return if @previous_source.empty?
  return if generation.errors.empty?
  return if @previous_source != generation.script

  message = +""
  generation.errors.each do |parse_error|
    message << "\n" unless message.empty?
    message << "#{generation.name}:#{parse_error.location.start_line}:#{parse_error.message} "
    message << "(#{parse_error.type})"
    line = generation.line(parse_error)
    if line
      message << "\n#{line.chomp}\n"
      message << "#{" " * parse_error.location.start_column}^#{"~" * [parse_error.location.length - 1, 0].max}"
    end
  end
  raise RubyModKit::SyntaxError, message
end

#perform(generation) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)

rbs:

  • generation: Generation

  • return: bool



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ruby_mod_kit/corrector_manager.rb', line 31

def perform(generation)
  return true if generation.errors.empty?

  check_prev_errors(generation)
  @previous_source = generation.script.dup

  @correctors_error_map.each_value do |correctors|
    correctors.each(&:setup)
  end

  generation.errors.each do |parse_error|
    correctors = @correctors_error_map[parse_error.type] || next
    correctors.each do |corrector|
      corrector.correct(parse_error, generation)
    end
  end

  false
end