Skip to content

Commit 7e91fd9

Browse files
Earlopainbbatsov
authored andcommitted
[Fix #13933] Fix wrong autocorrect for Style/KeywordParametersOrder when the arguments are on multiple lines and contain comments
There is nothing sensible to do here, except to not offer autocorrect at all. I think.
1 parent f6ddcdd commit 7e91fd9

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#13933](https://github.com/rubocop/rubocop/issues/13933): Fix wrong autocorrect for `Style/KeywordParametersOrder` when the arguments are on multiple lines and contain comments. ([@earlopain][])

lib/rubocop/cop/style/keyword_parameters_order.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,25 @@ def on_kwoptarg(node)
4242
return if kwarg_nodes.empty?
4343

4444
add_offense(node) do |corrector|
45-
if node.parent.find(&:kwoptarg_type?) == node
46-
corrector.insert_before(node, "#{kwarg_nodes.map(&:source).join(', ')}, ")
45+
defining_node = node.each_ancestor(:def, :defs, :block).first
46+
next if processed_source.contains_comment?(arguments_range(defining_node))
47+
next unless node.parent.find(&:kwoptarg_type?) == node
4748

48-
arguments = node.each_ancestor(:def, :defs, :block).first.arguments
49-
append_newline_to_last_kwoptarg(arguments, corrector) unless parentheses?(arguments)
50-
51-
remove_kwargs(kwarg_nodes, corrector)
52-
end
49+
autocorrect(corrector, node, defining_node, kwarg_nodes)
5350
end
5451
end
5552

5653
private
5754

55+
def autocorrect(corrector, node, defining_node, kwarg_nodes)
56+
corrector.insert_before(node, "#{kwarg_nodes.map(&:source).join(', ')}, ")
57+
58+
arguments = defining_node.arguments
59+
append_newline_to_last_kwoptarg(arguments, corrector) unless parentheses?(arguments)
60+
61+
remove_kwargs(kwarg_nodes, corrector)
62+
end
63+
5864
def append_newline_to_last_kwoptarg(arguments, corrector)
5965
last_argument = arguments.last
6066
return if last_argument.type?(:kwrestarg, :blockarg)

spec/rubocop/cop/style/keyword_parameters_order_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ def m arg, required1:, required2:, optional1: 1, optional2: 2, **rest, &block
9393
RUBY
9494
end
9595

96+
it 'registers an offense but does not autocorrect when the argument range contains comments' do
97+
expect_offense(<<~RUBY)
98+
def foo(optional: 123,
99+
^^^^^^^^^^^^^ Place optional keyword parameters at the end of the parameters list.
100+
# Some explanation
101+
required:)
102+
end
103+
RUBY
104+
105+
expect_no_corrections
106+
end
107+
96108
it 'does not register an offense when there are no `kwoptarg`s before `kwarg`s' do
97109
expect_no_offenses(<<~RUBY)
98110
def m(arg, required:, optional: 1)

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