Class: RuboCop::Cop::Generator
- Inherits:
-
Object
- Object
- RuboCop::Cop::Generator
- Defined in:
- lib/rubocop/cop/generator.rb
Overview
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.
Constant Summary collapse
- SOURCE_TEMPLATE =
" # frozen_string_literal: true\n\n # TODO: when finished, run `rake generate_cops_documentation` to update the docs\n module RuboCop\n module Cop\n module %<department>s\n # TODO: Write cop description and example of bad / good code.\n #\n # @example\n # # bad\n # bad_method()\n #\n # # bad\n # bad_method(args)\n #\n # # good\n # good_method()\n #\n # # good\n # good_method(args)\n class %<cop_name>s < Cop\n # TODO: Implement the cop into here.\n #\n # In many cases, you can use a node matcher for matching node pattern.\n # See. https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/node_pattern.rb\n #\n # For example\n MSG = 'Message of %<cop_name>s'.freeze\n\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 add_offense(node, :expression)\n end\n end\n end\n end\n end\n".strip_indent
- SPEC_TEMPLATE =
" # frozen_string_literal: true\n\n describe RuboCop::Cop::%<department>s::%<cop_name>s do\n let(:config) { RuboCop::Config.new }\n subject(:cop) { described_class.new(config) }\n\n # TODO: Write test code\n #\n # For example\n it 'registers an offense for offending code' do\n inspect_source(cop, 'bad_method')\n expect(cop.offenses.size).to eq(1)\n expect(cop.messages)\n .to eq(['Message of %<cop_name>s'])\n end\n\n it 'accepts' do\n inspect_source(cop, 'good_method')\n expect(cop.offenses).to be_empty\n end\n end\n".strip_indent
Instance Method Summary collapse
-
#initialize(name) ⇒ Generator
constructor
A new instance of Generator.
- #todo ⇒ Object
- #write_source ⇒ Object
- #write_spec ⇒ Object
Constructor Details
#initialize(name) ⇒ Generator
Returns a new instance of Generator.
78 79 80 81 82 83 84 |
# File 'lib/rubocop/cop/generator.rb', line 78 def initialize(name) @badge = Badge.parse(name) return if badge.qualified? raise ArgumentError, 'Specify a cop name with Department/Name style' end |
Instance Method Details
#todo ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/rubocop/cop/generator.rb', line 94 def todo " created\n - \#{source_path}\n - \#{spec_path}\n\n Do 4 steps\n - Add an entry to `New feature` section in CHANGELOG.md\n - e.g. Add new `\#{badge.cop_name}` cop. ([@your_id][])\n - Add `require '\#{require_path}'` into lib/rubocop.rb\n - Add an entry into config/enabled.yml or config/disabled.yml\n - Implement a new cop to the generated file!\n TODO\nend\n".strip_indent |
#write_source ⇒ Object
86 87 88 |
# File 'lib/rubocop/cop/generator.rb', line 86 def write_source write_unless_file_exists(source_path, generated_source) end |
#write_spec ⇒ Object
90 91 92 |
# File 'lib/rubocop/cop/generator.rb', line 90 def write_spec write_unless_file_exists(spec_path, generated_spec) end |