Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support auto-correct for Style/SingleLineBlockParams #9167

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9167](https://github.com/rubocop-hq/rubocop/pull/9167): Support auto-correct for `StyleSingleLineBlockParams`. ([@koic][])
2 changes: 1 addition & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4272,7 +4272,7 @@ Style/SingleLineBlockParams:
Description: 'Enforces the names of some block params.'
Enabled: false
VersionAdded: '0.16'
VersionChanged: '0.47'
VersionChanged: <<next>>
Methods:
- reduce:
- acc
Expand Down
37 changes: 30 additions & 7 deletions lib/rubocop/cop/style/single_line_block_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ module Style
# c + d
# end
class SingleLineBlockParams < Base
extend AutoCorrector

MSG = 'Name `%<method>s` block params `|%<params>s|`.'

def on_block(node)
Expand All @@ -37,20 +39,41 @@ def on_block(node)
return unless eligible_method?(node)
return unless eligible_arguments?(node)

return if args_match?(node.send_node.method_name, node.arguments)
method_name = node.send_node.method_name
return if args_match?(method_name, node.arguments)

preferred_block_arguments = build_preferred_arguments_map(node, target_args(method_name))
joined_block_arguments = preferred_block_arguments.values.join(', ')

message = message(node.arguments)
message = format(MSG, method: method_name, params: joined_block_arguments)

add_offense(node.arguments, message: message)
add_offense(node.arguments, message: message) do |corrector|
autocorrect(corrector, node, preferred_block_arguments, joined_block_arguments)
end
end

private

def message(node)
method_name = node.parent.send_node.method_name
arguments = target_args(method_name).join(', ')
def build_preferred_arguments_map(node, preferred_arguments)
preferred_arguments_map = {}
node.arguments.each_with_index do |current_lvar, index|
preferred_argument = preferred_arguments[index]
current_argument = current_lvar.source
preferred_argument = "_#{preferred_argument}" if current_argument.start_with?('_')
preferred_arguments_map[current_argument] = preferred_argument
end

preferred_arguments_map
end

def autocorrect(corrector, node, preferred_block_arguments, joined_block_arguments)
corrector.replace(node.arguments, "|#{joined_block_arguments}|")

format(MSG, method: method_name, params: arguments)
node.each_descendant(:lvar) do |lvar|
if (preferred_lvar = preferred_block_arguments[lvar.source])
corrector.replace(lvar, preferred_lvar)
end
end
end

def eligible_arguments?(node)
Expand Down
18 changes: 17 additions & 1 deletion spec/rubocop/cop/style/single_line_block_params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ def m
^^^^^^ Name `test` block params `|x, y|`.
end
RUBY

expect_correction(<<~RUBY)
def m
[0, 1].reduce { |a, e| a + e }
[0, 1].reduce{ |a, e| a + e }
[0, 1].reduce(5) { |a, e| a + e }
[0, 1].reduce(5){ |a, e| a + e }
[0, 1].reduce (5) { |a, e| a + e }
[0, 1].reduce(5) { |a, e| a + e }
ala.test { |x, y| bala }
end
RUBY
end

it 'allows calls with proper argument names' do
Expand All @@ -49,7 +61,11 @@ def m
it 'finds incorrectly named parameters with leading underscores' do
expect_offense(<<~RUBY)
File.foreach(filename).reduce(0) { |_x, _y| }
^^^^^^^^ Name `reduce` block params `|a, e|`.
^^^^^^^^ Name `reduce` block params `|_a, _e|`.
RUBY

expect_correction(<<~RUBY)
File.foreach(filename).reduce(0) { |_a, _e| }
RUBY
end

Expand Down