Class: Spoom::Deadcode::Plugins::ActiveRecord

Inherits:
Base
  • Object
show all
Defined in:
lib/spoom/deadcode/plugins/active_record.rb

Constant Summary collapse

CALLBACKS =
[
  "after_commit",
  "after_create_commit",
  "after_create",
  "after_destroy_commit",
  "after_destroy",
  "after_find",
  "after_initialize",
  "after_rollback",
  "after_save_commit",
  "after_save",
  "after_touch",
  "after_update_commit",
  "after_update",
  "after_validation",
  "around_create",
  "around_destroy",
  "around_save",
  "around_update",
  "before_create",
  "before_destroy",
  "before_save",
  "before_update",
  "before_validation",
].freeze
CALLBACK_CONDITIONS =

: Array

[
  "if",
  "unless",
].freeze
CRUD_METHODS =

: Array

[
  "assign_attributes",
  "create",
  "create!",
  "insert",
  "insert!",
  "new",
  "update",
  "update!",
  "upsert",
].freeze
ARRAY_METHODS =

: Array

[
  "insert_all",
  "insert_all!",
  "upsert_all",
].freeze

Instance Attribute Summary

Attributes inherited from Base

#index

Instance Method Summary collapse

Methods inherited from Base

ignore_classes_inheriting_from, ignore_classes_named, ignore_constants_named, ignore_methods_named, ignore_modules_named, #initialize, #internal_on_define_accessor, #internal_on_define_class, #internal_on_define_constant, #internal_on_define_method, #internal_on_define_module, #on_define_accessor, #on_define_class, #on_define_constant, #on_define_method, #on_define_module

Constructor Details

This class inherits a constructor from Spoom::Deadcode::Plugins::Base

Instance Method Details

#on_send(send) ⇒ Object

: (Send send) -> void



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/spoom/deadcode/plugins/active_record.rb', line 69

def on_send(send)
  if send.recv.nil? && CALLBACKS.include?(send.name)
    # Process direct symbol arguments
    send.each_arg(Prism::SymbolNode) do |arg|
      @index.reference_method(arg.unescaped, send.location)
    end

    # Process hash arguments for conditions like if: :method_name
    send.each_arg_assoc do |key, value|
      key = key.slice.delete_suffix(":")

      case key
      when *CALLBACK_CONDITIONS
        if value&.is_a?(Prism::SymbolNode)
          @index.reference_method(value.unescaped, send.location)
        end
      end
    end

    return
  end

  return unless send.recv

  case send.name
  when *CRUD_METHODS
    send.each_arg_assoc do |key, _value|
      key = key.slice.delete_suffix(":")
      @index.reference_method("#{key}=", send.location)
    end
  when *ARRAY_METHODS
    send.each_arg(Prism::ArrayNode) do |arg|
      arg.elements.each do |part|
        next unless part.is_a?(Prism::HashNode)

        part.elements.each do |assoc|
          next unless assoc.is_a?(Prism::AssocNode)

          key = assoc.key.slice.delete_suffix(":")
          @index.reference_method("#{key}=", send.location)
        end
      end
    end
  end
end