class RuboCop::Cop::RSpec::MetadataStyle
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`)
@example EnforcedStyle: symbol (default)
# bad describe 'Something', a: true # good describe 'Something', :a
@example EnforcedStyle: hash
# bad describe 'Something', :a # good describe 'Something', a: true
Public Instance Methods
Source
# File lib/rubocop/cop/rspec/metadata_style.rb, line 47 def on_metadata(symbols, hash) symbols.each do |symbol| on_metadata_symbol(symbol) if symbol.sym_type? end return unless hash hash.pairs.each do |pair| on_metadata_pair(pair) end end
Private Instance Methods
Source
# File lib/rubocop/cop/rspec/metadata_style.rb, line 61 def autocorrect_pair(corrector, node) remove_pair(corrector, node) insert_symbol(corrector, node) end
Source
# File lib/rubocop/cop/rspec/metadata_style.rb, line 66 def autocorrect_symbol(corrector, node) return if match_ambiguous_trailing_metadata?(node.parent) remove_symbol(corrector, node) insert_pair(corrector, node) end
Source
# File lib/rubocop/cop/rspec/metadata_style.rb, line 73 def bad_metadata_pair?(node) style == :symbol && match_boolean_metadata_pair?(node) end
Source
# File lib/rubocop/cop/rspec/metadata_style.rb, line 77 def bad_metadata_symbol?(_node) style == :hash end
Source
# File lib/rubocop/cop/rspec/metadata_style.rb, line 81 def format_symbol_to_pair_source(node) "#{node.value}: true" end
Source
# File lib/rubocop/cop/rspec/metadata_style.rb, line 85 def insert_pair(corrector, node) hash_node = extract_metadata_hash(node.parent) if hash_node.nil? insert_pair_as_last_argument(corrector, node) elsif hash_node.pairs.any? insert_pair_to_non_empty_hash_metadata(corrector, node, hash_node) else insert_pair_to_empty_hash_metadata(corrector, node, hash_node) end end
Source
# File lib/rubocop/cop/rspec/metadata_style.rb, line 96 def insert_pair_as_last_argument(corrector, node) corrector.insert_before( node.parent.location.end || node.parent.source_range.with( begin_pos: node.parent.source_range.end_pos ), ", #{format_symbol_to_pair_source(node)}" ) end
Source
# File lib/rubocop/cop/rspec/metadata_style.rb, line 105 def insert_pair_to_empty_hash_metadata(corrector, node, hash_node) corrector.insert_after( hash_node.location.begin, " #{format_symbol_to_pair_source(node)} " ) end
Source
# File lib/rubocop/cop/rspec/metadata_style.rb, line 112 def insert_pair_to_non_empty_hash_metadata(corrector, node, hash_node) corrector.insert_after( hash_node.children.last, ", #{format_symbol_to_pair_source(node)}" ) end
Source
# File lib/rubocop/cop/rspec/metadata_style.rb, line 119 def insert_symbol(corrector, node) corrector.insert_after( node.parent.left_sibling, ", #{node.key.value.inspect}" ) end
Source
# File lib/rubocop/cop/rspec/metadata_style.rb, line 126 def message_for_style format( 'Use %<style>s style for metadata.', style: style ) end
Source
# File lib/rubocop/cop/rspec/metadata_style.rb, line 133 def on_metadata_pair(node) return unless bad_metadata_pair?(node) add_offense(node, message: message_for_style) do |corrector| autocorrect_pair(corrector, node) end end
Source
# File lib/rubocop/cop/rspec/metadata_style.rb, line 141 def on_metadata_symbol(node) return unless bad_metadata_symbol?(node) add_offense(node, message: message_for_style) do |corrector| autocorrect_symbol(corrector, node) end end
Source
# File lib/rubocop/cop/rspec/metadata_style.rb, line 149 def remove_pair(corrector, node) if !node.parent.braces? || node.left_siblings.any? remove_pair_following(corrector, node) elsif node.right_siblings.any? remove_pair_preceding(corrector, node) else corrector.remove(node) end end
Source
# File lib/rubocop/cop/rspec/metadata_style.rb, line 159 def remove_pair_following(corrector, node) corrector.remove( range_with_surrounding_comma( range_with_surrounding_space( node.source_range, side: :left ), :left ) ) end
Source
# File lib/rubocop/cop/rspec/metadata_style.rb, line 171 def remove_pair_preceding(corrector, node) corrector.remove( range_with_surrounding_space( range_with_surrounding_comma( node.source_range, :right ), side: :right ) ) end
Source
# File lib/rubocop/cop/rspec/metadata_style.rb, line 183 def remove_symbol(corrector, node) corrector.remove( range_with_surrounding_comma( range_with_surrounding_space( node.source_range, side: :left ), :left ) ) end