Module: RuboCop::Cop::EnforceSuperclass

Included in:
Rails::ApplicationController, Rails::ApplicationJob, Rails::ApplicationMailer, Rails::ApplicationRecord
Defined in:
lib/rubocop/cop/mixin/enforce_superclass.rb

Overview

Common functionality for enforcing a specific superclass.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rubocop/cop/mixin/enforce_superclass.rb', line 7

def self.included(base)
  base.def_node_matcher :class_definition, <<~PATTERN
    (class (const _ !:#{base::SUPERCLASS}) #{base::BASE_PATTERN} ...)
  PATTERN

  base.def_node_matcher :class_new_definition, <<~PATTERN
    [!^(casgn {nil? cbase} :#{base::SUPERCLASS} ...)
     !^^(casgn {nil? cbase} :#{base::SUPERCLASS} (block ...))
     (send (const {nil? cbase} :Class) :new #{base::BASE_PATTERN})]
  PATTERN
end

Instance Method Details

#on_class(node) ⇒ Object



19
20
21
22
23
# File 'lib/rubocop/cop/mixin/enforce_superclass.rb', line 19

def on_class(node)
  class_definition(node) do
    register_offense(node.children[1])
  end
end

#on_send(node) ⇒ Object



25
26
27
28
29
# File 'lib/rubocop/cop/mixin/enforce_superclass.rb', line 25

def on_send(node)
  class_new_definition(node) do
    register_offense(node.children.last)
  end
end