class RuboCop::Cop::Style::SymbolArray
This cop can check for array literals made up of symbols that are not using the %i() syntax.
Alternatively, it checks for symbol arrays using the %i() syntax on projects which do not want to use that syntax, perhaps because they support a version of Ruby lower than 2.0.
Constants
- ARRAY_MSG
- PERCENT_MSG
Public Instance Methods
on_array(node)
click to toggle source
# File lib/rubocop/cop/style/symbol_array.rb, line 19 def on_array(node) if bracketed_array_of?(:sym, node) return if comments_in_array?(node) return if symbols_contain_spaces?(node) style_detected(:brackets) add_offense(node, :expression, PERCENT_MSG) if style == :percent elsif node.loc.begin && node.loc.begin.source =~ /\A%[iI]/ style_detected(:percent) add_offense(node, :expression, ARRAY_MSG) if style == :brackets end end
validate_config()
click to toggle source
# File lib/rubocop/cop/style/symbol_array.rb, line 31 def validate_config if style == :percent && target_ruby_version < 2.0 raise ValidationError, 'The default `percent` style for the ' '`Style/SymbolArray` cop is only compatible' ' with Ruby 2.0 and up, but the target Ruby' " version for your project is 1.9.\nPlease " 'either disable this cop, configure it to ' 'use `array` style, or adjust the ' '`TargetRubyVersion` parameter in your ' 'configuration.' end end
Private Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/style/symbol_array.rb, line 62 def autocorrect(node) syms = node.children.map { |c| c.children[0].to_s } corrected = if style == :percent escape = syms.any? { |s| double_quotes_required?(s) } syms = syms.map { |s| escape_string(s) } if escape syms = syms.map { |s| s.gsub(/\)/, '\)') } if escape "%I(#{syms.join(' ')})" else "%i(#{syms.join(' ')})" end else syms = syms.map { |s| to_symbol_literal(s) } "[#{syms.join(', ')}]" end lambda do |corrector| corrector.replace(node.source_range, corrected) end end
comments_in_array?(node)
click to toggle source
# File lib/rubocop/cop/style/symbol_array.rb, line 46 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
escape_string(string)
click to toggle source
# File lib/rubocop/cop/style/symbol_array.rb, line 83 def escape_string(string) string.inspect[1..-2].tap { |s| s.gsub!(/\"/, '"') } end
symbols_contain_spaces?(node)
click to toggle source
# File lib/rubocop/cop/style/symbol_array.rb, line 55 def symbols_contain_spaces?(node) node.children.any? do |sym| content, = *sym content =~ / / end end