class RuboCop::Cop::Generator

Source and spec generator for new cops

This generator will take a cop name and generate a source file and spec file when given a valid qualified cop name. @api private

Constants

CONFIGURATION_ADDED_MESSAGE
SOURCE_TEMPLATE
SPEC_TEMPLATE

Attributes

badge[R]
output[R]

Public Class Methods

new(name, output: $stdout) click to toggle source
# File lib/rubocop/cop/generator.rb, line 113
def initialize(name, output: $stdout)
  @badge = Badge.parse(name)
  @output = output
  return if badge.qualified?

  raise ArgumentError, 'Specify a cop name with Department/Name style'
end

Public Instance Methods

inject_config(config_file_path: 'config/default.yml', version_added: '<<next>>') click to toggle source
# File lib/rubocop/cop/generator.rb, line 133
def inject_config(config_file_path: 'config/default.yml',
                  version_added: '<<next>>')
  injector =
    ConfigurationInjector.new(configuration_file_path: config_file_path,
                              badge: badge,
                              version_added: version_added)

  injector.inject do # rubocop:disable Lint/UnexpectedBlockArity
    output.puts(format(CONFIGURATION_ADDED_MESSAGE,
                       configuration_file_path: config_file_path))
  end
end
inject_require(root_file_path: 'lib/rubocop.rb') click to toggle source
# File lib/rubocop/cop/generator.rb, line 129
def inject_require(root_file_path: 'lib/rubocop.rb')
  RequireFileInjector.new(source_path: source_path, root_file_path: root_file_path).inject
end
todo() click to toggle source
# File lib/rubocop/cop/generator.rb, line 146
      def todo
        <<~TODO
          Do 4 steps:
            1. Modify the description of #{badge} in config/default.yml
            2. Implement your new cop in the generated file!
            3. Commit your new cop with a message such as
               e.g. "Add new `#{badge}` cop"
            4. Run `bundle exec rake changelog:new` to generate a changelog entry
               for your new cop.
        TODO
      end
write_source() click to toggle source
# File lib/rubocop/cop/generator.rb, line 121
def write_source
  write_unless_file_exists(source_path, generated_source)
end
write_spec() click to toggle source
# File lib/rubocop/cop/generator.rb, line 125
def write_spec
  write_unless_file_exists(spec_path, generated_spec)
end

Private Instance Methods

generate(template) click to toggle source
# File lib/rubocop/cop/generator.rb, line 183
def generate(template)
  format(template, department: badge.department.to_s.gsub('/', '::'),
                   cop_name: badge.cop_name)
end
generated_source() click to toggle source
# File lib/rubocop/cop/generator.rb, line 175
def generated_source
  generate(SOURCE_TEMPLATE)
end
generated_spec() click to toggle source
# File lib/rubocop/cop/generator.rb, line 179
def generated_spec
  generate(SPEC_TEMPLATE)
end
snake_case(camel_case_string) click to toggle source
# File lib/rubocop/cop/generator.rb, line 208
def snake_case(camel_case_string)
  camel_case_string
    .gsub('RSpec', 'Rspec')
    .gsub(%r{([^A-Z/])([A-Z]+)}, '\1_\2')
    .gsub(%r{([A-Z])([A-Z][^A-Z\d/]+)}, '\1_\2')
    .downcase
end
source_path() click to toggle source
# File lib/rubocop/cop/generator.rb, line 198
def source_path
  File.join(
    'lib',
    'rubocop',
    'cop',
    snake_case(badge.department.to_s),
    "#{snake_case(badge.cop_name.to_s)}.rb"
  )
end
spec_path() click to toggle source
# File lib/rubocop/cop/generator.rb, line 188
def spec_path
  File.join(
    'spec',
    'rubocop',
    'cop',
    snake_case(badge.department.to_s),
    "#{snake_case(badge.cop_name.to_s)}_spec.rb"
  )
end
write_unless_file_exists(path, contents) click to toggle source
# File lib/rubocop/cop/generator.rb, line 162
def write_unless_file_exists(path, contents)
  if File.exist?(path)
    warn "rake new_cop: #{path} already exists!"
    exit!
  end

  dir = File.dirname(path)
  FileUtils.mkdir_p(dir)

  File.write(path, contents)
  output.puts "[create] #{path}"
end