Class: RuboCop::Cop::Legion::Singleton::UseInstance
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Legion::Singleton::UseInstance
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/legion/singleton/use_instance.rb
Overview
Detects ‘.new` calls on singleton classes and auto-corrects to `.instance`.
Constant Summary collapse
- RESTRICT_ON_SEND =
%i[new].freeze
- MSG =
'Use `%<class_name>s.instance` instead of `.new` for singleton classes.'
Instance Method Summary collapse
Instance Method Details
#on_send(node) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rubocop/cop/legion/singleton/use_instance.rb', line 24 def on_send(node) receiver = node.receiver return unless receiver&.const_type? class_name = receiver.children.last.to_s return unless singleton_classes.include?(class_name) = format(MSG, class_name: class_name) add_offense(node, message: , severity: :error) do |corrector| # Replace the entire send expression: Receiver.new(args) -> Receiver.instance corrector.replace(node, "#{class_name}.instance") end end |