@@ -4,6 +4,7 @@ module RuboCop
4
4
module Cop
5
5
# Common functionality for checking whether an AST node/token is aligned
6
6
# with something on a preceding or following line
7
+ # rubocop:disable Metrics/ModuleLength
7
8
module PrecedingFollowingAlignment
8
9
# Tokens that end with an `=`, as well as `<<`, that can be aligned together:
9
10
# `=`, `==`, `===`, `!=`, `<=`, `>=`, `<<` and operator assignment (`+=`, etc).
@@ -156,10 +157,14 @@ def assignment_tokens
156
157
@assignment_tokens ||= begin
157
158
tokens = processed_source . tokens . select ( &:equal_sign? )
158
159
159
- # we don't want to operate on equals signs which are part of an
160
- # optarg in a method definition
161
- # e.g.: def method(optarg = default_val); end
162
- tokens = remove_optarg_equals ( tokens , processed_source )
160
+ # We don't want to operate on equals signs which are part of an `optarg` in a
161
+ # method definition, or the separator of an endless method definition.
162
+ # For example (the equals sign to ignore is highlighted with ^):
163
+ # def method(optarg = default_val); end
164
+ # ^
165
+ # def method = foo
166
+ # ^
167
+ tokens = remove_equals_in_def ( tokens , processed_source )
163
168
164
169
# Only attempt to align the first = on each line
165
170
Set . new ( tokens . uniq ( &:line ) )
@@ -195,11 +200,20 @@ def relevant_assignment_lines(line_range)
195
200
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
196
201
# rubocop:enable Metrics/PerceivedComplexity, Metrics/MethodLength
197
202
198
- def remove_optarg_equals ( asgn_tokens , processed_source )
199
- optargs = processed_source . ast . each_node ( :optarg )
200
- optarg_eql = optargs . to_set { |o | o . loc . operator . begin_pos }
201
- asgn_tokens . reject { |t | optarg_eql . include? ( t . begin_pos ) }
203
+ def remove_equals_in_def ( asgn_tokens , processed_source )
204
+ nodes = processed_source . ast . each_node ( :optarg , :def )
205
+ eqls_to_ignore = nodes . with_object ( [ ] ) do |node , arr |
206
+ loc = if node . def_type?
207
+ node . loc . assignment if node . endless?
208
+ else
209
+ node . loc . operator
210
+ end
211
+ arr << loc . begin_pos if loc
212
+ end
213
+
214
+ asgn_tokens . reject { |t | eqls_to_ignore . include? ( t . begin_pos ) }
202
215
end
203
216
end
217
+ # rubocop:enable Metrics/ModuleLength
204
218
end
205
219
end
0 commit comments