-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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/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.
- Loading branch information
Showing
43 changed files
with
133 additions
and
78 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
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
64 changes: 64 additions & 0 deletions
64
lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.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,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 |
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
65 changes: 65 additions & 0 deletions
65
spec/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new_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,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 |
2 changes: 0 additions & 2 deletions
2
spec/rubocop/cop/internal_affairs/style_detected_api_use_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
2 changes: 0 additions & 2 deletions
2
spec/rubocop/cop/layout/empty_line_after_magic_comment_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
2 changes: 0 additions & 2 deletions
2
spec/rubocop/cop/layout/empty_lines_around_begin_body_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
1 change: 0 additions & 1 deletion
1
spec/rubocop/cop/layout/empty_lines_around_exception_handling_keywords_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
2 changes: 0 additions & 2 deletions
2
spec/rubocop/cop/layout/space_around_method_call_operator_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
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
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
2 changes: 0 additions & 2 deletions
2
spec/rubocop/cop/lint/trailing_comma_in_attribute_declaration_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
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
Oops, something went wrong.