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
-
DIGIT_DOLLAR =
# File 'lib/rubocop/cop/utils/format_string.rb', line 8/(\d+)\$/.freeze
-
FLAG =
# File 'lib/rubocop/cop/utils/format_string.rb', line 9/[ #0+-]|#{DIGIT_DOLLAR}/.freeze
-
NAME =
# File 'lib/rubocop/cop/utils/format_string.rb', line 15/<(?<name>\w+)>/.freeze
-
NUMBER =
# File 'lib/rubocop/cop/utils/format_string.rb', line 11/\d+|#{NUMBER_ARG}/.freeze
-
NUMBER_ARG =
# File 'lib/rubocop/cop/utils/format_string.rb', line 10/\*#{DIGIT_DOLLAR}?/.freeze
-
PRECISION =
# File 'lib/rubocop/cop/utils/format_string.rb', line 13/\.(?<precision>#{NUMBER})/.freeze
-
SEQUENCE =
# File 'lib/rubocop/cop/utils/format_string.rb', line 18/ % (?<type>%) | % (?<flags>#{FLAG}*) (?: (?: #{WIDTH}? #{PRECISION}? #{NAME}? | #{WIDTH}? #{NAME} #{PRECISION}? | #{NAME} (?<more_flags>#{FLAG}*) #{WIDTH}? #{PRECISION}? ) #{TYPE} | #{WIDTH}? #{PRECISION}? #{TEMPLATE_NAME} ) /x.freeze
-
TEMPLATE_NAME =
# File 'lib/rubocop/cop/utils/format_string.rb', line 16/\{(?<name>\w+)\}/.freeze
-
TYPE =
# File 'lib/rubocop/cop/utils/format_string.rb', line 14/(?<type>[bBdiouxXeEfgGaAcps])/.freeze
-
WIDTH =
# File 'lib/rubocop/cop/utils/format_string.rb', line 12/(?<width>#{NUMBER})/.freeze
Class Method Summary
- .new(string) ⇒ FormatString constructor
Instance Attribute Summary
- #named_interpolation? ⇒ Boolean readonly
- #valid? ⇒ Boolean readonly
- #mixed_formats? ⇒ Boolean readonly private
Instance Method Summary
Constructor Details
.new(string) ⇒ FormatString
# 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