class SandiMeter::WarningScanner

Constants

INDENTATION_WARNING_REGEXP

Attributes

indentation_warnings[R]

Public Instance Methods

scan(source) click to toggle source
# File lib/sandi_meter/warning_scanner.rb, line 9
def scan(source)
  status, @warnings, process = if defined? Bundler
                              Bundler.with_clean_env do
                                   validate(source)
                                end
                              else
                                validate(source)
                              end

  check_syntax(status)
  @indentation_warnings = parse_warnings
end

Private Instance Methods

check_syntax(status) click to toggle source
# File lib/sandi_meter/warning_scanner.rb, line 27
def check_syntax(status)
  raise SyntaxError, @warnings unless !!(status =~ /Syntax\sOK/)
end
check_token_lines(token, line_num, end_line_num) click to toggle source
# File lib/sandi_meter/warning_scanner.rb, line 31
def check_token_lines(token, line_num, end_line_num)
  raise 'No valid end line number' unless end_line_num =~ /^\d+$/
  raise 'No valid line number' unless line_num =~ /^\d+$/
  raise 'No valid token ("def" or "class")' unless token =~ /^def|class|module$/
end
extract_indentation_mismatch(warning_line) click to toggle source
# File lib/sandi_meter/warning_scanner.rb, line 37
def extract_indentation_mismatch(warning_line)
  _, end_line_num, warning_type, warning_body = warning_line.split(':').map(&:strip)
  return nil unless warning_type == 'warning'
  return nil unless warning_body =~ /at 'end' with '(def|class|module)' at (\d+)\z/

  res = warning_body.match(INDENTATION_WARNING_REGEXP)[1..2] << end_line_num
  check_token_lines(*res)

  res
end
parse_warnings() click to toggle source
# File lib/sandi_meter/warning_scanner.rb, line 48
def parse_warnings
  @warnings.split("\n").inject({}) do |warnings, warning|
    token, line, end_line = extract_indentation_mismatch(warning)
    warnings[token] ||= []
    warnings[token] << [line.to_i, end_line.to_i]
    warnings
  end
end
validate(source) click to toggle source
# File lib/sandi_meter/warning_scanner.rb, line 23
def validate(source)
  Open3.capture3("#{RUBY_ENGINE} -wc", stdin_data: source)
end