class RuboCop::Cop::Style::FormatString


puts ‘%10s’ % ‘hoge’
# good
puts sprintf(‘%10s’, ‘hoge’)
puts format(‘%10s’, ‘hoge’)
# bad
@example EnforcedStyle: percent
puts sprintf(‘%10s’, ‘hoge’)
# good
puts ‘%10s’ % ‘hoge’
puts format(‘%10s’, ‘hoge’)
# bad
@example EnforcedStyle: sprintf
puts format(‘%10s’, ‘hoge’)
# good
puts ‘%10s’ % ‘hoge’
puts sprintf(‘%10s’, ‘hoge’)
# bad
@example EnforcedStyle: format (default)
argument is an array literal.
if the first argument is a string literal and if the second
manner for all cases, so only two scenarios are considered -
The detection of String#% cannot be implemented in a reliable
Valid options include Kernel#format, Kernel#sprintf and String#%.
This cop enforces the use of a single string formatting utility.

def autocorrect(node)

def autocorrect(node)
  return if variable_argument?(node)
  lambda do |corrector|
    case node.method_name
    when :%
      autocorrect_from_percent(corrector, node)
    when :format, :sprintf
      case style
      when :percent
        autocorrect_to_percent(corrector, node)
      when :format, :sprintf
        corrector.replace(node.loc.selector, style.to_s)
      end
    end
  end
end

def autocorrect_from_percent(corrector, node)

def autocorrect_from_percent(corrector, node)
  percent_rhs = node.first_argument
  args = case percent_rhs.type
         when :array, :hash
           percent_rhs.children.map(&:source).join(', ')
         else
           percent_rhs.source
         end
  corrected = "#{style}(#{node.receiver.source}, #{args})"
  corrector.replace(node.loc.expression, corrected)
end

def autocorrect_to_percent(corrector, node)

def autocorrect_to_percent(corrector, node)
  format_arg, *param_args = node.arguments
  format = format_arg.source
  args = if param_args.one?
           arg = param_args.last
           arg.hash_type? ? "{ #{arg.source} }" : arg.source
         else
           "[#{param_args.map(&:source).join(', ')}]"
         end
  corrector.replace(node.loc.expression, "#{format} % #{args}")
end

def message(detected_style)

def message(detected_style)
  format(MSG,
         prefer: method_name(style),
         current: method_name(detected_style))
end

def method_name(style_name)

def method_name(style_name)
  style_name == :percent ? 'String#%' : style_name
end

def on_send(node)

def on_send(node)
  formatter(node) do |selector|
    detected_style = selector == :% ? :percent : selector
    return if detected_style == style
    add_offense(node, location: :selector,
                      message: message(detected_style))
  end
end