Class: RuboCop::Cop::Generator Private

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

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.

Defined Under Namespace

Classes: ConfigurationInjector, RequireFileInjector

Constant Summary collapse

SOURCE_TEMPLATE =

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.

"# frozen_string_literal: true\n\nmodule RuboCop\n  module Cop\n    module %<department>s\n      # TODO: Write cop description and example of bad / good code. For every\n      # `SupportedStyle` and unique configuration, there needs to be examples.\n      # Examples must have valid Ruby syntax. Do not use upticks.\n      #\n      # @safety\n      #   Delete this section if the cop is not unsafe (`Safe: false` or\n      #   `SafeAutoCorrect: false`), or use it to explain how the cop is\n      #   unsafe.\n      #\n      # @example EnforcedStyle: bar (default)\n      #   # Description of the `bar` style.\n      #\n      #   # bad\n      #   bad_bar_method\n      #\n      #   # bad\n      #   bad_bar_method(args)\n      #\n      #   # good\n      #   good_bar_method\n      #\n      #   # good\n      #   good_bar_method(args)\n      #\n      # @example EnforcedStyle: foo\n      #   # Description of the `foo` style.\n      #\n      #   # bad\n      #   bad_foo_method\n      #\n      #   # bad\n      #   bad_foo_method(args)\n      #\n      #   # good\n      #   good_foo_method\n      #\n      #   # good\n      #   good_foo_method(args)\n      #\n      class %<cop_name>s < Base\n        # TODO: Implement the cop in here.\n        #\n        # In many cases, you can use a node matcher for matching node pattern.\n        # See https://github.com/rubocop/rubocop-ast/blob/master/lib/rubocop/ast/node_pattern.rb\n        #\n        # For example\n        MSG = 'Use `#good_method` instead of `#bad_method`.'\n\n        # TODO: Don't call `on_send` unless the method name is in this list\n        # If you don't need `on_send` in the cop you created, remove it.\n        RESTRICT_ON_SEND = %%i[bad_method].freeze\n\n        # @!method bad_method?(node)\n        def_node_matcher :bad_method?, <<~PATTERN\n          (send nil? :bad_method ...)\n        PATTERN\n\n        def on_send(node)\n          return unless bad_method?(node)\n\n          add_offense(node)\n        end\n      end\n    end\n  end\nend\n"
SPEC_TEMPLATE =

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.

"# frozen_string_literal: true\n\nRSpec.describe RuboCop::Cop::%<department>s::%<cop_name>s, :config do\n  let(:config) { RuboCop::Config.new }\n\n  # TODO: Write test code\n  #\n  # For example\n  it 'registers an offense when using `#bad_method`' do\n    expect_offense(<<~RUBY)\n      bad_method\n      ^^^^^^^^^^ Use `#good_method` instead of `#bad_method`.\n    RUBY\n  end\n\n  it 'does not register an offense when using `#good_method`' do\n    expect_no_offenses(<<~RUBY)\n      good_method\n    RUBY\n  end\nend\n"
CONFIGURATION_ADDED_MESSAGE =

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.

'[modify] A configuration for the cop is added into ' \
'%<configuration_file_path>s.'

Instance Method Summary collapse

Constructor Details

#initialize(name, output: $stdout) ⇒ Generator

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 Generator.

Raises:

  • (ArgumentError)


113
114
115
116
117
118
119
# 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

Instance Method Details

#inject_config(config_file_path: 'config/default.yml', version_added: '<<next>>') ⇒ Object

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.



133
134
135
136
137
138
139
140
141
142
143
144
# 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') ⇒ Object

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.



129
130
131
# 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

#todoObject

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.



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rubocop/cop/generator.rb', line 146

def todo
  "    Do 4 steps:\n      1. Modify the description of \#{badge} in config/default.yml\n      2. Implement your new cop in the generated file!\n      3. Commit your new cop with a message such as\n         e.g. \"Add new `\#{badge}` cop\"\n      4. Run `bundle exec rake changelog:new` to generate a changelog entry\n         for your new cop.\n  TODO\nend\n"

#write_sourceObject

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.



121
122
123
# File 'lib/rubocop/cop/generator.rb', line 121

def write_source
  write_unless_file_exists(source_path, generated_source)
end

#write_specObject

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.



125
126
127
# File 'lib/rubocop/cop/generator.rb', line 125

def write_spec
  write_unless_file_exists(spec_path, generated_spec)
end