-
-
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/RedundantDescribedClassAsSubject
cop
This PR adds new `InternalAffairs/RedundantDescribedClassAsSubject` cop. It checks for redundant `subject(:cop) { described_class.new }`. ```ruby # bad RSpec.describe RuboCop::Cop::Department::Foo do subject(:cop) { described_class.new(config) } end # good RSpec.describe RuboCop::Cop::Department::Foo, :config do end ``` No changelog entry has been added to the changelog due to this cop is for use inside development. And redundant `let(:config) { RuboCop::Config.new }` can also be removed. I will prepare the implementation as a different cop.
- Loading branch information
Showing
270 changed files
with
377 additions
and
794 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
61 changes: 61 additions & 0 deletions
61
lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.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,61 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module InternalAffairs | ||
# This cop checks for redundant `subject(:cop) { described_class.new }`. | ||
# | ||
# @example | ||
# # bad | ||
# RSpec.describe RuboCop::Cop::Department::Foo do | ||
# subject(:cop) { described_class.new(config) } | ||
# end | ||
# | ||
# # good | ||
# RSpec.describe RuboCop::Cop::Department::Foo, :config do | ||
# end | ||
# | ||
class RedundantDescribedClassAsSubject < Base | ||
include RangeHelp | ||
extend AutoCorrector | ||
|
||
MSG = 'Remove the redundant `subject`%<additional_message>s.' | ||
|
||
def_node_matcher :described_class_subject?, <<~PATTERN | ||
(block | ||
(send nil? :subject | ||
(sym :cop)) | ||
(args) | ||
(send | ||
(send nil? :described_class) :new | ||
$...)) | ||
PATTERN | ||
|
||
def on_block(node) | ||
return unless (described_class_arguments = described_class_subject?(node)) | ||
return if described_class_arguments.count >= 2 | ||
|
||
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
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
4 changes: 1 addition & 3 deletions
4
spec/rubocop/cop/internal_affairs/offense_location_keyword_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
65 changes: 65 additions & 0 deletions
65
spec/rubocop/cop/internal_affairs/redundant_described_class_as_subject_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::RedundantDescribedClassAsSubject, :config do | ||
it 'registers an offense when using `subject(:cop)` and `:config` is not specified in `describe`' do | ||
expect_offense(<<~RUBY) | ||
RSpec.describe RuboCop::Cop::Lint::RegexpAsCondition do | ||
subject(:cop) { described_class.new(config) } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove the redundant `subject` and specify `:config` in `describe`. | ||
let(:config) { RuboCop::Config.new } | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
RSpec.describe RuboCop::Cop::Lint::RegexpAsCondition, :config do | ||
let(:config) { RuboCop::Config.new } | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `subject(:cop)` and `:config` is already specified in `describe`' do | ||
expect_offense(<<~RUBY) | ||
RSpec.describe RuboCop::Cop::Lint::RegexpAsCondition, :config do | ||
subject(:cop) { described_class.new(config) } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove the redundant `subject`. | ||
let(:config) { RuboCop::Config.new } | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
RSpec.describe RuboCop::Cop::Lint::RegexpAsCondition, :config do | ||
let(:config) { RuboCop::Config.new } | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `subject(:cop)` with no argument `described_class.new` and `:config` is specified' do | ||
expect_offense(<<~RUBY) | ||
RSpec.describe RuboCop::Cop::Lint::RegexpAsCondition, :config do | ||
subject(:cop) { described_class.new } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove the redundant `subject`. | ||
let(:config) { RuboCop::Config.new } | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
RSpec.describe RuboCop::Cop::Lint::RegexpAsCondition, :config do | ||
let(:config) { RuboCop::Config.new } | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `subject(:cop)` with multiple arguments to `described_class.new`' do | ||
expect_no_offenses(<<~RUBY) | ||
RSpec.describe RuboCop::Cop::Lint::RegexpAsCondition do | ||
subject(:cop) { described_class.new(config, options) } | ||
end | ||
RUBY | ||
end | ||
end |
4 changes: 1 addition & 3 deletions
4
spec/rubocop/cop/internal_affairs/redundant_location_argument_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
4 changes: 1 addition & 3 deletions
4
spec/rubocop/cop/internal_affairs/redundant_message_argument_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
4 changes: 1 addition & 3 deletions
4
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
4 changes: 1 addition & 3 deletions
4
spec/rubocop/cop/internal_affairs/useless_message_assertion_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
4 changes: 1 addition & 3 deletions
4
spec/rubocop/cop/layout/closing_parenthesis_indentation_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
4 changes: 1 addition & 3 deletions
4
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
4 changes: 1 addition & 3 deletions
4
spec/rubocop/cop/layout/empty_line_after_multiline_condition_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_attribute_accessor_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
4 changes: 1 addition & 3 deletions
4
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
4 changes: 1 addition & 3 deletions
4
spec/rubocop/cop/layout/empty_lines_around_method_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
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.