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

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, NodePattern::Macros
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

#on_block(node) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb', line 53

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

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

  if message
    add_offense(str_node.loc.expression, message: message) do |corrector|
      expression_end = str_node.source.match?("::") ? 3 : 6
      corrector.replace(Parser::Source::Range.new(str_node.source_range.source_buffer,
                                                  str_node.source_range.begin_pos + 1,
                                                  str_node.source_range.begin_pos + expression_end), ".")
    end
  end
end