Class: RuboCop::Cop::Greenroom::CandidateIsPrivate

Inherits:
Base
  • Object
show all
Includes:
VisibilityHelp
Defined in:
lib/rubocop/cop/greenroom/candidate_is_private.rb

Overview

Requires the candidate method that try calls to be private.

A candidate is scaffolding. Once the experiment wins, a later change folds the candidate into the method body and deletes the scaffolding. A public candidate invites a caller from outside the class, and that deletion then breaks the caller. A private one keeps the deletion inside the class that owns it.

The cop answers only when this file shows the answer: the try body must be one call to the object itself, and the definition must sit in the same class. A definition in another file leaves this cop quiet, because the gate that reads the whole change decides that case.

Constant Summary collapse

MSG =
"A candidate method must be private."

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rubocop/cop/greenroom/candidate_is_private.rb', line 23

def on_block(node)
  return unless science_try_block?(node)

  candidate_call = node.body
  return unless candidate_call&.send_type?
  return unless candidate_call.receiver.nil? || candidate_call.receiver.self_type?

  class_node = node.each_ancestor.find(&:class_type?)
  return unless class_node

  definition = candidate_definition(class_node, candidate_call.method_name)
  return unless definition
  return if node_visibility(definition) == :private

  add_offense(node)
end