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

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
Alignment, AllowedPattern, CheckAssignment, ConfigurableEnforcedStyle, 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. The block body indentation for method chain blocks can be configured using the EnforcedStyleAlignWith setting.

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
# bad
value = (
foo - bar
)

# good
value = (
  foo - bar
)

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

EnforcedStyleAlignWith: start_of_line (default)

# good
records.uniq { |el| el[:profile_id] }
       .map do |message|
  SomeJob.perform_later(message[:id])
end

EnforcedStyleAlignWith: relative_to_receiver

# good
records.uniq { |el| el[:profile_id] }
       .map do |message|
         SomeJob.perform_later(message[:id])
       end

Constant Summary collapse

MSG =
'Use %<configured_indentation_width>d (not %<indentation>d) ' \
'%<indentation_type>s 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, #project_index

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

cop_dir_for, #exclude_limit, read_limits

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



84
85
86
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 84

def_node_matcher :access_modifier?, <<~PATTERN
  [(send ...) access_modifier?]
PATTERN

#on_begin(node) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 109

def on_begin(node)
  # Only a parenthesized grouping expression (e.g. `(\n  foo\n)`) has
  # explicit delimiters. Indent the body one step from the line
  # the opening parenthesis is on, but only when the closing parenthesis is
  # first on its line (a body on the opening line is skipped downstream).
  return unless parentheses?(node) && begins_its_line?(node.loc.end)

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

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



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 119

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

  return unless begins_its_line?(end_loc)

  base_loc = block_body_indentation_base(node, end_loc)
  check_indentation(base_loc, node.body)

  return unless indented_internal_methods_style?
  return unless contains_access_modifier?(node.body)

  check_members(end_loc, [node.body])
end

#on_case(case_node) ⇒ Object



179
180
181
182
183
184
185
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 179

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



187
188
189
190
191
192
193
194
195
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 187

def on_case_match(case_match)
  case_match.in_pattern_branches.each 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



136
137
138
139
140
141
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 136

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



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

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

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

#on_ensure(node) ⇒ Object



97
98
99
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 97

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

#on_if(node, base = node) ⇒ Object



197
198
199
200
201
202
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 197

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



101
102
103
104
105
106
107
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 101

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



92
93
94
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 92

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

#on_rescue(node) ⇒ Object



88
89
90
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 88

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

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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 145

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



169
170
171
172
173
174
175
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 169

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

  return unless node.single_line_condition?

  check_indentation(base.loc, node.body)
end