Skip to content

Commit

Permalink
Merge pull request #1497 from r7kamura/feature/metadata-style
Browse files Browse the repository at this point in the history
Add `RSpec/MetadataStyle` and `RSpec/EmptyMetadata` cops
  • Loading branch information
Darhazer authored Sep 8, 2023
2 parents 9fc5ca4 + 54d7b8e commit 97269bc
Show file tree
Hide file tree
Showing 13 changed files with 724 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ RSpec/ContainExactly:
Enabled: true
RSpec/DuplicatedMetadata:
Enabled: true
RSpec/EmptyMetadata:
Enabled: true
RSpec/Eq:
Enabled: true
RSpec/ExcessiveDocstringSpacing:
Expand All @@ -156,6 +158,8 @@ RSpec/IndexedLet:
Enabled: true
RSpec/MatchArray:
Enabled: true
RSpec/MetadataStyle:
Enabled: true
RSpec/NoExpectationExample:
Enabled: true
RSpec/PendingWithoutReason:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Split `RSpec/FilePath` into `RSpec/SpecFilePathSuffix` and `RSpec/SpecFilePathFormat`. `RSpec/FilePath` cop is enabled by default, the two new cups are pending and need to be enabled explicitly. ([@ydah])
- Add support `RSpec/Rails/HttpStatus` when `have_http_status` with string argument. ([@ydah])
- Fix a false positive `RSpec/Focus` when chained method call and inside define method. ([@ydah])
- Add `RSpec/MetadataStyle` and `RSpec/EmptyMetadata` cops. ([@r7kamura])

## 2.23.2 (2023-08-09)

Expand Down
16 changes: 16 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ RSpec/EmptyLineAfterSubject:
StyleGuide: https://rspec.rubystyle.guide/#empty-line-after-let
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyLineAfterSubject

RSpec/EmptyMetadata:
Description: Avoid empty metadata hash.
Enabled: pending
VersionAdded: "<<next>>"
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyMetadata

RSpec/Eq:
Description: Use `eq` instead of `be ==` to compare objects.
Enabled: pending
Expand Down Expand Up @@ -619,6 +625,16 @@ RSpec/MessageSpies:
VersionAdded: '1.9'
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MessageSpies

RSpec/MetadataStyle:
Description: Use consistent metadata style.
Enabled: pending
EnforcedStyle: symbol
SupportedStyles:
- hash
- symbol
VersionAdded: "<<next>>"
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MetadataStyle

RSpec/MissingExampleGroupArgument:
Description: Checks that the first argument to an example group is not empty.
Enabled: true
Expand Down
2 changes: 2 additions & 0 deletions docs/modules/ROOT/pages/cops.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* xref:cops_rspec.adoc#rspecemptylineafterfinallet[RSpec/EmptyLineAfterFinalLet]
* xref:cops_rspec.adoc#rspecemptylineafterhook[RSpec/EmptyLineAfterHook]
* xref:cops_rspec.adoc#rspecemptylineaftersubject[RSpec/EmptyLineAfterSubject]
* xref:cops_rspec.adoc#rspecemptymetadata[RSpec/EmptyMetadata]
* xref:cops_rspec.adoc#rspeceq[RSpec/Eq]
* xref:cops_rspec.adoc#rspecexamplelength[RSpec/ExampleLength]
* xref:cops_rspec.adoc#rspecexamplewithoutdescription[RSpec/ExampleWithoutDescription]
Expand Down Expand Up @@ -61,6 +62,7 @@
* xref:cops_rspec.adoc#rspecmessagechain[RSpec/MessageChain]
* xref:cops_rspec.adoc#rspecmessageexpectation[RSpec/MessageExpectation]
* xref:cops_rspec.adoc#rspecmessagespies[RSpec/MessageSpies]
* xref:cops_rspec.adoc#rspecmetadatastyle[RSpec/MetadataStyle]
* xref:cops_rspec.adoc#rspecmissingexamplegroupargument[RSpec/MissingExampleGroupArgument]
* xref:cops_rspec.adoc#rspecmultipledescribes[RSpec/MultipleDescribes]
* xref:cops_rspec.adoc#rspecmultipleexpectations[RSpec/MultipleExpectations]
Expand Down
87 changes: 87 additions & 0 deletions docs/modules/ROOT/pages/cops_rspec.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,37 @@ let(:foo) { bar }
* https://rspec.rubystyle.guide/#empty-line-after-let
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyLineAfterSubject

== RSpec/EmptyMetadata

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Yes
| <<next>>
| -
|===

Avoid empty metadata hash.

=== Examples

==== EnforcedStyle: symbol (default)

[source,ruby]
----
# bad
describe 'Something', {}
# good
describe 'Something'
----

=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/EmptyMetadata

== RSpec/Eq

|===
Expand Down Expand Up @@ -3227,6 +3258,62 @@ do_something

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MessageSpies

== RSpec/MetadataStyle

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Yes
| <<next>>
| -
|===

Use consistent metadata style.

This cop does not support autocorrection in the case of
`EnforcedStyle: hash` where the trailing metadata type is ambiguous.
(e.g. `describe 'Something', :a, b`)

=== Examples

==== EnforcedStyle: symbol (default)

[source,ruby]
----
# bad
describe 'Something', a: true
# good
describe 'Something', :a
----

==== EnforcedStyle: hash

[source,ruby]
----
# bad
describe 'Something', :a
# good
describe 'Something', a: true
----

=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `symbol`
| `hash`, `symbol`
|===

=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MetadataStyle

== RSpec/MissingExampleGroupArgument

|===
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/duplicated_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DuplicatedMetadata < Base

MSG = 'Avoid duplicated metadata.'

def on_metadata(symbols, _pairs)
def on_metadata(symbols, _hash)
symbols.each do |symbol|
on_metadata_symbol(symbol)
end
Expand Down
46 changes: 46 additions & 0 deletions lib/rubocop/cop/rspec/empty_metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

module RuboCop
module Cop
module RSpec
# Avoid empty metadata hash.
#
# @example EnforcedStyle: symbol (default)
# # bad
# describe 'Something', {}
#
# # good
# describe 'Something'
class EmptyMetadata < Base
extend AutoCorrector

include Metadata
include RangeHelp

MSG = 'Avoid empty metadata hash.'

def on_metadata(_symbols, hash)
return unless hash&.pairs&.empty?

add_offense(hash) do |corrector|
remove_empty_metadata(corrector, hash)
end
end

private

def remove_empty_metadata(corrector, node)
corrector.remove(
range_with_surrounding_comma(
range_with_surrounding_space(
node.source_range,
side: :left
),
:left
)
)
end
end
end
end
end
Loading

0 comments on commit 97269bc

Please sign in to comment.