123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Utils::FormatString

Relationships & Source Files
Namespace Children
Classes:
Inherits: Object
Defined in: lib/rubocop/cop/utils/format_string.rb

Overview

Parses Kernel#sprintf format strings.

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(string) ⇒ FormatString

[ GitHub ]

  
# File 'lib/rubocop/cop/utils/format_string.rb', line 89

def initialize(string)
  @source = string
end

Instance Attribute Details

#mixed_formats?Boolean (readonly, private)

[ GitHub ]

  
# 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

#named_interpolation?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/utils/format_string.rb', line 101

def named_interpolation?
  format_sequences.any?(&:name)
end

#valid?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/utils/format_string.rb', line 97

def valid?
  !mixed_formats?
end

Instance Method Details

#format_sequences

[ GitHub ]

  
# File 'lib/rubocop/cop/utils/format_string.rb', line 93

def format_sequences
  @format_sequences ||= parse
end

#max_digit_dollar_num

[ GitHub ]

  
# File 'lib/rubocop/cop/utils/format_string.rb', line 105

def max_digit_dollar_num
  format_sequences.map(&:max_digit_dollar_num).max
end

#parse (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/utils/format_string.rb', line 111

def parse
  matches = []
  @source.scan(SEQUENCE) { matches << FormatSequence.new(Regexp.last_match) }
  matches
end