class RubbyCop::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.
Configuration option: MinSize If set, arrays with fewer elements than this value will not trigger the cop. For
example, a `MinSize of `3` will not enforce a style on an array of 2 or fewer elements.
@example
EnforcedStyle: percent (default) # good %i[foo bar baz] # bad [:foo, :bar, :baz]
@example
EnforcedStyle: brackets # good [:foo, :bar, :baz] # bad %i[foo bar baz]
Constants
- ARRAY_MSG
- PERCENT_MSG
Attributes
largest_brackets[RW]
Public Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubbycop/cop/style/symbol_array.rb, line 59 def autocorrect(node) if style == :percent correct_percent(node, 'i') else correct_bracketed(node) end end
on_array(node)
click to toggle source
# File lib/rubbycop/cop/style/symbol_array.rb, line 51 def on_array(node) if bracketed_array_of?(:sym, node) check_bracketed_array(node) elsif node.percent_literal?(:symbol) check_percent_array(node) end end
Private Instance Methods
check_bracketed_array(node)
click to toggle source
# File lib/rubbycop/cop/style/symbol_array.rb, line 69 def check_bracketed_array(node) return if comments_in_array?(node) || symbols_contain_spaces?(node) || below_array_length?(node) array_style_detected(:brackets, node.values.size) add_offense(node, :expression, PERCENT_MSG) if style == :percent end
check_percent_array(node)
click to toggle source
# File lib/rubbycop/cop/style/symbol_array.rb, line 78 def check_percent_array(node) array_style_detected(:percent, node.values.size) add_offense(node, :expression, ARRAY_MSG) if style == :brackets end
comments_in_array?(node)
click to toggle source
# File lib/rubbycop/cop/style/symbol_array.rb, line 83 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
correct_bracketed(node)
click to toggle source
# File lib/rubbycop/cop/style/symbol_array.rb, line 99 def correct_bracketed(node) syms = node.children.map { |c| to_symbol_literal(c.children[0].to_s) } lambda do |corrector| corrector.replace(node.source_range, "[#{syms.join(', ')}]") end end
symbols_contain_spaces?(node)
click to toggle source
# File lib/rubbycop/cop/style/symbol_array.rb, line 92 def symbols_contain_spaces?(node) node.children.any? do |sym| content, = *sym content =~ / / end end