Skip to content

Commit

Permalink
Add new InternalAffairs/RedundantContextConfigParameter cop
Browse files Browse the repository at this point in the history
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
koic authored and bbatsov committed Feb 13, 2022
1 parent 4a3d37b commit 70b994f
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/rubocop/cop/internal_affairs.rb
Expand Up @@ -9,6 +9,7 @@
require_relative 'internal_affairs/node_matcher_directive'
require_relative 'internal_affairs/node_type_predicate'
require_relative 'internal_affairs/offense_location_keyword'
require_relative 'internal_affairs/redundant_context_config_parameter'
require_relative 'internal_affairs/redundant_described_class_as_subject'
require_relative 'internal_affairs/redundant_let_rubocop_config_new'
require_relative 'internal_affairs/redundant_location_argument'
Expand Down
@@ -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
@@ -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
2 changes: 1 addition & 1 deletion spec/rubocop/cop/style/ascii_comments_spec.rb
Expand Up @@ -19,7 +19,7 @@
expect_no_offenses('# AZaz1@$%~,;*_`|')
end

context 'when certain non-ascii chars are allowed', :config do
context 'when certain non-ascii chars are allowed' do
let(:cop_config) { { 'AllowedChars' => ['∂'] } }

it 'accepts comment with allowed non-ascii chars' do
Expand Down

0 comments on commit 70b994f

Please sign in to comment.