Module: Solargraph::Diagnostics::RubocopHelpers
- Included in:
- Rubocop, LanguageServer::Message::TextDocument::Formatting
- Defined in:
- lib/solargraph/diagnostics/rubocop_helpers.rb
Overview
Utility methods for the RuboCop diagnostics reporter.
Class Method Summary collapse
-
.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.
-
.generate_options(filename, code) ⇒ Array(Array<String>, Array<String>)
Generate command-line options for the specified filename and code.
- .redirect_stdout ⇒ String
-
.require_rubocop(version = nil) ⇒ Object
Requires a specific version of rubocop, or the latest installed version if version is ‘nil`.
Class Method Details
.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.
48 49 50 51 |
# File 'lib/solargraph/diagnostics/rubocop_helpers.rb', line 48 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.
35 36 37 38 39 40 41 |
# File 'lib/solargraph/diagnostics/rubocop_helpers.rb', line 35 def filename, code args = ['-f', 'j', '--force-exclusion', filename] = RuboCop::Options.new , paths = .parse(args) [:stdin] = code [, paths] end |
.redirect_stdout ⇒ String
TODO:
This is a smelly way to redirect output, but the RuboCop specs do the same thing.
56 57 58 59 60 61 62 |
# File 'lib/solargraph/diagnostics/rubocop_helpers.rb', line 56 def redirect_stdout redir = StringIO.new $stdout = redir yield if block_given? $stdout = STDOUT redir.string end |
.require_rubocop(version = nil) ⇒ Object
Requires a specific version of rubocop, or the latest installed version if version is ‘nil`.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/solargraph/diagnostics/rubocop_helpers.rb', line 15 def require_rubocop(version = nil) begin gem_path = Gem::Specification.find_by_name('rubocop', version).full_gem_path gem_lib_path = File.join(gem_path, 'lib') $LOAD_PATH.unshift(gem_lib_path) unless $LOAD_PATH.include?(gem_lib_path) # @todo Gem::MissingSpecVersionError is undocumented for some reason # @sg-ignore rescue Gem::MissingSpecVersionError => e raise InvalidRubocopVersionError, "could not find '#{e.name}' (#{e.requirement}) - "\ "did find: [#{e.specs.map { |s| s.version.version }.join(', ')}]" end require 'rubocop' end |