Class: CopsDocumentationGenerator Private

Inherits:
Object
  • Object
show all
Includes:
RuboCop::Cop::Documentation
Defined in:
lib/rubocop/cops_documentation_generator.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class for generating documentation of all cops departments

Defined Under Namespace

Classes: CopData

Constant Summary collapse

STRUCTURE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  name:                  ->(data) { cop_header(data.cop) },
  required_ruby_version: ->(data) { required_ruby_version(data.cop) },
  properties:            ->(data) { properties(data.cop) },
  description:           ->(data) { "#{data.description}\n" },
  safety:                ->(data) { safety_object(data.safety_objects, data.cop) },
  examples:              ->(data) { examples(data.example_objects, data.cop) },
  configuration:         ->(data) { configurations(data.cop.department, data.cop, data.config) },
  references:            ->(data) { references(data.cop, data.see_objects) }
}.freeze

Instance Method Summary collapse

Methods included from RuboCop::Cop::Documentation

base_url_for, builtin?, default_base_url, default_extension, department_to_basename, extension_for, url_for

Constructor Details

#initialize(departments: [], extra_info: {}, base_dir: Dir.pwd, plugin_name: nil) ⇒ CopsDocumentationGenerator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This class will only generate documentation for cops that belong to one of the departments given in the ‘departments` array. E.g. if we only wanted documentation for Lint cops:

CopsDocumentationGenerator.new(departments: ['Lint']).call

For plugin extensions, specify ‘:plugin_name` keyword as follows:

CopsDocumentationGenerator.new(
  departments: ['Performance'], plugin_name: 'rubocop-performance'
).call

You can append additional information:

callback = ->(data) { required_rails_version(data.cop) }
CopsDocumentationGenerator.new(extra_info: { ruby_version: callback }).call

This will insert the string returned from the lambda after the section from RuboCop itself. See ‘CopsDocumentationGenerator::STRUCTURE` for available sections.

[View source]

45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubocop/cops_documentation_generator.rb', line 45

def initialize(departments: [], extra_info: {}, base_dir: Dir.pwd, plugin_name: nil)
  @departments = departments.map(&:to_sym).sort!
  @extra_info = extra_info
  @cops = RuboCop::Cop::Registry.global
  @config = RuboCop::ConfigLoader.default_configuration
  # NOTE: For example, this prevents excessive plugin loading before another task executes,
  # in cases where plugins are already loaded by `internal_investigation`.
  if plugin_name && @config.loaded_plugins.none? { |plugin| plugin.about.name == plugin_name }
    RuboCop::Plugin.integrate_plugins(RuboCop::Config.new, [plugin_name])
  end
  @base_dir = base_dir
  @docs_path = "#{base_dir}/docs/modules/ROOT"
  FileUtils.mkdir_p("#{@docs_path}/pages")
end

Instance Method Details

#callObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

[View source]

60
61
62
63
64
65
66
67
# File 'lib/rubocop/cops_documentation_generator.rb', line 60

def call
  YARD::Registry.load!
  departments.each { |department| print_cops_of_department(department) }

  print_table_of_contents
ensure
  RuboCop::ConfigLoader.default_configuration = nil
end