Class: RuboCop::Cop::Legion::Framework::ModuleFunctionPrivate

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/legion/framework/module_function_private.rb

Overview

Detects modules that call both bare ‘module_function` and bare `private`. Using `private` after `module_function` resets method visibility to instance-only, which is almost never intended.

Only bare-word forms (no arguments) are flagged. Targeted calls like ‘module_function :foo` or `private :bar` are not flagged.

Examples:

# bad
module Helpers
  module_function

  def foo; end

  private

  def bar; end
end

# good
module Helpers
  module_function

  def foo; end
end

Constant Summary collapse

MSG =
'`private` after `module_function` resets visibility to instance-only. ' \
'Do not use both in the same module.'
SEVERITY =
:convention

Instance Method Summary collapse

Instance Method Details

#on_module(node) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/rubocop/cop/legion/framework/module_function_private.rb', line 37

def on_module(node)
  body = node.body
  return unless body

  bare_calls = collect_bare_visibility_calls(body)
  private_node = bare_calls[:private]
  return unless bare_calls[:module_function] && private_node

  add_offense(private_node, severity: SEVERITY)
end