lib/rubocop/cop/style/word_array.rb in rubocop-0.42.0 vs lib/rubocop/cop/style/word_array.rb in rubocop-0.43.0

- old
+ new

@@ -23,23 +23,14 @@ check_percent(node) end end def autocorrect(node) - words = node.children if style == :percent - escape = words.any? { |w| needs_escaping?(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 + correct_percent(node) else - words = words.map { |w| to_string_literal(w.children[0]) } - lambda do |corrector| - corrector.replace(node.source_range, "[#{words.join(', ')}]") - end + correct_bracketed(node) end end private @@ -48,13 +39,13 @@ 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 + return unless style == :percent && array_elems.size >= min_size + + add_offense(node, :expression, PERCENT_MSG) end def check_percent(node) array_elems = node.children @@ -92,16 +83,35 @@ def word_regex cop_config['WordRegex'] end + def correct_percent(node) + words = node.children + escape = words.any? { |w| needs_escaping?(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 + end + + def correct_bracketed(node) + words = node.children.map { |w| to_string_literal(w.children[0]) } + + lambda do |corrector| + corrector.replace(node.source_range, "[#{words.join(', ')}]") + end + end + 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 = node.children.first content = escape ? escape_string(content) : content content.gsub!(/\)/, '\\)') line_breaks + content end.join(' ') end