diff options
Diffstat (limited to 'rubocop/cop/migration/background_migration_missing_active_concern.rb')
-rw-r--r-- | rubocop/cop/migration/background_migration_missing_active_concern.rb | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/rubocop/cop/migration/background_migration_missing_active_concern.rb b/rubocop/cop/migration/background_migration_missing_active_concern.rb new file mode 100644 index 00000000000..417472bf512 --- /dev/null +++ b/rubocop/cop/migration/background_migration_missing_active_concern.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +require_relative '../../migration_helpers' + +module RuboCop + module Cop + module Migration + # Cop that checks `ActiveSupport::Concern` is included in EE batched background migrations + # if they define `scope_to`. + class BackgroundMigrationMissingActiveConcern < RuboCop::Cop::Base + include MigrationHelpers + + MSG = <<~MSG + Extend `ActiveSupport::Concern` in the EE background migration if it defines `scope_to`. + MSG + + def_node_matcher :prepended_block_uses_scope_to?, <<~PATTERN + (:block (:send nil? :prepended) (:args) `(:send nil? :scope_to ...)) + PATTERN + + def_node_matcher :scope_to?, <<~PATTERN + (:send nil? :scope_to ...) + PATTERN + + def_node_matcher :extend_activesupport_concern?, <<~PATTERN + (:send nil? :extend (:const (:const nil? :ActiveSupport) :Concern)) + PATTERN + + def on_block(node) + return unless in_ee_background_migration?(node) + return unless prepended_block_uses_scope_to?(node) + + return if module_extends_activesupport_concern?(node) + + node.descendants.each do |descendant| + next unless scope_to?(descendant) + + add_offense(descendant) + end + end + + private + + def module_extends_activesupport_concern?(node) + while node = node.parent + break if node.type == :module + end + + return false unless node + + node.descendants.any? do |descendant| + extend_activesupport_concern?(descendant) + end + end + end + end + end +end |