Class: Rubocop::Cop::Style::FavorSprintf

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/style/favor_sprintf.rb

Overview

This cop checks for uses of String#%.

It cannot be implemented in a reliable manner for all cases, so only two scenarios are considered - if the first argument is a string literal and if the second argument is an array literal.

Constant Summary collapse

MSG =
'Favor sprintf over String#%.'

Constants inherited from Cop

Cop::OPERATOR_METHODS

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offences, #processed_source

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect?, #convention, #cop_config, cop_name, #cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, #message, non_rails, rails?, style?, #support_autocorrect?, #warning

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#on_send(node) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/rubocop/cop/style/favor_sprintf.rb', line 14

def on_send(node)
  receiver_node, method_name, *arg_nodes = *node

  if method_name == :% &&
      ([:str, :dstr].include?(receiver_node.type) ||
       arg_nodes[0].type == :array)
    convention(node, :selector)
  end
end