Class: RuboCop::Cop::Layout::IndentationWidth
Overview
This cop checks for indentation that doesn’t use the specified number of spaces.
See also the IndentationConsistency cop which is the companion to this one.
Constant Summary
collapse
- MSG =
'Use %<configured_indentation_width>d (not %<indentation>d) ' \
'spaces for%<name>s indentation.'
Constants inherited
from Base
Base::RESTRICT_ON_SEND
Instance Attribute Summary
Attributes inherited from Cop
#offenses
Attributes inherited from Base
#config, #processed_source
Instance Method Summary
collapse
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 Cop
#add_offense, all, #corrections, #find_location, #highlights, joining_forces, #messages, #on_investigation_end, #on_new_investigation, #parse, qualified_cop_name, registry, support_autocorrect?, #support_autocorrect?
Methods inherited from Base
#add_global_offense, #add_offense, autocorrect_incompatible_with, badge, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, joining_forces, lint?, match?, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version
#exclude_limit
#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #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
56
57
58
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 56
def_node_matcher :access_modifier?, "[(send ...) access_modifier?]\n"
|
#autocorrect(node) ⇒ Object
148
149
150
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 148
def autocorrect(node)
AlignmentCorrector.correct(processed_source, node, @column_delta)
end
|
#on_block(node) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 80
def on_block(node)
end_loc = node.loc.end
return unless begins_its_line?(end_loc)
check_indentation(end_loc, node.body)
return unless indented_internal_methods_style?
check_members(end_loc, [node.body])
end
|
#on_case(case_node) ⇒ Object
132
133
134
135
136
137
138
139
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 132
def on_case(case_node)
case_node.each_when 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_class(node) ⇒ Object
Also known as:
on_sclass, on_module
92
93
94
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 92
def on_class(node)
check_members(node.loc.keyword, [node.body])
end
|
#on_def(node) ⇒ Object
Also known as:
on_defs
115
116
117
118
119
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 115
def on_def(node)
return if ignored_node?(node)
check_indentation(node.loc.keyword, node.body)
end
|
#on_ensure(node) ⇒ Object
Also known as:
on_resbody, on_for
65
66
67
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 65
def on_ensure(node)
check_indentation(node.loc.keyword, node.body)
end
|
#on_if(node, base = node) ⇒ Object
141
142
143
144
145
146
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 141
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
72
73
74
75
76
77
78
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 72
def on_kwbegin(node)
return unless begins_its_line?(node.loc.end)
check_indentation(node.loc.end, node.children.first)
end
|
#on_rescue(node) ⇒ Object
60
61
62
63
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 60
def on_rescue(node)
_begin_node, *_rescue_nodes, else_node = *node
check_indentation(node.loc.else, else_node)
end
|
#on_send(node) ⇒ Object
Also known as:
on_csend
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 98
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
122
123
124
125
126
127
128
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 122
def on_while(node, base = node)
return if ignored_node?(node)
return unless node.single_line_condition?
check_indentation(base.loc, node.body)
end
|