Skip to content

Commit ad85a55

Browse files
koicbbatsov
authored andcommitted
Add new InternalAffairs/RedundantLetRuboCopConfigNew cop
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.
1 parent e916e9a commit ad85a55

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+133
-78
lines changed

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Metrics/ModuleLength:
109109

110110
RSpec/FilePath:
111111
Exclude:
112+
- spec/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new_spec.rb
112113
- spec/rubocop/formatter/junit_formatter_spec.rb
113114

114115
RSpec/PredicateMatcher:

lib/rubocop/cop/internal_affairs.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
require_relative 'internal_affairs/node_type_predicate'
88
require_relative 'internal_affairs/offense_location_keyword'
99
require_relative 'internal_affairs/redundant_described_class_as_subject'
10-
require_relative 'internal_affairs/redundant_message_argument'
10+
require_relative 'internal_affairs/redundant_let_rubocop_config_new'
1111
require_relative 'internal_affairs/redundant_location_argument'
12+
require_relative 'internal_affairs/redundant_message_argument'
1213
require_relative 'internal_affairs/style_detected_api_use'
1314
require_relative 'internal_affairs/useless_message_assertion'
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module Cop
5+
module InternalAffairs
6+
# This cop checks that `let` is `RuboCop::Config.new` with no arguments.
7+
#
8+
# @example
9+
# # bad
10+
# RSpec.describe RuboCop::Cop::Department::Foo, :config do
11+
# let(:config) { RuboCop::Config.new }
12+
# end
13+
#
14+
# # good
15+
# RSpec.describe RuboCop::Cop::Department::Foo, :config do
16+
# end
17+
#
18+
# RSpec.describe RuboCop::Cop::Department::Foo, :config do
19+
# let(:config) { RuboCop::Config.new(argument) }
20+
# end
21+
#
22+
class RedundantLetRuboCopConfigNew < Base
23+
include RangeHelp
24+
extend AutoCorrector
25+
26+
MSG = 'Remove `let` that is `RuboCop::Config.new` with no arguments%<additional_message>s.'
27+
28+
def_node_matcher :let_rubocop_config_new?, <<~PATTERN
29+
(block
30+
(send nil? :let
31+
(sym :config))
32+
(args)
33+
(send
34+
(const
35+
(const nil? :RuboCop) :Config) :new))
36+
PATTERN
37+
38+
def on_block(node)
39+
return unless let_rubocop_config_new?(node)
40+
41+
describe = find_describe_method_node(node)
42+
43+
unless (exist_config = describe.last_argument.source == ':config')
44+
additional_message = ' and specify `:config` in `describe`'
45+
end
46+
47+
message = format(MSG, additional_message: additional_message)
48+
49+
add_offense(node, message: message) do |corrector|
50+
corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
51+
52+
corrector.insert_after(describe.last_argument, ', :config') unless exist_config
53+
end
54+
end
55+
56+
private
57+
58+
def find_describe_method_node(block_node)
59+
block_node.ancestors.find { |node| node.block_type? && node.method?(:describe) }.send_node
60+
end
61+
end
62+
end
63+
end
64+
end

