Class: RuboCop::Cop::Layout::IndentationWidth
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.
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
Instance Method Summary
collapse
support_autocorrect?
extract_rhs, #on_lvasgn
#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
#exclude_limit
#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?
#ignore_node, #ignored_node?, #part_of_ignored_node?
Methods included from Util
silence_warnings
Instance Method Details
#access_modifier?(node) ⇒ Object
73
74
75
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 73
def_node_matcher :access_modifier?, "[(send ...) access_modifier?]\n"
|
#on_block(node) ⇒ Object
Also known as:
on_numblock, on_itblock
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 98
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
158
159
160
161
162
163
164
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 158
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
166
167
168
169
170
171
172
173
174
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 166
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
115
116
117
118
119
120
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 115
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
141
142
143
144
145
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 141
def on_def(node)
return if ignored_node?(node)
check_indentation(node.loc.keyword, node.body)
end
|
#on_ensure(node) ⇒ Object
86
87
88
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 86
def on_ensure(node)
check_indentation(node.loc.keyword, node.branch)
end
|
#on_if(node, base = node) ⇒ Object
176
177
178
179
180
181
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 176
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
90
91
92
93
94
95
96
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 90
def on_kwbegin(node)
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
81
82
83
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 81
def on_resbody(node)
check_indentation(node.loc.keyword, node.body)
end
|
#on_rescue(node) ⇒ Object
77
78
79
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 77
def on_rescue(node)
check_indentation(node.loc.else, node.else_branch)
end
|
#on_send(node) ⇒ Object
Also known as:
on_csend
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 124
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
148
149
150
151
152
153
154
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 148
def on_while(node, base = node)
return if ignored_node?(node)
return unless node.single_line_condition?
check_indentation(base.loc, node.body)
end
|