Module: RuboCop::Cop::ConfigurableEnforcedStyle

Overview

Handles EnforcedStyle configuration parameters.

Instance Method Summary collapse

Instance Method Details

#alternative_styleObject


86
87
88
89
90
91
92
93
# File 'lib/rubocop/cop/mixin/configurable_enforced_style.rb', line 86

def alternative_style
  a = cop_config['SupportedStyles'].map(&:to_sym)
  if a.size != 2
    fail 'alternative_style can only be used when there are exactly ' \
         '2 SupportedStyles'
  end
  style == a.first ? a.last : a.first
end

#ambiguous_style_detected(*possibilities) ⇒ Object


19
20
21
# File 'lib/rubocop/cop/mixin/configurable_enforced_style.rb', line 19

def ambiguous_style_detected(*possibilities)
  style_detected(possibilities)
end

#correct_style_detectedObject


11
12
13
# File 'lib/rubocop/cop/mixin/configurable_enforced_style.rb', line 11

def correct_style_detected
  style_detected(style)
end

#detected_styleObject


54
55
56
# File 'lib/rubocop/cop/mixin/configurable_enforced_style.rb', line 54

def detected_style
  config_to_allow_offenses[parameter_name]
end

#detected_style=(style) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rubocop/cop/mixin/configurable_enforced_style.rb', line 58

def detected_style=(style)
  if style.nil?
    no_acceptable_style!
  elsif style.is_a?(Array)
    if style.empty?
      no_acceptable_style!
    elsif style.one?
      config_to_allow_offenses[parameter_name] = style[0]
    else
      config_to_allow_offenses[parameter_name] = style
    end
  else
    config_to_allow_offenses[parameter_name] = style
  end
end

#no_acceptable_style!Object Also known as: conflicting_styles_detected, unrecognized_style_detected


50
51
52
# File 'lib/rubocop/cop/mixin/configurable_enforced_style.rb', line 50

def no_acceptable_style!
  self.config_to_allow_offenses = { 'Enabled' => false }
end

#no_acceptable_style?Boolean

Returns:

  • (Boolean)

46
47
48
# File 'lib/rubocop/cop/mixin/configurable_enforced_style.rb', line 46

def no_acceptable_style?
  config_to_allow_offenses['Enabled'] == false
end

#opposite_style_detectedObject


7
8
9
# File 'lib/rubocop/cop/mixin/configurable_enforced_style.rb', line 7

def opposite_style_detected
  style_detected(alternative_style)
end

#parameter_nameObject


95
96
97
# File 'lib/rubocop/cop/mixin/configurable_enforced_style.rb', line 95

def parameter_name
  'EnforcedStyle'
end

#styleObject


77
78
79
80
81
82
83
84
# File 'lib/rubocop/cop/mixin/configurable_enforced_style.rb', line 77

def style
  s = cop_config[parameter_name]
  if cop_config['SupportedStyles'].include?(s)
    s.to_sym
  else
    fail "Unknown style #{s} selected!"
  end
end

#style_detected(detected) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rubocop/cop/mixin/configurable_enforced_style.rb', line 23

def style_detected(detected)
  # `detected` can be a single style, or an Array of possible styles
  # (if there is more than one which matches the observed code)

  return if no_acceptable_style?

  if detected.is_a?(Array)
    detected.map!(&:to_s)
  else
    detected = detected.to_s
  end

  if !detected_style # we haven't observed any specific style yet
    self.detected_style = detected
  elsif detected_style.is_a?(Array)
    self.detected_style &= [*detected]
  elsif detected.is_a?(Array)
    no_acceptable_style! unless detected.include?(detected_style)
  else
    no_acceptable_style! unless detected_style == detected
  end
end

#unexpected_style_detected(unexpected) ⇒ Object


15
16
17
# File 'lib/rubocop/cop/mixin/configurable_enforced_style.rb', line 15

def unexpected_style_detected(unexpected)
  style_detected(unexpected)
end