Sha256: 406a4abedd1e000e2894874d109d4145de1b58ed9a87bda39ad922d36207887d

Contents?: true

Size: 1.83 KB

Versions: 20

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Ezcater
      # Use ".<class method>" instead of "self.<class method>" in RSpec example
      # group descriptions.
      #
      # @example
      #
      #   # good
      #   describe ".does_stuff" do
      #     ...
      #   end
      #
      #   # bad
      #   describe "self.does_stuff" do
      #     ...
      #   end
      #
      #   # bad
      #   describe "::does_stuff" do
      #     ...
      #   end

      class RspecDotNotSelfDot < Cop
        SELF_DOT_REGEXP = /["']self\./.freeze
        COLON_COLON_REGEXP = /["'](\:\:)/.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.'

        def_node_matcher :example_group_match, <<-PATTERN
          (send _ #{RuboCop::RSpec::Language::ExampleGroups::ALL.node_pattern_union} $_ ...)
        PATTERN

        def on_send(node)
          example_group_match(node) do |doc|
            if doc.source.match?(SELF_DOT_REGEXP)
              add_offense(doc, location: :expression, message: SELF_DOT_MSG)
            elsif doc.source.match?(COLON_COLON_REGEXP)
              add_offense(doc, location: :expression, message: COLON_COLON_MSG)
            end
          end
        end

        def autocorrect(node)
          lambda do |corrector|
            experession_end = node.source.match?(COLON_COLON_REGEXP) ? 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
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
ezcater_rubocop-2.5.0 lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb
ezcater_rubocop-2.4.0 lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb
ezcater_rubocop-2.3.0 lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb
ezcater_rubocop-2.2.0 lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb
ezcater_rubocop-2.1.0 lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb
ezcater_rubocop-2.1.0.pre1 lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb
ezcater_rubocop-2.0.0 lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb
ezcater_rubocop-2.0.0.pre1 lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb
ezcater_rubocop-1.4.1 lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb
ezcater_rubocop-1.4.0 lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb
ezcater_rubocop-1.3.0 lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb
ezcater_rubocop-2.0.0.pre0 lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb
ezcater_rubocop-1.2.0 lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb
ezcater_rubocop-1.1.1 lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb
ezcater_rubocop-1.1.0 lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb
ezcater_rubocop-1.0.2 lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb
ezcater_rubocop-1.0.1 lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb
ezcater_rubocop-1.0.0 lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb
ezcater_rubocop-0.61.1 lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb
ezcater_rubocop-0.61.0 lib/rubocop/cop/ezcater/rspec_dot_not_self_dot.rb