Module: RubbyCop::Cop::EnforceSuperclass

Included in:
Rails::ApplicationJob, Rails::ApplicationRecord
Defined in:
lib/rubbycop/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
# File 'lib/rubbycop/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 :#{base::SUPERCLASS} ...) (send (const nil :Class) :new #{base::BASE_PATTERN})]
  PATTERN
end

Instance Method Details

#autocorrect(node) ⇒ Object



29
30
31
32
33
# File 'lib/rubbycop/cop/mixin/enforce_superclass.rb', line 29

def autocorrect(node)
  lambda do |corrector|
    corrector.replace(node.source_range, self.class::SUPERCLASS)
  end
end

#on_class(node) ⇒ Object



17
18
19
20
21
# File 'lib/rubbycop/cop/mixin/enforce_superclass.rb', line 17

def on_class(node)
  class_definition(node) do
    add_offense(node.children[1], :expression, self.class::MSG)
  end
end

#on_send(node) ⇒ Object



23
24
25
26
27
# File 'lib/rubbycop/cop/mixin/enforce_superclass.rb', line 23

def on_send(node)
  class_new_definition(node) do
    add_offense(node.children.last, :expression, self.class::MSG)
  end
end