Skip to content

Commit

Permalink
[Fix #12353] Add new Style/SuperWithArgsParentheses cop
Browse files Browse the repository at this point in the history
Fixes #12353.

This PR adds new `Style/SuperWithArgsParentheses` cop

```ruby
# bad
super name, age

# good
super(name, age)
```

The cop is named `Style/SuperWithArgsParentheses`, contrasting with `Style/MethodCallWithArgsParentheses`.
Unlike `Style/MethodCallWithArgsParentheses`, which requires consideration for no-parentheses usage in DSL.
`super` is not used as a DSL, so it is enabled by default.

For these reasons, it has been made into a separate, new cop.
  • Loading branch information
koic authored and bbatsov committed Nov 20, 2023
1 parent 2c0f759 commit dc4cbf0
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12353](https://github.com/rubocop/rubocop/issues/12353): Add new `Style/SuperWithArgsParentheses` cop. ([@koic][])
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5334,6 +5334,12 @@ Style/StructInheritance:
VersionAdded: '0.29'
VersionChanged: '1.20'

Style/SuperWithArgsParentheses:
Description: 'Use parentheses for `super` with arguments.'
StyleGuide: '#super-with-args'
Enabled: pending
VersionAdded: '<<next>>'

Style/SwapValues:
Description: 'Enforces the use of shorthand-style swapping of 2 variables.'
StyleGuide: '#values-swapping'
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@
require_relative 'rubocop/cop/style/string_methods'
require_relative 'rubocop/cop/style/strip'
require_relative 'rubocop/cop/style/struct_inheritance'
require_relative 'rubocop/cop/style/super_with_args_parentheses'
require_relative 'rubocop/cop/style/swap_values'
require_relative 'rubocop/cop/style/symbol_array'
require_relative 'rubocop/cop/style/symbol_literal'
Expand Down
35 changes: 35 additions & 0 deletions lib/rubocop/cop/style/super_with_args_parentheses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Style
# Enforces the presence of parentheses in `super` containing arguments.
#
# `super` is a keyword and is provided as a distinct cop from those designed for method call.
#
# @example
#
# # bad
# super name, age
#
# # good
# super(name, age)
#
class SuperWithArgsParentheses < Base
extend AutoCorrector

MSG = 'Use parentheses for `super` with arguments.'

def on_super(node)
return if node.parenthesized?

add_offense(node) do |corrector|
range = node.loc.keyword.end.join(node.first_argument.source_range.begin)
corrector.replace(range, '(')
corrector.insert_after(node.last_argument, ')')
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/rubocop/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize(path, offenses_by_iteration, loop_start: -1)
message = 'Infinite loop detected'
message += " in #{path}" if path
message += " and caused by #{root_cause}" if root_cause
super message
super(message)
end
end

Expand Down
32 changes: 32 additions & 0 deletions spec/rubocop/cop/style/super_with_args_parentheses_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Style::SuperWithArgsParentheses, :config do
it 'registers an offense when using `super` without parenthesized arguments' do
expect_offense(<<~RUBY)
super name, age
^^^^^^^^^^^^^^^ Use parentheses for `super` with arguments.
RUBY

expect_correction(<<~RUBY)
super(name, age)
RUBY
end

it 'does not register an offense when using `super` with parenthesized arguments' do
expect_no_offenses(<<~RUBY)
super(name, age)
RUBY
end

it 'does not register an offense when using `super` without arguments' do
expect_no_offenses(<<~RUBY)
super()
RUBY
end

it 'does not register an offense when using zero arity `super`' do
expect_no_offenses(<<~RUBY)
super
RUBY
end
end

0 comments on commit dc4cbf0

Please sign in to comment.