class RuboCop::Cop::Utils::FormatString
Parses {Kernel#sprintf} format strings.
Constants
- DIGIT_DOLLAR
-
Escaping the ‘#` in `INTERPOLATION` and `TEMPLATE_NAME` is necessary to avoid a bug in Ruby 3.2.0 See: bugs.ruby-lang.org/issues/19379
- FLAG
- INTERPOLATION
- NAME
- NUMBER
- NUMBER_ARG
- PRECISION
- SEQUENCE
- TEMPLATE_NAME
- TYPE
- WIDTH
Public Class Methods
Source
# File lib/rubocop/cop/utils/format_string.rb, line 94 def initialize(string) @source = string end
Public Instance Methods
Source
# File lib/rubocop/cop/utils/format_string.rb, line 98 def format_sequences @format_sequences ||= parse end
Source
# File lib/rubocop/cop/utils/format_string.rb, line 110 def max_digit_dollar_num format_sequences.map(&:max_digit_dollar_num).max end
Source
# File lib/rubocop/cop/utils/format_string.rb, line 106 def named_interpolation? format_sequences.any?(&:name) end
Source
# File lib/rubocop/cop/utils/format_string.rb, line 102 def valid? !mixed_formats? end
Private Instance Methods
Source
# File lib/rubocop/cop/utils/format_string.rb, line 122 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
Source
# File lib/rubocop/cop/utils/format_string.rb, line 116 def parse matches = [] @source.scan(SEQUENCE) { matches << FormatSequence.new(Regexp.last_match) } matches end