Class: RuboCop::Cop::Rubomatic::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/cop/rubomatic/generator.rb,
lib/rubocop/cop/rubomatic/generator/cop_readme_injector.rb,
lib/rubocop/cop/rubomatic/generator/dept_readme_injector.rb

Defined Under Namespace

Classes: CopReadmeInjector, DeptReadmeInjector

Constant Summary collapse

COP_DOC =
<<~RUBY
  # 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.
  #
  # @safety
  #   Delete this section if the cop is not unsafe (`Safe: false` or
  #   `SafeAutoCorrect: false`), or use it to explain how the cop is
  #   unsafe.
  #
  # @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)
  #
RUBY
SOURCE_TEMPLATE =
<<~RUBY
  # frozen_string_literal: true

  module RuboCop
    module Cop
      module %{department}
        class %{cop_name} < 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/rubocop-ast/blob/master/lib/rubocop/ast/node_pattern.rb
          #
          # For example
          MSG = 'Use `#good_method` instead of `#bad_method`.'

          # TODO: Don't call `on_send` unless the method name is in this list
          # If you don't need `on_send` in the cop you created, remove it.
          RESTRICT_ON_SEND = %%i[bad_method].freeze

          # @!method bad_method?(node)
          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
README_ADDED_MESSAGE =
'[modify] A link for the %{dept_vs_cop} has been added into %{readme_file_path}.'
DEPT_README_TEMPLATE =
<<~ADOC
  = %{department}

  Describe the department here

  == Cops

ADOC
COP_README_TEMPLATE =
<<~ADOC
  = ``%{department}/%{cop_name}``

  == Description

  Add a description here

  == Examples

  [source,ruby]
  ----
  # Bad
  # Add a bad example here

  # Good
  # Add a good example here
  ----

  == Configurable Attributes

  |===
  |Name |Default value |Configurable values

  |Max
  |120
  |Integer

  |===

  == References

  https://github.com/BrandsInsurance/expert-chainsaw/issues
ADOC

Instance Method Summary collapse

Constructor Details

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

:nodoc:



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rubocop/cop/rubomatic/generator.rb', line 131

def initialize(name, output: $stdout)
  name = ['Rubomatic', name].join('/') unless name.start_with?('Rubomatic/')

  unless name.count('/') == 2
    raise(
      [
        'You must provide a single department under Rubomatic i.e. Rubomatic/Department/CopName',
        'or Department/CopName'
      ].join(' ')
    )
  end

  @base_gen = RuboCop::Cop::Generator.new(name, output: output)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing*

Calls methods in the base class

Returns:

  • (*)


150
151
152
# File 'lib/rubocop/cop/rubomatic/generator.rb', line 150

def method_missing(...)
  @base_gen.__send__(...)
end

Instance Method Details

#inject_cop_readme(readme_file_path: dept_docs_path) ⇒ void

This method returns an undefined value.

Injects the new cop readme link into the department readme Modified version of ‘inject_config` from RuboCop::Cop::Generator



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/rubocop/cop/rubomatic/generator.rb', line 203

def inject_cop_readme(readme_file_path: dept_docs_path)
  # Add this cop to the dept readme
  injector = CopReadmeInjector.new(
    readme_file_path: readme_file_path,
    badge: badge,
    department: department
  )

  injector.inject_string do
    output.puts(format(README_ADDED_MESSAGE, readme_file_path: readme_file_path, dept_vs_cop: 'cop'))
  end
end

#inject_dept_readme(readme_file_path: 'README.adoc') ⇒ void

This method returns an undefined value.

Injects the, possibly new, department readme link into the base readme Modified version of ‘inject_config` from RuboCop::Cop::Generator



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/rubocop/cop/rubomatic/generator.rb', line 185

def inject_dept_readme(readme_file_path: 'README.adoc')
  # Add this dept to base readme if not already there
  injector = DeptReadmeInjector.new(
    readme_file_path: readme_file_path,
    badge: badge,
    department: department
  )

  injector.inject_string do
    output.puts(format(README_ADDED_MESSAGE, readme_file_path: readme_file_path, dept_vs_cop: 'department'))
  end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

‘self` responds to `method_name` if `@base_gen` does

Returns:

  • (Boolean)


156
157
158
# File 'lib/rubocop/cop/rubomatic/generator.rb', line 156

def respond_to_missing?(method_name, include_private = false)
  @base_gen.respond_to?(method_name, include_private)
end

#write_cop_readmevoid

This method returns an undefined value.

Creates the cop readme if it doesn’t exist Modified version of ‘wirte_source` from RuboCop::Cop::Generator



176
177
178
# File 'lib/rubocop/cop/rubomatic/generator.rb', line 176

def write_cop_readme
  write_unless_file_exists(docs_path, generated_cop_docs)
end

#write_dept_readmevoid

This method returns an undefined value.

Creates the department readme if it doesn’t exist Modified version of ‘wirte_source` from RuboCop::Cop::Generator



165
166
167
168
169
# File 'lib/rubocop/cop/rubomatic/generator.rb', line 165

def write_dept_readme
  return if File.exist?(dept_docs_path)

  write_unless_file_exists(dept_docs_path, generated_dept_docs)
end