Skip to content

Commit

Permalink
Import EnforceSuperclass module
Browse files Browse the repository at this point in the history
  • Loading branch information
koic committed Nov 23, 2020
1 parent 9808efd commit b1212b6
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/rubocop/cop/mixin/enforce_superclass.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module RuboCop
module Cop
# Common functionality for enforcing a specific superclass.
module EnforceSuperclass
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

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

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

0 comments on commit b1212b6

Please sign in to comment.