class RuboCop::Cop::Utils::FormatString

Parses {Kernel#sprintf} format strings.

Constants

DIGIT_DOLLAR
FLAG
NAME
NUMBER
NUMBER_ARG
PRECISION
SEQUENCE
TEMPLATE_NAME
TYPE
WIDTH

Public Class Methods

new(string) click to toggle source
# File lib/rubocop/cop/utils/format_string.rb, line 89
def initialize(string)
  @source = string
end

Public Instance Methods

format_sequences() click to toggle source
# File lib/rubocop/cop/utils/format_string.rb, line 93
def format_sequences
  @format_sequences ||= parse
end
max_digit_dollar_num() click to toggle source
# File lib/rubocop/cop/utils/format_string.rb, line 105
def max_digit_dollar_num
  format_sequences.map(&:max_digit_dollar_num).max
end
named_interpolation?() click to toggle source
# File lib/rubocop/cop/utils/format_string.rb, line 101
def named_interpolation?
  format_sequences.any?(&:name)
end
valid?() click to toggle source
# File lib/rubocop/cop/utils/format_string.rb, line 97
def valid?
  !mixed_formats?
end

Private Instance Methods

mixed_formats?() click to toggle source
# File lib/rubocop/cop/utils/format_string.rb, line 117
def mixed_formats?
  formats = format_sequences.reject(&:percent?).map do |seq|
    if seq.name
      :named
    elsif seq.max_digit_dollar_num
      :numbered
    else
      :unnumbered
    end
  end

  formats.uniq.size > 1
end
parse() click to toggle source
# File lib/rubocop/cop/utils/format_string.rb, line 111
def parse
  matches = []
  @source.scan(SEQUENCE) { matches << FormatSequence.new(Regexp.last_match) }
  matches
end