Module: RuboCop::Cop::ConfigurableEnforcedStyle

Included in:
ConfigurableNaming, EndKeywordAlignment, Lint::BlockAlignment, Lint::InheritException, MultilineLiteralBraceLayout, Rails::ActionFilter, Rails::Date, Rails::Exit, Rails::RequestReferer, Rails::TimeZone, Style::AccessModifierIndentation, Style::Alias, Style::AndOr, Style::BarePercentLiterals, Style::BlockDelimiters, Style::BracesAroundHashParameters, Style::CaseIndentation, Style::ClassAndModuleChildren, Style::ClassCheck, Style::CommandLiteral, Style::ConditionalAssignment, Style::DotPosition, Style::EmptyElse, Style::EmptyLinesAroundBody, Style::Encoding, Style::FirstParameterIndentation, Style::For, Style::FormatString, Style::FrozenStringLiteralComment, Style::GuardClause, Style::HashSyntax, Style::IndentArray, Style::IndentHash, Style::IndentationConsistency, Style::Lambda, Style::LambdaCall, Style::MethodDefParentheses, Style::MissingElse, Style::ModuleFunction, Style::MultilineAssignmentLayout, Style::MultilineMethodCallIndentation, Style::MultilineOperationIndentation, Style::Next, Style::NumericPredicate, Style::PercentQLiterals, Style::RaiseArgs, Style::RegexpLiteral, Style::SignalException, Style::SpaceAroundBlockParameters, Style::SpaceAroundEqualsInParameterDefault, Style::SpaceBeforeBlockBraces, Style::SpaceInsideBlockBraces, Style::SpaceInsideHashLiteralBraces, Style::SpaceInsideStringInterpolation, Style::SpecialGlobalVars, Style::StabbyLambdaParentheses, Style::StringLiterals, Style::StringLiteralsInInterpolation, Style::SymbolArray, Style::TernaryParentheses, Style::TrailingBlankLines, TrailingComma
Defined in:
lib/rubocop/cop/mixin/configurable_enforced_style.rb

Overview

Handles ‘EnforcedStyle` configuration parameters.

Instance Method Summary collapse

Instance Method Details

#alternative_styleObject



75
76
77
78
79
80
81
# File 'lib/rubocop/cop/mixin/configurable_enforced_style.rb', line 75

def alternative_style
  if supported_styles.size != 2
    raise 'alternative_style can only be used when there are exactly ' \
         '2 SupportedStyles'
  end
  (supported_styles - [style]).first
end

#ambiguous_style_detected(*possibilities) ⇒ Object



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

def ambiguous_style_detected(*possibilities)
  style_detected(possibilities)
end

#correct_style_detectedObject



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

def correct_style_detected
  style_detected(style)
end

#detected_styleObject



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

def detected_style
  Formatter::DisabledConfigFormatter.detected_styles[cop_name] ||= nil
end

#detected_style=(style) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/rubocop/cop/mixin/configurable_enforced_style.rb', line 53

def detected_style=(style)
  Formatter::DisabledConfigFormatter.detected_styles[cop_name] = style

  return no_acceptable_style! if style.nil?
  return no_acceptable_style! if style.empty?

  config_to_allow_offenses[parameter_name] = style.first
end

#no_acceptable_style!Object Also known as: conflicting_styles_detected, unrecognized_style_detected



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

def no_acceptable_style!
  self.config_to_allow_offenses = { 'Enabled' => false }
  Formatter::DisabledConfigFormatter.detected_styles[cop_name] = []
end

#no_acceptable_style?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/rubocop/cop/mixin/configurable_enforced_style.rb', line 40

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

#opposite_style_detectedObject



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

def opposite_style_detected
  style_detected(alternative_style)
end

#parameter_nameObject



87
88
89
# File 'lib/rubocop/cop/mixin/configurable_enforced_style.rb', line 87

def parameter_name
  'EnforcedStyle'
end

#styleObject



65
66
67
68
69
70
71
72
73
# File 'lib/rubocop/cop/mixin/configurable_enforced_style.rb', line 65

def style
  @enforced_style ||= begin
    s = cop_config[parameter_name].to_sym
    unless supported_styles.include?(s)
      raise "Unknown style #{s} selected!"
    end
    s
  end
end

#style_detected(detected) ⇒ Object



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

def style_detected(detected)
  return if no_acceptable_style?

  # `detected` can be a single style, or an Array of possible styles
  # (if there is more than one which matches the observed code)
  detected_as_strings = Array(detected).map(&:to_s)

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

#supported_stylesObject



83
84
85
# File 'lib/rubocop/cop/mixin/configurable_enforced_style.rb', line 83

def supported_styles
  @supported_styles ||= cop_config['SupportedStyles'].map(&:to_sym)
end

#unexpected_style_detected(unexpected) ⇒ Object



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

def unexpected_style_detected(unexpected)
  style_detected(unexpected)
end