Skip to content

Commit

Permalink
Add new InternalAffairs/RedundantLetRuboCopConfigNew cop
Browse files Browse the repository at this point in the history
Follow #9421.

This PR adds new `InternalAffairs/RedundantLetRuboCopConfigNew` cop.
It checks that `let` is `RuboCop::Config.new` with no arguments.

```ruby
# bad
RSpec.describe RuboCop::Cop::Department::Foo, :config do
  let(:config) { RuboCop::Config.new }
end

# good
RSpec.describe RuboCop::Cop::Department::Foo, :config do
end

RSpec.describe RuboCop::Cop::Department::Foo, :config do
  let(:config) { RuboCop::Config.new('Department/Bar' => configuration) }
end
```

No changelog entry has been added to the changelog due to this cop is
for use inside development.
  • Loading branch information
koic authored and bbatsov committed Jan 28, 2021
1 parent e916e9a commit ad85a55
Show file tree
Hide file tree
Showing 43 changed files with 133 additions and 78 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Metrics/ModuleLength:

RSpec/FilePath:
Exclude:
- spec/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new_spec.rb
- spec/rubocop/formatter/junit_formatter_spec.rb

RSpec/PredicateMatcher:
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/internal_affairs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
require_relative 'internal_affairs/node_type_predicate'
require_relative 'internal_affairs/offense_location_keyword'
require_relative 'internal_affairs/redundant_described_class_as_subject'
require_relative 'internal_affairs/redundant_message_argument'
require_relative 'internal_affairs/redundant_let_rubocop_config_new'
require_relative 'internal_affairs/redundant_location_argument'
require_relative 'internal_affairs/redundant_message_argument'
require_relative 'internal_affairs/style_detected_api_use'
require_relative 'internal_affairs/useless_message_assertion'
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

module RuboCop
module Cop
module InternalAffairs
# This cop checks that `let` is `RuboCop::Config.new` with no arguments.
#
# @example
# # bad
# RSpec.describe RuboCop::Cop::Department::Foo, :config do
# let(:config) { RuboCop::Config.new }
# end
#
# # good
# RSpec.describe RuboCop::Cop::Department::Foo, :config do
# end
#
# RSpec.describe RuboCop::Cop::Department::Foo, :config do
# let(:config) { RuboCop::Config.new(argument) }
# end
#
class RedundantLetRuboCopConfigNew < Base
include RangeHelp
extend AutoCorrector

MSG = 'Remove `let` that is `RuboCop::Config.new` with no arguments%<additional_message>s.'

def_node_matcher :let_rubocop_config_new?, <<~PATTERN
(block
(send nil? :let
(sym :config))
(args)
(send
(const
(const nil? :RuboCop) :Config) :new))
PATTERN

def on_block(node)
return unless let_rubocop_config_new?(node)

describe = find_describe_method_node(node)

unless (exist_config = describe.last_argument.source == ':config')
additional_message = ' and specify `:config` in `describe`'
end

message = format(MSG, additional_message: additional_message)

add_offense(node, message: message) do |corrector|
corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))

corrector.insert_after(describe.last_argument, ', :config') unless exist_config
end
end

private

def find_describe_method_node(block_node)
block_node.ancestors.find { |node| node.block_type? && node.method?(:describe) }.send_node
end
end
end
end
end
2 changes: 0 additions & 2 deletions spec/rubocop/cop/internal_affairs/method_name_equal_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::InternalAffairs::MethodNameEqual, :config do
let(:config) { RuboCop::Config.new }

it 'registers an offense when using `#method == :do_something`' do
expect_offense(<<~RUBY)
node.method_name == :do_something
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::InternalAffairs::RedundantLetRuboCopConfigNew, :config do
it 'registers an offense when using `let(:config)` and `:config` is not specified in `describe`' do
expect_offense(<<~RUBY)
RSpec.describe RuboCop::Cop::Layout::SpaceAfterComma do
subject(:cop) { described_class.new(config) }
let(:config) { RuboCop::Config.new }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove `let` that is `RuboCop::Config.new` with no arguments and specify `:config` in `describe`.
end
RUBY

expect_correction(<<~RUBY)
RSpec.describe RuboCop::Cop::Layout::SpaceAfterComma, :config do
subject(:cop) { described_class.new(config) }
end
RUBY
end

it 'registers an offense when using `let(:config)` and `:config` is already specified in `describe`' do
expect_offense(<<~RUBY)
RSpec.describe RuboCop::Cop::Layout::SpaceAfterComma, :config do
subject(:cop) { described_class.new(config) }
let(:config) { RuboCop::Config.new }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove `let` that is `RuboCop::Config.new` with no arguments.
end
RUBY

