Skip to content

Commit

Permalink
[Fix rubocop#12437] Fix an infinite loop error for `Style/MethodCallW…
Browse files Browse the repository at this point in the history
…ithArgsParentheses`

Fixes rubocop#12437.

This PR fixes an infinite loop error for `EnforcedStyle: omit_parentheses` of
`Style/MethodCallWithArgsParentheses` with `Style/SuperWithArgsParentheses`.

In rubocop#12390, `super` has become a separate `Style/SuperWithArgsParentheses` cop.
This PR prevents the infinite loop error by ensuring that `Style/MethodCallWithArgsParentheses`
no longer detects `super`, following the separation of `super` into `Style/SuperWithArgsParentheses` cop.

This approach aligns with the perspective that methods and `super` have different
considerations regarding parentheses usage, as mentioned in rubocop#12390.
  • Loading branch information
koic committed Jan 6, 2024
1 parent 7adcf78 commit c30daa2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12437](https://github.com/rubocop/rubocop/issues/12437): Fix an infinite loop error for `EnforcedStyle: omit_parentheses` of `Style/MethodCallWithArgsParentheses` with `Style/SuperWithArgsParentheses`. ([@koic][])
4 changes: 1 addition & 3 deletions lib/rubocop/cop/style/method_call_with_args_parentheses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,13 @@ def on_send(node)
send(style, node) # call require_parentheses or omit_parentheses
end
alias on_csend on_send
alias on_super on_send
alias on_yield on_send

private

def args_begin(node)
loc = node.loc
selector =
node.super_type? || node.yield_type? ? loc.keyword : loc.selector
selector = node.yield_type? ? loc.keyword : loc.selector

resize_by = args_parenthesized?(node) ? 2 : 1
selector.end.resize(resize_by)
Expand Down
30 changes: 30 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,36 @@ def foo(&block)
RUBY
end

it 'corrects `EnforcedStyle: omit_parentheses` of `Style/MethodCallWithArgsParentheses` with ' \
'`Style/SuperWithArgsParentheses`' do
create_file('.rubocop.yml', <<~YAML)
Style/MethodCallWithArgsParentheses:
EnforcedStyle: omit_parentheses
YAML
create_file('example.rb', <<~RUBY)
class Derived < Base
def do_something(arg)
super(arg)
end
end
RUBY
expect(
cli.run(
[
'--autocorrect',
'--only', 'Style/MethodCallWithArgsParentheses,Style/SuperWithArgsParentheses'
]
)
).to eq(0)
expect(File.read('example.rb')).to eq(<<~RUBY)
class Derived < Base
def do_something(arg)
super(arg)
end
end
RUBY
end

it 'corrects `Style/IfUnlessModifier` with `Style/SoleNestedConditional`' do
source = <<~RUBY
def foo
Expand Down
22 changes: 4 additions & 18 deletions spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,10 @@ def foo
RUBY
end

it 'registers an offense for superclass call without parens' do
expect_offense(<<~RUBY)
it 'does not register an offense for superclass call without parens' do
expect_no_offenses(<<~RUBY)
def foo
super a
^^^^^^^ Use parentheses for method calls with arguments.
end
RUBY

expect_correction(<<~RUBY)
def foo
super(a)
end
RUBY
end
Expand Down Expand Up @@ -526,17 +519,10 @@ def foo(**)
RUBY
end

it 'registers an offense for superclass call with parens' do
expect_offense(<<~RUBY)
it 'does not register an offense for superclass call with parens' do
expect_no_offenses(<<~RUBY)
def foo
super(a)
^^^ Omit parentheses for method calls with arguments.
end
RUBY

expect_correction(<<~RUBY)
def foo
super a
end
RUBY
end
Expand Down

0 comments on commit c30daa2

Please sign in to comment.