123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::ArrayMinSize

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/rubocop/cop/mixin/array_min_size.rb

Overview

Handles the MinSize configuration option for array-based cops Style/SymbolArray and Style/WordArray, which check for use of the relevant percent literal syntax such as %i[…​] and %w[…​]

Instance Method Summary

Instance Method Details

#array_style_detected(style, ary_size) (private)

rubocop:todo Metrics/AbcSize

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/array_min_size.rb', line 19

def array_style_detected(style, ary_size) # rubocop:todo Metrics/AbcSize
  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

#below_array_length?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/array_min_size.rb', line 11

def below_array_length?(node)
  node.values.length < min_size_config
end

#largest_brackets_size(style, ary_size) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/array_min_size.rb', line 38

def largest_brackets_size(style, ary_size)
  self.class.largest_brackets ||= -Float::INFINITY

  if style == :brackets && ary_size > self.class.largest_brackets
    self.class.largest_brackets = ary_size
  end

  self.class.largest_brackets
end

#min_size_config (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/array_min_size.rb', line 15

def min_size_config
  cop_config['MinSize']
end

#smallest_percent_size(style, ary_size) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/array_min_size.rb', line 48

def smallest_percent_size(style, ary_size)
  @smallest_percent ||= Float::INFINITY

  @smallest_percent = ary_size if style == :percent && ary_size < @smallest_percent

  @smallest_percent
end