Class: RuboCop::Cop::Betterment::InternalsProtection

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/betterment/internals_protection.rb

Constant Summary collapse

MSG =
<<~END.gsub(/\s+/, " ")
  Internal constants may only be referenced from code within its containing module.
  Constants defined within a module's Internals submodule may only be referenced by code in that module,
  or nested classes and modules
  (e.g. MyModule::Internals::MyClass may only be referenced from code in MyModule or MyModule::MyPublicClass).
END

Instance Method Summary collapse

Instance Method Details

#association_with_class_name(node) ⇒ Object



15
16
17
# File 'lib/rubocop/cop/betterment/internals_protection.rb', line 15

def_node_matcher :association_with_class_name, <<-PATTERN
  (send nil? {:has_many :has_one :belongs_to} ... (hash <(pair (sym :class_name) ${str}) ...>))
PATTERN

#on_const(node) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/rubocop/cop/betterment/internals_protection.rb', line 24

def on_const(node)
  if node.children[1] == :Internals
    module_path = const_path(node)

    ensure_allowed_reference!(node, module_path)
  end
end

#on_send(node) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubocop/cop/betterment/internals_protection.rb', line 32

def on_send(node)
  class_name_node = association_with_class_name(node)
  return unless class_name_node

  full_path = string_path(class_name_node)
  internals_index = full_path.find_index(:Internals)
  if internals_index
    module_path = full_path.take(internals_index)
    ensure_allowed_reference!(class_name_node, module_path)
  end
end

#rspec_describe(node) ⇒ Object



20
21
22
# File 'lib/rubocop/cop/betterment/internals_protection.rb', line 20

def_node_matcher :rspec_describe, <<-PATTERN
  (block (send (const nil? :RSpec) :describe ${const | str} ...) ...)
PATTERN