Class: RuboCop::Cop::RSpec::MetadataStyle

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ConfigurableEnforcedStyle, RangeHelp, Metadata
Defined in:
lib/rubocop/cop/rspec/metadata_style.rb

Overview

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)

# bad
describe 'Something', a: true

# good
describe 'Something', :a

EnforcedStyle: hash

# bad
describe 'Something', :a

# good
describe 'Something', a: true

Instance Method Summary collapse

Methods included from Metadata

#metadata_in_block, #on_block, #rspec_configure, #rspec_metadata

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Methods inherited from Base

inherited, #on_new_investigation

Instance Method Details

#extract_metadata_hash(node) ⇒ Object



33
34
35
# File 'lib/rubocop/cop/rspec/metadata_style.rb', line 33

def_node_matcher :extract_metadata_hash, <<~PATTERN
  (send _ _ _ ... $hash)
PATTERN

#match_ambiguous_trailing_metadata?(node) ⇒ Object



43
44
45
# File 'lib/rubocop/cop/rspec/metadata_style.rb', line 43

def_node_matcher :match_ambiguous_trailing_metadata?, <<~PATTERN
  (send _ _ _ ... !{hash sym})
PATTERN

#match_boolean_metadata_pair?(node) ⇒ Object



38
39
40
# File 'lib/rubocop/cop/rspec/metadata_style.rb', line 38

def_node_matcher :match_boolean_metadata_pair?, <<~PATTERN
  (pair sym true)
PATTERN

#on_metadata(symbols, hash) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rubocop/cop/rspec/metadata_style.rb', line 47

def (symbols, hash)
  # RSpec example groups accept two string arguments. In such a case,
  # the rspec_metadata matcher will interpret the second string
  # argument as a metadata symbol.
  symbols.shift if symbols.first&.str_type?

  symbols.each do |symbol|
    (symbol)
  end

  return unless hash

  hash.pairs.each do |pair|
    (pair)
  end
end