From b1212b6d0e45478aa07288a102695f32d9e19f4a Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 24 Nov 2020 06:47:51 +0900 Subject: [PATCH] Import `EnforceSuperclass` module --- lib/rubocop/cop/mixin/enforce_superclass.rb | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 lib/rubocop/cop/mixin/enforce_superclass.rb diff --git a/lib/rubocop/cop/mixin/enforce_superclass.rb b/lib/rubocop/cop/mixin/enforce_superclass.rb new file mode 100644 index 0000000000..1667e78312 --- /dev/null +++ b/lib/rubocop/cop/mixin/enforce_superclass.rb @@ -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