Class: RuboCop::Cop::Generator::RegistrationInjector Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/cop/generator/registration_injector.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.

A class that injects a register_cop directive into the cop's department module so that the new cop is registered for lazy loading. The directive is injected in alphabetical order among the existing directives without reordering them, since the registration order defines the cop execution order. When the department module file does not exist yet, it is created, and a require for it is injected into the root RuboCop file.

Anything unexpected in the department module, such as a commented-out or otherwise unparsable register_cop line, raises an error instead of silently producing a broken registration.

Constant Summary collapse

EXTEND_PATTERN =

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.

/\A(?<indent>\s*)extend LazyLoader\n\z/.freeze
ENTRY_PATTERN =

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.

%r{
  \A(?<indent>\s*)register_cop\s
  :(?<constant>[A-Za-z0-9]+),\s
  "\#\{__dir__\}/(?<path>[a-z0-9_/]+)"\n\z
}x.freeze
DEPARTMENT_REQUIRE_PATTERN =

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.

%r{\Arequire_relative 'rubocop/cop/[a-z0-9_/]+'\n\z}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(source_path:, badge:, root_file_path: 'lib/rubocop.rb', output: $stdout) ⇒ RegistrationInjector

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.

Returns a new instance of RegistrationInjector.



25
26
27
28
29
30
31
# File 'lib/rubocop/cop/generator/registration_injector.rb', line 25

def initialize(source_path:, badge:, root_file_path: 'lib/rubocop.rb', output: $stdout)
  @source_path = Pathname(source_path)
  @badge = badge
  @root_file_path = root_file_path
  @department_file_path = Pathname("#{@source_path.dirname}.rb")
  @output = output
end

Instance Method Details

#injectObject

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.



33
34
35
36
37
38
39
40
# File 'lib/rubocop/cop/generator/registration_injector.rb', line 33

def inject
  if File.exist?(department_file_path)
    inject_directive
  else
    create_department_file
    inject_department_require
  end
end