expect_correction(<<~RUBY)
RSpec.describe RuboCop::Cop::Layout::SpaceAfterComma, :config do
subject(:cop) { described_class.new(config) }
end
RUBY
end

it 'registers an offense when using `let(:config)` with no argument `RuboCop::Config.new` and `:config` is specified' do
expect_offense(<<~RUBY)
RSpec.describe RuboCop::Cop::Layout::SpaceAfterComma, :config do
subject(:cop) { described_class.new }
let(:config) { RuboCop::Config.new }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove `let` that is `RuboCop::Config.new` with no arguments.
end
RUBY

expect_correction(<<~RUBY)
RSpec.describe RuboCop::Cop::Layout::SpaceAfterComma, :config do
subject(:cop) { described_class.new }
end
RUBY
end

it 'does not register an offense when using `let(:config)` with arguments to `RuboCop::Config.new`' do
expect_no_offenses(<<~RUBY)
RSpec.describe RuboCop::Cop::Layout::SpaceAfterComma do
let(:config) { RuboCop::Config.new('Layout/SpaceInsideHashLiteralBraces' => brace_config) }
end
RUBY
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::InternalAffairs::StyleDetectedApiUse, :config do
let(:config) { RuboCop::Config.new }

it 'registers an offense when correct_style_detected is used without a negative *_style_detected follow up' do
expect_offense(<<~RUBY)
def on_send(node)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Layout::EmptyLineAfterMagicComment, :config do
let(:config) { RuboCop::Config.new }

it 'registers an offense for code that immediately follows comment' do
expect_offense(<<~RUBY)
# frozen_string_literal: true
Expand Down
2 changes: 0 additions & 2 deletions spec/rubocop/cop/layout/empty_lines_around_begin_body_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Layout::EmptyLinesAroundBeginBody, :config do
let(:config) { RuboCop::Config.new }

shared_examples 'accepts' do |name, code|
it "accepts #{name}" do
expect_no_offenses(code)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Layout::EmptyLinesAroundExceptionHandlingKeywords, :config do
let(:config) { RuboCop::Config.new }
let(:message) { '^{} Extra empty line detected' }

shared_examples 'accepts' do |name, code|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Layout::SpaceAroundMethodCallOperator, :config do
let(:config) { RuboCop::Config.new }

# FIXME: Remove unused vars
shared_examples 'offense' do |name, _code, offense, correction|
it "registers an offense and corrects when #{name}" do
Expand Down
2 changes: 0 additions & 2 deletions spec/rubocop/cop/lint/big_decimal_new_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::BigDecimalNew, :config do
let(:config) { RuboCop::Config.new }

it 'registers an offense and corrects using `BigDecimal.new()`' do
expect_offense(<<~RUBY)
BigDecimal.new(123.456, 3)
Expand Down
2 changes: 0 additions & 2 deletions spec/rubocop/cop/lint/deprecated_open_ssl_constant_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::DeprecatedOpenSSLConstant, :config do
let(:config) { RuboCop::Config.new }

it 'registers an offense with cipher constant and two arguments and corrects' do
expect_offense(<<~RUBY)
OpenSSL::Cipher::AES.new(128, :GCM)
Expand Down
2 changes: 0 additions & 2 deletions spec/rubocop/cop/lint/duplicate_methods_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::DuplicateMethods, :config do
let(:config) { RuboCop::Config.new }

shared_examples 'in scope' do |type, opening_line|
it "registers an offense for duplicate method in #{type}" do
expect_offense(<<~RUBY)
Expand Down
2 changes: 0 additions & 2 deletions spec/rubocop/cop/lint/mixed_regexp_capture_types_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::MixedRegexpCaptureTypes, :config do
let(:config) { RuboCop::Config.new }

it 'registers an offense when both of named and numbered captures are used' do
expect_offense(<<~RUBY)
/(?<foo>bar)(baz)/
Expand Down
2 changes: 0 additions & 2 deletions spec/rubocop/cop/lint/multiple_comparison_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::MultipleComparison, :config do
let(:config) { RuboCop::Config.new }

shared_examples 'Check to use two comparison operator' do |operator1, operator2|
it "registers an offense for x #{operator1} y #{operator2} z" do
expect_offense(<<~RUBY, operator1: operator1, operator2: operator2)
Expand Down
2 changes: 0 additions & 2 deletions spec/rubocop/cop/lint/no_return_in_begin_end_blocks_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::NoReturnInBeginEndBlocks, :config do
let(:config) { RuboCop::Config.new }

shared_examples 'rejects return inside a block' do |operator|
it "rejects a return statement inside a block when using #{operator}" do
expect_offense(<<-RUBY)
Expand Down
2 changes: 0 additions & 2 deletions spec/rubocop/cop/lint/out_of_range_regexp_ref_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::OutOfRangeRegexpRef, :config do
let(:config) { RuboCop::Config.new }

