-
-
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.
Add new
InternalAffairs/RedundantExpectOffenseArguments
cop
This PR adds new `InternalAffairs/RedundantExpectOffenseArguments` cop, which checks for redundant arguments of `RuboCop::RSpec::ExpectOffense`'s methods. ```ruby # bad expect_no_offenses('code', keyword: keyword) # good expect_no_offenses('code') ```
- Loading branch information
Showing
11 changed files
with
121 additions
and
13 deletions.
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
34 changes: 34 additions & 0 deletions
34
lib/rubocop/cop/internal_affairs/redundant_expect_offense_arguments.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,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module InternalAffairs | ||
# Checks for redundant arguments of `RuboCop::RSpec::ExpectOffense`'s methods. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# expect_no_offenses('code', keyword: keyword) | ||
# | ||
# # good | ||
# expect_no_offenses('code') | ||
# | ||
class RedundantExpectOffenseArguments < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Remove the redundant arguments.' | ||
RESTRICT_ON_SEND = %i[expect_no_offenses].freeze | ||
|
||
def on_send(node) | ||
return if node.arguments.one? || !node.arguments[1]&.hash_type? | ||
|
||
range = node.first_argument.source_range.end.join(node.last_argument.source_range.end) | ||
|
||
add_offense(range) do |corrector| | ||
corrector.remove(range) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
73 changes: 73 additions & 0 deletions
73
spec/rubocop/cop/internal_affairs/redundant_expect_offense_arguments_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,73 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::InternalAffairs::RedundantExpectOffenseArguments, :config do | ||
it 'registers an offense when using `expect_no_offenses` with string and single keyword arguments' do | ||
expect_offense(<<~RUBY) | ||
expect_no_offenses('code', keyword: keyword) | ||
^^^^^^^^^^^^^^^^^^ Remove the redundant arguments. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect_no_offenses('code') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `expect_no_offenses` with heredoc and single keyword arguments' do | ||
expect_offense(<<~RUBY) | ||
expect_no_offenses(<<~CODE, keyword: keyword) | ||
^^^^^^^^^^^^^^^^^^ Remove the redundant arguments. | ||
CODE | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect_no_offenses(<<~CODE) | ||
CODE | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `expect_no_offenses` with heredoc multiple keyword arguments' do | ||
expect_offense(<<~RUBY) | ||
expect_no_offenses(<<~CODE, foo: foo, bar: bar) | ||
^^^^^^^^^^^^^^^^^^^^ Remove the redundant arguments. | ||
CODE | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect_no_offenses(<<~CODE) | ||
CODE | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `expect_no_offenses` with string argument only' do | ||
expect_no_offenses(<<~RUBY) | ||
expect_no_offenses('code') | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `expect_no_offenses` with heredoc argument only' do | ||
expect_no_offenses(<<~RUBY) | ||
expect_no_offenses(<<~CODE) | ||
CODE | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `expect_no_offenses` with string and positional arguments' do | ||
expect_no_offenses(<<~RUBY) | ||
expect_no_offenses('code', file) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `expect_no_offenses` with heredoc and positional arguments' do | ||
expect_no_offenses(<<~RUBY) | ||
expect_no_offenses(<<~CODE, file) | ||
CODE | ||
RUBY | ||
end | ||
|
||
it 'does not crash when using `expect_no_offenses` with no arguments' do | ||
expect_no_offenses(<<~RUBY) | ||
expect_no_offenses | ||
CODE | ||
RUBY | ||
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
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
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
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