-
-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
252 additions
and
35 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Avoid duplicated metadata. | ||
# | ||
# @example | ||
# # bad | ||
# describe 'Something', :a, :a | ||
# | ||
# # good | ||
# describe 'Something', :a | ||
class DuplicatedMetadata < Base | ||
extend AutoCorrector | ||
|
||
include Metadata | ||
include RangeHelp | ||
|
||
MSG = 'Avoid duplicated metadata.' | ||
|
||
def on_metadata(symbols, _pairs) | ||
symbols.each do |symbol| | ||
on_metadata_symbol(symbol) | ||
end | ||
end | ||
|
||
private | ||
|
||
def on_metadata_symbol(node) | ||
return unless duplicated?(node) | ||
|
||
add_offense(node) do |corrector| | ||
autocorrect(corrector, node) | ||
end | ||
end | ||
|
||
def autocorrect(corrector, node) | ||
corrector.remove( | ||
range_with_surrounding_comma( | ||
range_with_surrounding_space( | ||
node.location.expression, | ||
side: :left | ||
), | ||
:left | ||
) | ||
) | ||
end | ||
|
||
def duplicated?(node) | ||
node.left_siblings.any? do |sibling| | ||
sibling.eql?(node) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Helper methods to find RSpec metadata. | ||
module Metadata | ||
extend RuboCop::NodePattern::Macros | ||
|
||
include RuboCop::RSpec::Language | ||
|
||
# @!method rspec_metadata(node) | ||
def_node_matcher :rspec_metadata, <<~PATTERN | ||
(block | ||
(send | ||
#rspec? {#Examples.all #ExampleGroups.all #SharedGroups.all #Hooks.all} _ ${send str sym}* (hash $...)?) | ||
...) | ||
PATTERN | ||
|
||
# @!method rspec_configure(node) | ||
def_node_matcher :rspec_configure, <<~PATTERN | ||
(block (send #rspec? :configure) (args (arg $_)) ...) | ||
PATTERN | ||
|
||
# @!method metadata_in_block(node) | ||
def_node_search :metadata_in_block, <<~PATTERN | ||
(send (lvar %) #Hooks.all _ ${send str sym}* (hash $...)?) | ||
PATTERN | ||
|
||
def on_block(node) | ||
rspec_configure(node) do |block_var| | ||
metadata_in_block(node, block_var) do |symbols, pairs| | ||
on_metadata(symbols, pairs.flatten) | ||
end | ||
end | ||
|
||
rspec_metadata(node) do |symbols, pairs| | ||
on_metadata(symbols, pairs.flatten) | ||
end | ||
end | ||
alias on_numblock on_block | ||
|
||
def on_metadata(_symbols, _pairs) | ||
raise ::NotImplementedError | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::DuplicatedMetadata do | ||
context 'when metadata is not used' do | ||
it 'registers no offense' do | ||
expect_no_offenses(<<~RUBY) | ||
describe 'Something' do | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when metadata is not duplicated' do | ||
it 'registers no offense' do | ||
expect_no_offenses(<<~RUBY) | ||
describe 'Something', :a, :b do | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when metadata is duplicated on example group' do | ||
it 'registers offense' do | ||
expect_offense(<<~RUBY) | ||
describe 'Something', :a, :a do | ||
^^ Avoid duplicated metadata. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
describe 'Something', :a do | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when metadata is duplicated in different order' do | ||
it 'registers offense' do | ||
expect_offense(<<~RUBY) | ||
describe 'Something', :a, :b, :a do | ||
^^ Avoid duplicated metadata. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
describe 'Something', :a, :b do | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when metadata is duplicated on example' do | ||
it 'registers offense' do | ||
expect_offense(<<~RUBY) | ||
it 'does something', :a, :a do | ||
^^ Avoid duplicated metadata. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
it 'does something', :a do | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when metadata is duplicated on shared examples' do | ||
it 'registers offense' do | ||
expect_offense(<<~RUBY) | ||
shared_examples 'something', :a, :a do | ||
^^ Avoid duplicated metadata. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
shared_examples 'something', :a do | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when metadata is duplicated on configuration hook' do | ||
it 'registers offense' do | ||
expect_offense(<<~RUBY) | ||
RSpec.configure do |configuration| | ||
configuration.before(:each, :a, :a) do | ||
^^ Avoid duplicated metadata. | ||
end | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
RSpec.configure do |configuration| | ||
configuration.before(:each, :a) do | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
end |