class RuboCop::Cop::Style::WordArray
This cop can check for array literals made up of word-like strings, that are not using the %w() syntax.
Alternatively, it can check for uses of the %w() syntax, in projects which do not want to include that syntax.
Constants
- ARRAY_MSG
- PERCENT_MSG
- QUESTION_MARK_SIZE
Public Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/style/word_array.rb, line 26 def autocorrect(node) words = node.children if style == :percent escape = words.any? { |w| double_quotes_required?(w.children[0]) } char = escape ? 'W' : 'w' contents = autocorrect_words(words, escape, node.loc.line) lambda do |corrector| corrector.replace(node.source_range, "%#{char}(#{contents})") end else words = words.map { |w| to_string_literal(w.children[0]) } lambda do |corrector| corrector.replace(node.source_range, "[#{words.join(', ')}]") end end end
on_array(node)
click to toggle source
# File lib/rubocop/cop/style/word_array.rb, line 18 def on_array(node) if bracketed_array_of?(:str, node) check_bracketed(node) elsif percent_syntax?(node) check_percent(node) end end
Private Instance Methods
autocorrect_words(word_nodes, escape, base_line_number)
click to toggle source
# File lib/rubocop/cop/style/word_array.rb, line 96 def autocorrect_words(word_nodes, escape, base_line_number) previous_node_line_number = base_line_number word_nodes.map do |node| number_of_line_breaks = node.loc.line - previous_node_line_number line_breaks = "\n" * number_of_line_breaks previous_node_line_number = node.loc.line content = node.children[0] content = escape ? escape_string(content) : content content.gsub!(/\)/, '\)') line_breaks + content end.join(' ') end
check_bracketed(node)
click to toggle source
# File lib/rubocop/cop/style/word_array.rb, line 45 def check_bracketed(node) array_elems = node.children return if complex_content?(array_elems) || comments_in_array?(node) style_detected(:brackets, array_elems.size) if style == :percent && array_elems.size >= min_size add_offense(node, :expression, PERCENT_MSG) end end
check_percent(node)
click to toggle source
# File lib/rubocop/cop/style/word_array.rb, line 57 def check_percent(node) array_elems = node.children style_detected(:percent, array_elems.size) add_offense(node, :expression, ARRAY_MSG) if style == :brackets end
comments_in_array?(node)
click to toggle source
# File lib/rubocop/cop/style/word_array.rb, line 68 def comments_in_array?(node) comments = processed_source.comments array_range = node.source_range.to_a comments.any? do |comment| !(comment.loc.expression.to_a & array_range).empty? end end
complex_content?(strings)
click to toggle source
# File lib/rubocop/cop/style/word_array.rb, line 77 def complex_content?(strings) strings.any? do |s| string = s.str_content !string.valid_encoding? || string !~ word_regex || string =~ / / end end
escape_string(string)
click to toggle source
# File lib/rubocop/cop/style/word_array.rb, line 109 def escape_string(string) string.inspect[1..-2].tap { |s| s.gsub!(/\"/, '"') } end
largest_brackets_size(style, ary_size)
click to toggle source
# File lib/rubocop/cop/style/word_array.rb, line 132 def largest_brackets_size(style, ary_size) @largest_brackets ||= -Float::INFINITY if style == :brackets && ary_size > @largest_brackets @largest_brackets = ary_size end @largest_brackets end
min_size()
click to toggle source
# File lib/rubocop/cop/style/word_array.rb, line 88 def min_size cop_config['MinSize'] end
percent_syntax?(node)
click to toggle source
# File lib/rubocop/cop/style/word_array.rb, line 64 def percent_syntax?(node) node.loc.begin && node.loc.begin.source =~ /\A%[wW]/ end
smallest_percent_size(style, ary_size)
click to toggle source
# File lib/rubocop/cop/style/word_array.rb, line 142 def smallest_percent_size(style, ary_size) @smallest_percent ||= Float::INFINITY if style == :percent && ary_size < @smallest_percent @smallest_percent = ary_size end @smallest_percent end
style()
click to toggle source
# File lib/rubocop/cop/style/word_array.rb, line 84 def style cop_config['EnforcedStyle'].to_sym end
style_detected(style, ary_size)
click to toggle source
# File lib/rubocop/cop/style/word_array.rb, line 113 def style_detected(style, ary_size) cfg = config_to_allow_offenses return if cfg['Enabled'] == false largest_brackets = largest_brackets_size(style, ary_size) smallest_percent = smallest_percent_size(style, ary_size) if cfg['EnforcedStyle'] == style.to_s # do nothing elsif cfg['EnforcedStyle'].nil? cfg['EnforcedStyle'] = style.to_s elsif smallest_percent <= largest_brackets self.config_to_allow_offenses = { 'Enabled' => false } else cfg['EnforcedStyle'] = 'percent' cfg['MinSize'] = largest_brackets + 1 end end
word_regex()
click to toggle source
# File lib/rubocop/cop/style/word_array.rb, line 92 def word_regex cop_config['WordRegex'] end