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/RedundantContextConfigParameter
cop
Follow up of rubocop/rubocop-rails#638 (comment). This PR adds new `InternalAffairs/RedundantContextConfigParameter` cop that checks for redundant `:config` parameter in the `context` arguments. ```ruby # bad context 'foo', :config do end # good context 'foo' do end ```
- Loading branch information
Showing
4 changed files
with
98 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
46 changes: 46 additions & 0 deletions
46
lib/rubocop/cop/internal_affairs/redundant_context_config_parameter.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,46 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module InternalAffairs | ||
# Checks for redundant `:config` parameter in the `context` arguments. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# context 'foo', :config do | ||
# end | ||
# | ||
# # good | ||
# context 'foo' do | ||
# end | ||
# | ||
class RedundantContextConfigParameter < Base | ||
include RangeHelp | ||
extend AutoCorrector | ||
|
||
MSG = 'Remove the redundant `:config` parameter.' | ||
RESTRICT_ON_SEND = %i[context].freeze | ||
|
||
def on_send(node) | ||
arguments = node.arguments | ||
config_node = arguments.detect { |argument| argument.source == ':config' } | ||
return unless config_node | ||
|
||
add_offense(config_node) do |corrector| | ||
dup_arguments = arguments.dup | ||
dup_arguments.delete(config_node) | ||
|
||
corrector.replace(offense_range(arguments), dup_arguments.map(&:source).join(', ')) | ||
end | ||
end | ||
|
||
private | ||
|
||
def offense_range(arguments) | ||
range_between(arguments.first.source_range.begin_pos, arguments.last.source_range.end_pos) | ||
end | ||
end | ||
end | ||
end | ||
end |
50 changes: 50 additions & 0 deletions
50
spec/rubocop/cop/internal_affairs/redundant_context_config_parameter_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,50 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::InternalAffairs::RedundantContextConfigParameter, :config do | ||
it 'registers an offense when using `:config` parameter' do | ||
expect_offense(<<~RUBY) | ||
context 'foo', :config do | ||
^^^^^^^ Remove the redundant `:config` parameter. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
context 'foo' do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `:config` parameter with other parameters' do | ||
expect_offense(<<~RUBY) | ||
context 'foo', :ruby30, :rails70, :config do | ||
^^^^^^^ Remove the redundant `:config` parameter. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
context 'foo', :ruby30, :rails70 do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when not using `:config`' do | ||
expect_no_offenses(<<~RUBY) | ||
context 'foo' do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `:ruby30` only' do | ||
expect_no_offenses(<<~RUBY) | ||
context 'foo', :ruby30 do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `:config` in other than `context`' do | ||
expect_no_offenses(<<~RUBY) | ||
shared_context 'foo', :config do | ||
end | ||
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