Class: RuboCop::Cop::Generator Private
- Inherits:
-
Object
- Object
- RuboCop::Cop::Generator
- 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.
NOTE: RDoc 5.1.0 or lower has the following issue. github.com/rubocop-hq/rubocop/issues/7043
The following ‘String#gsub` can be replaced with squiggly heredoc when RuboCop supports Ruby 2.5 or higher (RDoc 6.0 or higher).
<<-RUBY.gsub(/^ {8}/, '') # frozen_string_literal: true module RuboCop module Cop module %<department>s # TODO: Write cop description and example of bad / good code. For every # `SupportedStyle` and unique configuration, there needs to be examples. # Examples must have valid Ruby syntax. Do not use upticks. # # @example EnforcedStyle: bar (default) # # Description of the `bar` style. # # # bad # bad_bar_method # # # bad # bad_bar_method(args) # # # good # good_bar_method # # # good # good_bar_method(args) # # @example EnforcedStyle: foo # # Description of the `foo` style. # # # bad # bad_foo_method # # # bad # bad_foo_method(args) # # # good # good_foo_method # # # good # good_foo_method(args) # class %<cop_name>s < Base # TODO: Implement the cop in here. # # In many cases, you can use a node matcher for matching node pattern. # See https://github.com/rubocop-hq/rubocop-ast/blob/master/lib/rubocop/ast/node_pattern.rb # # For example MSG = 'Use `#good_method` instead of `#bad_method`.' def_node_matcher :bad_method?, <<~PATTERN (send nil? :bad_method ...) PATTERN def on_send(node) return unless bad_method?(node) add_offense(node) end end end end end RUBY
- 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.
<<~SPEC # frozen_string_literal: true RSpec.describe RuboCop::Cop::%<department>s::%<cop_name>s do subject(:cop) { described_class.new(config) } let(:config) { RuboCop::Config.new } # TODO: Write test code # # For example it 'registers an offense when using `#bad_method`' do expect_offense(<<~RUBY) bad_method ^^^^^^^^^^ Use `#good_method` instead of `#bad_method`. RUBY end it 'does not register an offense when using `#good_method`' do expect_no_offenses(<<~RUBY) good_method RUBY end end SPEC
- 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
-
#initialize(name, github_user, output: $stdout) ⇒ Generator
constructor
private
A new instance of Generator.
- #inject_config(config_file_path: 'config/default.yml', version_added: '<<next>>') ⇒ Object private
- #inject_require(root_file_path: 'lib/rubocop.rb') ⇒ Object private
- #todo ⇒ Object private
- #write_source ⇒ Object private
- #write_spec ⇒ Object private
Constructor Details
#initialize(name, github_user, 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.
111 112 113 114 115 116 117 118 |
# File 'lib/rubocop/cop/generator.rb', line 111 def initialize(name, github_user, output: $stdout) @badge = Badge.parse(name) @github_user = github_user @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.
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/rubocop/cop/generator.rb', line 135 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.
128 129 130 131 132 133 |
# File 'lib/rubocop/cop/generator.rb', line 128 def inject_require(root_file_path: 'lib/rubocop.rb') RequireFileInjector.new( source_path: source_path, root_file_path: root_file_path ).inject end |
#todo ⇒ 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.
148 149 150 151 152 153 154 155 156 |
# File 'lib/rubocop/cop/generator.rb', line 148 def todo <<~TODO Do 3 steps: 1. Add an entry to the "New features" section in CHANGELOG.md, e.g. "Add new `#{badge}` cop. ([@#{github_user}][])" 2. Modify the description of #{badge} in config/default.yml 3. Implement your new cop in the generated file! TODO end |
#write_source ⇒ 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.
120 121 122 |
# File 'lib/rubocop/cop/generator.rb', line 120 def write_source write_unless_file_exists(source_path, generated_source) end |
#write_spec ⇒ 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.
124 125 126 |
# File 'lib/rubocop/cop/generator.rb', line 124 def write_spec write_unless_file_exists(spec_path, generated_spec) end |