Module: Solargraph::Diagnostics::RubocopHelpers

Included in:
Rubocop
Defined in:
lib/solargraph/diagnostics/rubocop_helpers.rb

Overview

Utility methods for the RuboCop diagnostics reporter.

Class Method Summary collapse

Class Method Details

.find_rubocop_file(filename) ⇒ String?

Find a RuboCop configuration file in a file's directory tree.


27
28
29
30
31
32
33
34
35
# File 'lib/solargraph/diagnostics/rubocop_helpers.rb', line 27

def find_rubocop_file filename
  dir = File.dirname(filename)
  until File.dirname(dir) == dir
    here = File.join(dir, '.rubocop.yml')
    return here if File.exist?(here)
    dir = File.dirname(dir)
  end
  nil
end

.fix_drive_letter(path) ⇒ String

RuboCop internally uses capitalized drive letters for Windows paths, so we need to convert the paths provided to the command.


42
43
44
45
# File 'lib/solargraph/diagnostics/rubocop_helpers.rb', line 42

def fix_drive_letter path
  return path unless path.match(/^[a-z]:/)
  path[0].upcase + path[1..-1]
end

.generate_options(filename, code) ⇒ Array(Array<String>, Array<String>)

Generate command-line options for the specified filename and code.


13
14
15
16
17
18
19
20
21
# File 'lib/solargraph/diagnostics/rubocop_helpers.rb', line 13

def generate_options filename, code
  args = ['-f', 'j']
  rubocop_file = find_rubocop_file(filename)
  args.push('-c', fix_drive_letter(rubocop_file)) unless rubocop_file.nil?
  args.push filename
  options, paths = RuboCop::Options.new.parse(args)
  options[:stdin] = code
  [options, paths]
end