Class: RuboCop::Cop::Ezcater::RspecDotNotSelfDot

Inherits:
RuboCop::Cop
  • Object
show all
Extended by:
RSpec::Language::NodePattern
Includes:
RSpec::Language
Defined in:
lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb

Overview

Use “.<class method>” instead of “self.<class method>” in RSpec example group descriptions.

Examples:


# good
describe ".does_stuff" do
  ...
end

# bad
describe "self.does_stuff" do
  ...
end

# bad
describe "::does_stuff" do
  ...
end

Constant Summary collapse

RSPEC_EXAMPLE_PREFIXES =
["", "x", "f"].freeze
EXAMPLE_GROUP_IDENTIFIERS =
(RSPEC_EXAMPLE_PREFIXES.map do |prefix|
  %w(describe context feature).map { |identifier| "#{prefix}#{identifier}" }
end.flatten + %w(example_group)).freeze
EXAMPLE_IDENTIFIERS =
(RSPEC_EXAMPLE_PREFIXES.map do |prefix|
  %w(it specify example scenario).map { |identifier| "#{prefix}#{identifier}" }
end.flatten + %w(its focus skip)).freeze
SELF_DOT_REGEXP =

rubocop:disable Style/RedundantFreeze

/\Aself\./.freeze
COLON_COLON_REGEXP =

rubocop:disable Style/RedundantFreeze

/\A(::)/.freeze
SELF_DOT_MSG =
'Use ".<class method>" instead of "self.<class method>" for example group description.'
COLON_COLON_MSG =
'Use ".<class method>" instead of "::<class method>" for example group description.'

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb', line 63

def autocorrect(node)
  lambda do |corrector|
    experession_end = node.source.match?("::") ? 3 : 6
    corrector.replace(Parser::Source::Range.new(node.source_range.source_buffer,
                                                node.source_range.begin_pos + 1,
                                                node.source_range.begin_pos + experession_end), ".")
  end
end

#on_block(node) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb', line 52

def on_block(node)
  return unless example_group?(node)

  str_node = node.send_node.arguments[0]
  if str_node.value.match?(SELF_DOT_REGEXP)
    add_offense(str_node, location: :expression, message: SELF_DOT_MSG)
  elsif str_node.value.match?(COLON_COLON_REGEXP)
    add_offense(str_node, location: :expression, message: COLON_COLON_MSG)
  end
end