it 'registers an offense when references are used before any regexp' do
expect_offense(<<~RUBY)
puts $3
Expand Down
2 changes: 0 additions & 2 deletions spec/rubocop/cop/lint/redundant_with_index_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::RedundantWithIndex, :config do
let(:config) { RuboCop::Config.new }

it 'registers an offense for `ary.each_with_index { |v| v }` ' \
'and corrects to `ary.each`' do
expect_offense(<<~RUBY)
Expand Down
2 changes: 0 additions & 2 deletions spec/rubocop/cop/lint/redundant_with_object_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::RedundantWithObject, :config do
let(:config) { RuboCop::Config.new }

it 'registers an offense and corrects when using ' \
'`ary.each_with_object { |v| v }`' do
expect_offense(<<~RUBY)
Expand Down
2 changes: 0 additions & 2 deletions spec/rubocop/cop/lint/regexp_as_condition_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::RegexpAsCondition, :config do
let(:config) { RuboCop::Config.new }

it 'registers an offense and corrects for a regexp literal in `if` condition' do
expect_offense(<<~RUBY)
if /foo/
Expand Down
2 changes: 0 additions & 2 deletions spec/rubocop/cop/lint/rescue_type_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::RescueType, :config do
let(:config) { RuboCop::Config.new }

it 'accepts rescue modifier' do
expect_no_offenses('foo rescue nil')
end
Expand Down
3 changes: 1 addition & 2 deletions spec/rubocop/cop/lint/script_permission_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# frozen_string_literal: true

# rubocop:disable Style/NumericLiteralPrefix
RSpec.describe RuboCop::Cop::Lint::ScriptPermission do
RSpec.describe RuboCop::Cop::Lint::ScriptPermission, :config do
subject(:cop) { described_class.new(config, options) }

let(:config) { RuboCop::Config.new }
let(:options) { nil }

let(:file) { Tempfile.new('') }
Expand Down
2 changes: 0 additions & 2 deletions spec/rubocop/cop/lint/send_with_mixin_argument_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::SendWithMixinArgument, :config do
let(:config) { RuboCop::Config.new }

it 'registers an offense when using `send` with `include`' do
expect_offense(<<~RUBY)
Foo.send(:include, Bar)
Expand Down
2 changes: 0 additions & 2 deletions spec/rubocop/cop/lint/struct_new_override_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::StructNewOverride, :config do
let(:config) { RuboCop::Config.new }

it 'registers an offense using `Struct.new(symbol)`' do
expect_offense(<<~RUBY)
Bad = Struct.new(:members)
Expand Down
2 changes: 0 additions & 2 deletions spec/rubocop/cop/lint/to_json_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::ToJSON, :config do
let(:config) { RuboCop::Config.new }

it 'registers an offense and corrects using `#to_json` without arguments' do
expect_offense(<<~RUBY)
def to_json
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::TrailingCommaInAttributeDeclaration, :config do
let(:config) { RuboCop::Config.new }

it 'registers an offense when using trailing comma' do
expect_offense(<<~RUBY)
class Foo
Expand Down
2 changes: 0 additions & 2 deletions spec/rubocop/cop/lint/uri_escape_unescape_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::UriEscapeUnescape, :config do
let(:config) { RuboCop::Config.new }

it "registers an offense when using `URI.escape('http://example.com')`" do
expect_offense(<<~RUBY)
URI.escape('http://example.com')
Expand Down
2 changes: 0 additions & 2 deletions spec/rubocop/cop/lint/uri_regexp_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::UriRegexp, :config do
let(:config) { RuboCop::Config.new }

it 'does not register an offense when using `regexp` without receiver' do
expect_no_offenses(<<~RUBY)
regexp('http://example.com')
Expand Down
2 changes: 0 additions & 2 deletions spec/rubocop/cop/lint/useless_access_modifier_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::UselessAccessModifier, :config do
let(:config) { RuboCop::Config.new }

context 'when an access modifier has no effect' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
Expand Down
2 changes: 0 additions & 2 deletions spec/rubocop/cop/lint/void_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::Void, :config do
let(:config) { RuboCop::Config.new }

described_class::BINARY_OPERATORS.each do |op|
it "registers an offense for void op #{op} if not on last line" do
expect_offense(<<~RUBY, op: op)
Expand Down
2 changes: 0 additions & 2 deletions spec/rubocop/cop/style/commented_keyword_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Style::CommentedKeyword, :config do
let(:config) { RuboCop::Config.new }

it 'registers an offense and corrects when commenting on the same line as `end`' do
expect_offense(<<~RUBY)
if x
Expand Down
Loading

0 comments on commit ad85a55

Please sign in to comment.