class RubocopChallenger::Rubocop::Yardoc

To read YARD style documentation from rubocop gem source code

Attributes

cop_class[R]
yardoc[R]

Public Class Methods

new(cop) click to toggle source
# File lib/rubocop_challenger/rubocop/yardoc.rb, line 7
def initialize(cop)
  load_rspec_gems!
  @cop_class = find_cop_class(cop)
  load_yardoc!
end

Public Instance Methods

description() click to toggle source
# File lib/rubocop_challenger/rubocop/yardoc.rb, line 13
def description
  yardoc.docstring
end
examples() click to toggle source
# File lib/rubocop_challenger/rubocop/yardoc.rb, line 17
def examples
  yardoc.tags('example').map { |tag| [tag.name, tag.text] }
end
safe_autocorrect?() click to toggle source

Indicates whether the auto-correct a cop does is safe (equivalent) by design. If a cop is unsafe its auto-correct automatically becomes unsafe as well.

@return [Boolean]

# File lib/rubocop_challenger/rubocop/yardoc.rb, line 25
def safe_autocorrect?
  config = RuboCop::ConfigLoader.default_configuration
  cop_class.new(config).safe_autocorrect?
end

Private Instance Methods

find_cop_class(cop) click to toggle source

Find a RuboCop class by cop name. It find from rubocop/rspec if cannot find any class from rubocop gem.

@param cop [String] The target cop name (e.g. “Performance/Size”) @return [RuboCop::Cop] Found a RuboCop::Cop class

# File lib/rubocop_challenger/rubocop/yardoc.rb, line 48
def find_cop_class(cop)
  Object.const_get("RuboCop::Cop::#{cop.sub('/', '::')}")
rescue NameError
  Object.const_get("RuboCop::Cop::RSpec::#{cop.sub('/', '::')}")
end
instance_methods() click to toggle source
# File lib/rubocop_challenger/rubocop/yardoc.rb, line 61
def instance_methods
  [
    cop_class.instance_methods(false),
    cop_class.private_instance_methods(false)
  ].flatten!
end
load_rspec_gems!() click to toggle source

Loads gems for YARDoc creation

# File lib/rubocop_challenger/rubocop/yardoc.rb, line 35
def load_rspec_gems!
  RSPEC_GEMS.each do |dependency|
    require dependency
  rescue LoadError
    nil
  end
end
load_yardoc!() click to toggle source

Loads yardoc from the RuboCop::Cop class file

# File lib/rubocop_challenger/rubocop/yardoc.rb, line 55
def load_yardoc!
  YARD.parse(source_file_path)
  @yardoc = YARD::Registry.all(:class).first
  YARD::Registry.clear
end
source_file_path() click to toggle source
# File lib/rubocop_challenger/rubocop/yardoc.rb, line 68
def source_file_path
  if Object.respond_to?(:const_source_location)
    Object.const_source_location(cop_class.name).first
  else
    instance_methods
      .map { |m| cop_class.instance_method(m).source_location }
      .reject(&:nil?)
      .map(&:first)
      .first
  end
end