spec/rubocop/cop/internal_affairs/method_name_equal_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RuboCop::Cop::InternalAffairs::MethodNameEqual, :config do
4-
let(:config) { RuboCop::Config.new }
5-
64
it 'registers an offense when using `#method == :do_something`' do
75
expect_offense(<<~RUBY)
86
node.method_name == :do_something
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe RuboCop::Cop::InternalAffairs::RedundantLetRuboCopConfigNew, :config do
4+
it 'registers an offense when using `let(:config)` and `:config` is not specified in `describe`' do
5+
expect_offense(<<~RUBY)
6+
RSpec.describe RuboCop::Cop::Layout::SpaceAfterComma do
7+
subject(:cop) { described_class.new(config) }
8+
9+
let(:config) { RuboCop::Config.new }
10+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove `let` that is `RuboCop::Config.new` with no arguments and specify `:config` in `describe`.
11+
end
12+
RUBY
13+
14+
expect_correction(<<~RUBY)
15+
RSpec.describe RuboCop::Cop::Layout::SpaceAfterComma, :config do
16+
subject(:cop) { described_class.new(config) }
17+
18+
end
19+
RUBY
20+
end
21+
22+
it 'registers an offense when using `let(:config)` and `:config` is already specified in `describe`' do
23+
expect_offense(<<~RUBY)
24+
RSpec.describe RuboCop::Cop::Layout::SpaceAfterComma, :config do
25+
subject(:cop) { described_class.new(config) }
26+
27+
let(:config) { RuboCop::Config.new }
28+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove `let` that is `RuboCop::Config.new` with no arguments.
29+
end
30+
RUBY
31+
32+
expect_correction(<<~RUBY)
33+
RSpec.describe RuboCop::Cop::Layout::SpaceAfterComma, :config do
34+
subject(:cop) { described_class.new(config) }
35+
36+
end
37+
RUBY
38+
end
39+
40+
it 'registers an offense when using `let(:config)` with no argument `RuboCop::Config.new` and `:config` is specified' do
41+
expect_offense(<<~RUBY)
42+
RSpec.describe RuboCop::Cop::Layout::SpaceAfterComma, :config do
43+
subject(:cop) { described_class.new }
44+
45+
let(:config) { RuboCop::Config.new }
46+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove `let` that is `RuboCop::Config.new` with no arguments.
47+
end
48+
RUBY
49+
50+
expect_correction(<<~RUBY)
51+
RSpec.describe RuboCop::Cop::Layout::SpaceAfterComma, :config do
52+
subject(:cop) { described_class.new }
53+
54+
end
55+
RUBY
56+
end
57+
58+
it 'does not register an offense when using `let(:config)` with arguments to `RuboCop::Config.new`' do
59+
expect_no_offenses(<<~RUBY)
60+
RSpec.describe RuboCop::Cop::Layout::SpaceAfterComma do
61+
let(:config) { RuboCop::Config.new('Layout/SpaceInsideHashLiteralBraces' => brace_config) }
62+
end
63+
RUBY
64+
end
65+
end

spec/rubocop/cop/internal_affairs/style_detected_api_use_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RuboCop::Cop::InternalAffairs::StyleDetectedApiUse, :config do
4-
let(:config) { RuboCop::Config.new }
5-
64
it 'registers an offense when correct_style_detected is used without a negative *_style_detected follow up' do
75
expect_offense(<<~RUBY)
86
def on_send(node)

spec/rubocop/cop/layout/empty_line_after_magic_comment_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RuboCop::Cop::Layout::EmptyLineAfterMagicComment, :config do
4-
let(:config) { RuboCop::Config.new }
5-
64
it 'registers an offense for code that immediately follows comment' do
75
expect_offense(<<~RUBY)
86
# frozen_string_literal: true

spec/rubocop/cop/layout/empty_lines_around_begin_body_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RuboCop::Cop::Layout::EmptyLinesAroundBeginBody, :config do
4-
let(:config) { RuboCop::Config.new }
5-
64
shared_examples 'accepts' do |name, code|
75
it "accepts #{name}" do
86
expect_no_offenses(code)

spec/rubocop/cop/layout/empty_lines_around_exception_handling_keywords_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22

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

76
shared_examples 'accepts' do |name, code|

spec/rubocop/cop/layout/space_around_method_call_operator_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RuboCop::Cop::Layout::SpaceAroundMethodCallOperator, :config do
4-
let(:config) { RuboCop::Config.new }
5-
64
# FIXME: Remove unused vars
75
shared_examples 'offense' do |name, _code, offense, correction|
86
it "registers an offense and corrects when #{name}" do

0 commit comments

Comments
 (0)