/
class_and_module_children.rb
156 lines (127 loc) · 4.61 KB
/
class_and_module_children.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# This cop checks the style of children definitions at classes and
# modules. Basically there are two different styles:
#
# @example EnforcedStyle: nested (default)
# # good
# # have each child on its own line
# class Foo
# class Bar
# end
# end
#
# @example EnforcedStyle: compact
# # good
# # combine definitions as much as possible
# class Foo::Bar
# end
#
# The compact style is only forced for classes/modules with one child.
class ClassAndModuleChildren < Base
include ConfigurableEnforcedStyle
include RangeHelp
extend AutoCorrector
NESTED_MSG = 'Use nested module/class definitions instead of ' \
'compact style.'
COMPACT_MSG = 'Use compact module/class definition instead of ' \
'nested style.'
def on_class(node)
return if node.parent_class && style != :nested
check_style(node, node.body)
end
def on_module(node)
check_style(node, node.body)
end
private
def nest_or_compact(corrector, node)
if style == :nested
nest_definition(corrector, node)
else
compact_definition(corrector, node)
end
end
def nest_definition(corrector, node)
padding = ((' ' * indent_width) + leading_spaces(node)).to_s
padding_for_trailing_end = padding.sub(' ' * node.loc.end.column, '')
replace_keyword_with_module(corrector, node)
split_on_double_colon(corrector, node, padding)
add_trailing_end(corrector, node, padding_for_trailing_end)
end
def replace_keyword_with_module(corrector, node)
corrector.replace(node.loc.keyword, 'module')
end
def split_on_double_colon(corrector, node, padding)
children_definition = node.children.first
range = range_between(children_definition.loc.double_colon.begin_pos,
children_definition.loc.double_colon.end_pos)
replacement = "\n#{padding}#{node.loc.keyword.source} "
corrector.replace(range, replacement)
end
def add_trailing_end(corrector, node, padding)
replacement = "#{padding}end\n#{leading_spaces(node)}end"
corrector.replace(node.loc.end, replacement)
end
def compact_definition(corrector, node)
compact_node(corrector, node)
remove_end(corrector, node.body)
end
def compact_node(corrector, node)
replacement = "#{node.body.type} #{compact_identifier_name(node)}"
range = range_between(node.loc.keyword.begin_pos,
node.body.loc.name.end_pos)
corrector.replace(range, replacement)
end
def compact_identifier_name(node)
"#{node.identifier.const_name}::" \
"#{node.body.children.first.const_name}"
end
def remove_end(corrector, body)
range = range_between(
body.loc.end.begin_pos - leading_spaces(body).size,
body.loc.end.end_pos + 1
)
corrector.remove(range)
end
def leading_spaces(node)
node.source_range.source_line[/\A\s*/]
end
def indent_width
@config.for_cop('Layout/IndentationWidth')['Width'] || 2
end
def check_style(node, body)
return if node.identifier.children[0]&.cbase_type?
if style == :nested
check_nested_style(node)
else
check_compact_style(node, body)
end
end
def check_nested_style(node)
return unless compact_node_name?(node)
add_offense(node.loc.name, message: NESTED_MSG) do |corrector|
autocorrect(corrector, node)
end
end
def check_compact_style(node, body)
return unless one_child?(body) && !compact_node_name?(node)
add_offense(node.loc.name, message: COMPACT_MSG) do |corrector|
autocorrect(corrector, node)
end
end
def autocorrect(corrector, node)
return if node.class_type? && node.parent_class && style != :nested
nest_or_compact(corrector, node)
end
def one_child?(body)
body && %i[module class].include?(body.type)
end
def compact_node_name?(node)
/::/.match?(node.loc.name.source)
end
end
end
end
end