-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
first_argument_indentation.rb
276 lines (251 loc) · 8.41 KB
/
first_argument_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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# frozen_string_literal: true
module RuboCop
module Cop
module Layout
# This cop checks the indentation of the first argument in a method call.
# Arguments after the first one are checked by `Layout/ArgumentAlignment`,
# not by this cop.
#
# For indenting the first parameter of method _definitions_, check out
# `Layout/FirstParameterIndentation`.
#
# This cop will respect `Layout/ArgumentAlignment` and will not work when
# `EnforcedStyle: with_fixed_indentation` is specified for `Layout/ArgumentAlignment`.
#
# @example
#
# # bad
# some_method(
# first_param,
# second_param)
#
# foo = some_method(
# first_param,
# second_param)
#
# foo = some_method(nested_call(
# nested_first_param),
# second_param)
#
# foo = some_method(
# nested_call(
# nested_first_param),
# second_param)
#
# some_method nested_call(
# nested_first_param),
# second_param
#
# @example EnforcedStyle: consistent
# # The first argument should always be indented one step more than the
# # preceding line.
#
# # good
# some_method(
# first_param,
# second_param)
#
# foo = some_method(
# first_param,
# second_param)
#
# foo = some_method(nested_call(
# nested_first_param),
# second_param)
#
# foo = some_method(
# nested_call(
# nested_first_param),
# second_param)
#
# some_method nested_call(
# nested_first_param),
# second_param
#
# @example EnforcedStyle: consistent_relative_to_receiver
# # The first argument should always be indented one level relative to
# # the parent that is receiving the argument
#
# # good
# some_method(
# first_param,
# second_param)
#
# foo = some_method(
# first_param,
# second_param)
#
# foo = some_method(nested_call(
# nested_first_param),
# second_param)
#
# foo = some_method(
# nested_call(
# nested_first_param),
# second_param)
#
# some_method nested_call(
# nested_first_param),
# second_params
#
# @example EnforcedStyle: special_for_inner_method_call
# # The first argument should normally be indented one step more than
# # the preceding line, but if it's a argument for a method call that
# # is itself a argument in a method call, then the inner argument
# # should be indented relative to the inner method.
#
# # good
# some_method(
# first_param,
# second_param)
#
# foo = some_method(
# first_param,
# second_param)
#
# foo = some_method(nested_call(
# nested_first_param),
# second_param)
#
# foo = some_method(
# nested_call(
# nested_first_param),
# second_param)
#
# some_method nested_call(
# nested_first_param),
# second_param
#
# @example EnforcedStyle: special_for_inner_method_call_in_parentheses (default)
# # Same as `special_for_inner_method_call` except that the special rule
# # only applies if the outer method call encloses its arguments in
# # parentheses.
#
# # good
# some_method(
# first_param,
# second_param)
#
# foo = some_method(
# first_param,
# second_param)
#
# foo = some_method(nested_call(
# nested_first_param),
# second_param)
#
# foo = some_method(
# nested_call(
# nested_first_param),
# second_param)
#
# some_method nested_call(
# nested_first_param),
# second_param
#
class FirstArgumentIndentation < Base
include Alignment
include ConfigurableEnforcedStyle
include RangeHelp
extend AutoCorrector
MSG = 'Indent the first argument one step more than %<base>s.'
def on_send(node)
return if style != :consistent && enforce_first_argument_with_fixed_indentation?
return if !node.arguments? || bare_operator?(node) || node.setter_method?
indent = base_indentation(node) + configured_indentation_width
check_alignment([node.first_argument], indent)
end
alias on_csend on_send
private
def autocorrect(corrector, node)
AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
end
def bare_operator?(node)
node.operator_method? && !node.dot?
end
def message(arg_node)
return 'Bad indentation of the first argument.' unless arg_node
send_node = arg_node.parent
text = base_range(send_node, arg_node).source.strip
base = if !/\n/.match?(text) && special_inner_call_indentation?(send_node)
"`#{text}`"
elsif comment_line?(text.lines.reverse_each.first)
'the start of the previous line (not counting the comment)'
else
'the start of the previous line'
end
format(MSG, base: base)
end
def base_indentation(node)
if special_inner_call_indentation?(node)
column_of(base_range(node, node.first_argument))
else
previous_code_line(node.first_argument.first_line) =~ /\S/
end
end
def special_inner_call_indentation?(node)
return false if style == :consistent
return true if style == :consistent_relative_to_receiver
parent = node.parent
return false unless eligible_method_call?(parent)
return false if !parent.parenthesized? &&
style == :special_for_inner_method_call_in_parentheses
# The node must begin inside the parent, otherwise node is the first
# part of a chained method call.
node.source_range.begin_pos > parent.source_range.begin_pos
end
# @!method eligible_method_call?(node)
def_node_matcher :eligible_method_call?, <<~PATTERN
(send _ !:[]= ...)
PATTERN
def base_range(send_node, arg_node)
parent = send_node.parent
start_node = if parent && (parent.splat_type? || parent.kwsplat_type?)
send_node.parent
else
send_node
end
range_between(start_node.source_range.begin_pos, arg_node.source_range.begin_pos)
end
# Returns the column of the given range. For single line ranges, this
# is simple. For ranges with line breaks, we look a the last code line.
def column_of(range)
source = range.source.strip
if source.include?("\n")
previous_code_line(range.line + source.count("\n") + 1) =~ /\S/
else
display_column(range)
end
end
# Takes the line number of a given code line and returns a string
# containing the previous line that's not a comment line or a blank
# line.
def previous_code_line(line_number)
line = ''
while line.blank? || comment_lines.include?(line_number)
line_number -= 1
line = processed_source.lines[line_number - 1]
end
line
end
def comment_lines
@comment_lines ||=
processed_source
.comments
.select { |c| begins_its_line?(c.loc.expression) }
.map { |c| c.loc.line }
end
def on_new_investigation
@comment_lines = nil
end
def enforce_first_argument_with_fixed_indentation?
return false unless argument_alignment_config['Enabled']
argument_alignment_config['EnforcedStyle'] == 'with_fixed_indentation'
end
def argument_alignment_config
config.for_cop('Layout/ArgumentAlignment')
end
end
end
end
end