-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
6 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
spec/rubocop/cop/style/super_with_args_parentheses_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |