Skip to content

Commit 3dc1694

Browse files
dvandersluisbbatsov
authored andcommitted
[Fix #13751] Fix false positive in Layout/ExtraSpacing with ForceEqualSignAlignment: true for endless methods
1 parent cce60da commit 3dc1694

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#13751](https://github.com/rubocop/rubocop/issues/13751): Fix false positive in `Layout/ExtraSpacing` with `ForceEqualSignAlignment: true` for endless methods. ([@dvandersluis][])

lib/rubocop/cop/mixin/preceding_following_alignment.rb

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module RuboCop
44
module Cop
55
# Common functionality for checking whether an AST node/token is aligned
66
# with something on a preceding or following line
7+
# rubocop:disable Metrics/ModuleLength
78
module PrecedingFollowingAlignment
89
# Tokens that end with an `=`, as well as `<<`, that can be aligned together:
910
# `=`, `==`, `===`, `!=`, `<=`, `>=`, `<<` and operator assignment (`+=`, etc).
@@ -156,10 +157,14 @@ def assignment_tokens
156157
@assignment_tokens ||= begin
157158
tokens = processed_source.tokens.select(&:equal_sign?)
158159

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)
163168

164169
# Only attempt to align the first = on each line
165170
Set.new(tokens.uniq(&:line))
@@ -195,11 +200,20 @@ def relevant_assignment_lines(line_range)
195200
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
196201
# rubocop:enable Metrics/PerceivedComplexity, Metrics/MethodLength
197202

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) }
202215
end
203216
end
217+
# rubocop:enable Metrics/ModuleLength
204218
end
205219
end

spec/rubocop/cop/layout/extra_spacing_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,24 @@ def method(arg = 1)
628628
opt.ssh_config[e] = f
629629
RUBY
630630
end
631+
632+
context 'endless methods', :ruby30 do
633+
it 'does not register an offense when not aligned' do
634+
expect_no_offenses(<<~RUBY)
635+
def deleted = do_something
636+
def updated = do_something
637+
def added = do_something
638+
RUBY
639+
end
640+
641+
it 'does not register an offense with optional values' do
642+
expect_no_offenses(<<~RUBY)
643+
def deleted(x = true) = do_something(x)
644+
def updated(x = true) = do_something(x)
645+
def added(x = true) = do_something(x)
646+
RUBY
647+
end
648+
end
631649
end
632650

633651
context 'when exactly two comments have extra spaces' do

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy