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
#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
#autocorrect(node) ⇒ Object
147
148
149
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 147
def autocorrect(node)
AlignmentCorrector.correct(processed_source, node, @column_delta)
end
|
#on_block(node) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 79
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
131
132
133
134
135
136
137
138
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 131
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
91
92
93
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 91
def on_class(node)
check_members(node.loc.keyword, [node.body])
end
|
#on_def(node) ⇒ Object
Also known as:
on_defs
114
115
116
117
118
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 114
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
64
65
66
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 64
def on_ensure(node)
check_indentation(node.loc.keyword, node.body)
end
|
#on_if(node, base = node) ⇒ Object
140
141
142
143
144
145
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 140
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
71
72
73
74
75
76
77
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 71
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
59
60
61
62
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 59
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 97
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
121
122
123
124
125
126
127
|
# File 'lib/rubocop/cop/layout/indentation_width.rb', line 121
def on_while(node, base = node)
return if ignored_node?(node)
return unless node.single_line_condition?
check_indentation(base.loc, node.body)
end
|