-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
access_modifier_indentation.rb
98 lines (85 loc) · 2.57 KB
/
access_modifier_indentation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# frozen_string_literal: true
module RuboCop
module Cop
module Layout
# Bare access modifiers (those not applying to specific methods) should be
# indented as deep as method definitions, or as deep as the class/module
# keyword, depending on configuration.
#
# @example EnforcedStyle: indent (default)
# # bad
# class Plumbus
# private
# def smooth; end
# end
#
# # good
# class Plumbus
# private
# def smooth; end
# end
#
# @example EnforcedStyle: outdent
# # bad
# class Plumbus
# private
# def smooth; end
# end
#
# # good
# class Plumbus
# private
# def smooth; end
# end
class AccessModifierIndentation < Cop
include Alignment
include ConfigurableEnforcedStyle
include RangeHelp
MSG = '%<style>s access modifiers like `%<node>s`.'
def on_class(node)
return unless node.body&.begin_type?
check_body(node.body, node)
end
alias on_sclass on_class
alias on_module on_class
alias on_block on_class
def autocorrect(node)
AlignmentCorrector.correct(processed_source, node, @column_delta)
end
private
def check_body(body, node)
modifiers = body.each_child_node(:send)
.select(&:bare_access_modifier?)
end_range = node.loc.end
modifiers.each { |modifier| check_modifier(modifier, end_range) }
end
def check_modifier(send_node, end_range)
offset = column_offset_between(send_node.source_range, end_range)
@column_delta = expected_indent_offset - offset
if @column_delta.zero?
correct_style_detected
else
add_offense(send_node) do
if offset == unexpected_indent_offset
opposite_style_detected
else
unrecognized_style_detected
end
end
end
end
def message(node)
format(MSG, style: style.capitalize, node: node.loc.selector.source)
end
def expected_indent_offset
style == :outdent ? 0 : configured_indentation_width
end
# An offset that is not expected, but correct if the configuration is
# changed.
def unexpected_indent_offset
configured_indentation_width - expected_indent_offset
end
end
end
end
end