Class: RuboCop::Cop::Layout::IndentationWidth

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
Alignment, AllowedPattern, CheckAssignment, EndKeywordAlignment, RangeHelp
Defined in:
lib/rubocop/cop/layout/indentation_width.rb

Overview

Checks for indentation that doesn’t use the specified number of spaces. The indentation width can be configured using the Width setting. The default width is 2.

See also the Layout/IndentationConsistency cop which is the companion to this one.

Examples:

Width: 2 (default)

# bad
class A
 def test
  puts 'hello'
 end
end

# good
class A
  def test
    puts 'hello'
  end
end

AllowedPatterns: [‘^s*module’]

# bad
module A
class B
  def test
  puts 'hello'
  end
end
end

# good
module A
class B
  def test
    puts 'hello'
  end
end
end

Constant Summary collapse

MSG =
'Use %<configured_indentation_width>d (not %<indentation>d) ' \
'spaces for%<name>s indentation.'

Constants included from RangeHelp

RangeHelp::BYTE_ORDER_MARK, RangeHelp::NOT_GIVEN

Constants included from Alignment

Alignment::SPACE

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods included from CheckAssignment

extract_rhs, #on_lvasgn

Methods included from ConfigurableEnforcedStyle

#alternative_style, #alternative_styles, #ambiguous_style_detected, #correct_style_detected, #detected_style, #detected_style=, #no_acceptable_style!, #no_acceptable_style?, #opposite_style_detected, #style, #style_configured?, #style_detected, #style_parameter_name, #supported_styles, #unexpected_style_detected

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#access_modifier?(node) ⇒ Object



56
57
58
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 56

def_node_matcher :access_modifier?, "[(send ...) access_modifier?]\n"

#on_block(node) ⇒ Object Also known as: on_numblock, on_itblock



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 81

def on_block(node)
  end_loc = node.loc.end

  return unless begins_its_line?(end_loc)

  # For blocks where the dot is on a new line, use the dot position as the base.
  # Otherwise, use the end keyword position as the base.
  base_loc = dot_on_new_line?(node) ? node.send_node.loc.dot : end_loc
  check_indentation(base_loc, node.body)

  return unless indented_internal_methods_style?

  check_members(end_loc, [node.body])
end

#on_case(case_node) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 142

def on_case(case_node)
  case_node.when_branches.each do |when_node|
    check_indentation(when_node.loc.keyword, when_node.body)
  end

  check_indentation(case_node.when_branches.last.loc.keyword, case_node.else_branch)
end

#on_case_match(case_match) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 150

def on_case_match(case_match)
  case_match.each_in_pattern do |in_pattern_node|
    check_indentation(in_pattern_node.loc.keyword, in_pattern_node.body)
  end

  else_branch = case_match.else_branch&.empty_else_type? ? nil : case_match.else_branch

  check_indentation(case_match.in_pattern_branches.last.loc.keyword, else_branch)
end

#on_class(node) ⇒ Object Also known as: on_sclass, on_module



99
100
101
102
103
104
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 99

def on_class(node)
  base = node.loc.keyword
  return if same_line?(base, node.body)

  check_members(base, [node.body])
end

#on_def(node) ⇒ Object Also known as: on_defs



125
126
127
128
129
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 125

def on_def(node)
  return if ignored_node?(node)

  check_indentation(node.loc.keyword, node.body)
end

#on_ensure(node) ⇒ Object



69
70
71
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 69

def on_ensure(node)
  check_indentation(node.loc.keyword, node.branch)
end

#on_if(node, base = node) ⇒ Object



160
161
162
163
164
165
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 160

def on_if(node, base = node)
  return if ignored_node?(node)
  return if node.ternary? || node.modifier_form?

  check_if(node, node.body, node.else_branch, base.loc)
end

#on_kwbegin(node) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 73

def on_kwbegin(node)
  # Check indentation against end keyword but only if it's first on its
  # line.
  return unless begins_its_line?(node.loc.end)

  check_indentation(node.loc.end, node.children.first)
end

#on_resbody(node) ⇒ Object Also known as: on_for



64
65
66
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 64

def on_resbody(node)
  check_indentation(node.loc.keyword, node.body)
end

#on_rescue(node) ⇒ Object



60
61
62
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 60

def on_rescue(node)
  check_indentation(node.loc.else, node.else_branch)
end

#on_send(node) ⇒ Object Also known as: on_csend



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 108

def on_send(node)
  super
  return unless node.adjacent_def_modifier?

  def_end_config = config.for_cop('Layout/DefEndAlignment')
  style = def_end_config['EnforcedStyleAlignWith'] || 'start_of_line'
  base = if style == 'def'
           node.first_argument
         else
           leftmost_modifier_of(node) || node
         end

  check_indentation(base.source_range, node.first_argument.body)
  ignore_node(node.first_argument)
end

#on_while(node, base = node) ⇒ Object Also known as: on_until



132
133
134
135
136
137
138
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 132

def on_while(node, base = node)
  return if ignored_node?(node)

  return unless node.single_line_condition?

  check_indentation(base.loc, node.body)
end