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

Fix Style/MethodCallWithArgsParentheses for endless method definitions #9279

Merged
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
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Metrics/MethodLength:
# Offense count: 8
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 132
Max: 135

# Offense count: 10
RSpec/AnyInstance:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9279](https://github.com/rubocop-hq/rubocop/pull/9279): Add support for endless methods to `Style/MethodCallWithArgsParentheses`. ([@dvandersluis][])
2 changes: 1 addition & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3548,7 +3548,7 @@ Style/MethodCallWithArgsParentheses:
StyleGuide: '#method-invocation-parens'
Enabled: false
VersionAdded: '0.47'
VersionChanged: '0.61'
VersionChanged: <<next>>
IgnoreMacros: true
IgnoredMethods: []
IgnoredPatterns: []
Expand Down
3 changes: 3 additions & 0 deletions lib/rubocop/cop/style/method_call_with_args_parentheses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ module Style
# to `true` allows the presence of parentheses in such a method call
# even with arguments.
#
# NOTE: Parens are required around a method with arguments when inside an
# endless method definition (>= Ruby 3.0).
#
# @example EnforcedStyle: require_parentheses (default)
#
# # bad
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,35 @@ module OmitParentheses

def omit_parentheses(node)
return unless node.parenthesized?
return if inside_endless_method_def?(node)
return if node.implicit_call?
return if super_call_without_arguments?(node)
return if allowed_camel_case_method_call?(node)
return if legitimate_call_with_parentheses?(node)

add_offense(offense_range(node), message: OMIT_MSG) do |corrector|
if parentheses_at_the_end_of_multiline_call?(node)
corrector.replace(args_begin(node), ' \\')
else
corrector.replace(args_begin(node), ' ')
end
corrector.remove(node.loc.end)
auto_correct(corrector, node)
end
end

def auto_correct(corrector, node)
if parentheses_at_the_end_of_multiline_call?(node)
corrector.replace(args_begin(node), ' \\')
else
corrector.replace(args_begin(node), ' ')
end
corrector.remove(node.loc.end)
end

def offense_range(node)
node.loc.begin.join(node.loc.end)
end

def inside_endless_method_def?(node)
# parens are required around arguments inside an endless method
node.each_ancestor(:def).any?(&:endless?) && node.arguments.any?
end

def super_call_without_arguments?(node)
node.super_type? && node.arguments.none?
end
Expand Down
45 changes: 45 additions & 0 deletions spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,48 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Style::MethodCallWithArgsParentheses, :config do
shared_examples 'endless methods' do |omit: false|
context 'endless methods', :ruby30 do
context 'with arguments' do
it 'requires method calls to have parens' do
expect_no_offenses(<<~RUBY)
def x() = foo("bar")
RUBY
end
end

context 'without arguments' do
if omit
it 'registers an offense when there are parens' do
expect_offense(<<~RUBY)
def x() = foo()
^^ Omit parentheses for method calls with arguments.
RUBY

expect_correction(<<~RUBY)
def x() = foo#{trailing_whitespace}
RUBY
end
else
it 'does not register an offense when there are parens' do
expect_no_offenses(<<~RUBY)
def x() = foo()
RUBY
end
end

it 'does not register an offense when there are no parens' do
expect_no_offenses(<<~RUBY)
def x() = foo
RUBY
end
end
end
end

context 'when EnforcedStyle is require_parentheses (default)' do
it_behaves_like 'endless methods'

it 'accepts no parens in method call without args' do
expect_no_offenses('top.test')
end
Expand Down Expand Up @@ -318,6 +359,8 @@ module Foo
{ 'EnforcedStyle' => 'omit_parentheses' }
end

it_behaves_like 'endless methods', omit: true

it 'register an offense for parens in method call without args' do
trailing_whitespace = ' '

Expand Down Expand Up @@ -796,6 +839,8 @@ def seatle_style arg: default(42)
}
end

it_behaves_like 'endless methods'

context 'in a class body' do
it 'finds offense' do
expect_offense(<<~RUBY)
Expand Down