module Solargraph::Diagnostics::RubocopHelpers
Utility methods for the RuboCop diagnostics reporter.
Public Instance Methods
Source
# File lib/solargraph/diagnostics/rubocop_helpers.rb, line 51 def fix_drive_letter path return path unless path.match(/^[a-z]:/) path[0].upcase + path[1..-1] end
RuboCop internally uses capitalized drive letters for Windows paths, so we need to convert the paths provided to the command.
@param path [String] @return [String]
Source
# File lib/solargraph/diagnostics/rubocop_helpers.rb, line 37 def generate_options filename, code args = ['-f', 'j', '--force-exclusion', filename] base_options = RuboCop::Options.new options, paths = base_options.parse(args) # @sg-ignore options[:stdin] = code [options, paths] end
Generate command-line options for the specified filename and code.
@param filename [String] @param code [String] @return [Array(Array<String>, Array<String>)]
Source
# File lib/solargraph/diagnostics/rubocop_helpers.rb, line 59 def redirect_stdout redir = StringIO.new $stdout = redir yield if block_given? $stdout = STDOUT redir.string end
@todo This is a smelly way to redirect output, but the RuboCop specs do
the same thing.
@return [String]
Source
# File lib/solargraph/diagnostics/rubocop_helpers.rb, line 16 def require_rubocop(version = nil) begin # @type [String] 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) rescue Gem::MissingSpecVersionError => e # @type [Array<Gem::Specification>] specs = e.specs raise InvalidRubocopVersionError, "could not find '#{e.name}' (#{e.requirement}) - "\ "did find: [#{specs.map { |s| s.version.version }.join(', ')}]" end require 'rubocop' end
Requires a specific version of rubocop, or the latest installed version if version is ‘nil`.
@param version [String, nil] @raise [InvalidRubocopVersionError] if version is not installed @return [void]