Sha256: 41629d55be41845f3e09397d693f78eb41f9f84bcca9c554c86d8f834a3ef167

Contents?: true

Size: 1.95 KB

Versions: 15

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Enforces use of string to titleize shared examples.
      #
      # @example
      #   # bad
      #   it_behaves_like :foo_bar_baz
      #   it_should_behave_like :foo_bar_baz
      #   shared_examples :foo_bar_baz
      #   shared_examples_for :foo_bar_baz
      #   include_examples :foo_bar_baz
      #
      #   # good
      #   it_behaves_like 'foo bar baz'
      #   it_should_behave_like 'foo bar baz'
      #   shared_examples 'foo bar baz'
      #   shared_examples_for 'foo bar baz'
      #   include_examples 'foo bar baz'
      #
      class SharedExamples < Base
        extend AutoCorrector

        # @!method shared_examples(node)
        def_node_matcher :shared_examples, <<~PATTERN
          {
            (send #rspec? #SharedGroups.all ...)
            (send nil? #Includes.all ...)
          }
        PATTERN

        def on_send(node)
          shared_examples(node) do
            ast_node = node.first_argument
            next unless ast_node&.sym_type?

            checker = Checker.new(ast_node)
            add_offense(checker.node, message: checker.message) do |corrector|
              corrector.replace(checker.node, checker.preferred_style)
            end
          end
        end

        # :nodoc:
        class Checker
          MSG = 'Prefer %<prefer>s over `%<current>s` ' \
                'to titleize shared examples.'

          attr_reader :node

          def initialize(node)
            @node = node
          end

          def message
            format(MSG, prefer: preferred_style, current: symbol.inspect)
          end

          def preferred_style
            string = symbol.to_s.tr('_', ' ')
            wrap_with_single_quotes(string)
          end

          private

          def symbol
            node.value
          end

          def wrap_with_single_quotes(string)
            "'#{string}'"
          end
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 4 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/shared_examples.rb
rubocop-rspec-2.25.0 lib/rubocop/cop/rspec/shared_examples.rb
rubocop-rspec-2.24.1 lib/rubocop/cop/rspec/shared_examples.rb
rubocop-rspec-2.24.0 lib/rubocop/cop/rspec/shared_examples.rb
rubocop-rspec-2.23.2 lib/rubocop/cop/rspec/shared_examples.rb
rubocop-rspec-2.23.1 lib/rubocop/cop/rspec/shared_examples.rb
rubocop-rspec-2.23.0 lib/rubocop/cop/rspec/shared_examples.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/shared_examples.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/shared_examples.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/shared_examples.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/shared_examples.rb
rubocop-rspec-2.22.0 lib/rubocop/cop/rspec/shared_examples.rb
rubocop-rspec-2.21.0 lib/rubocop/cop/rspec/shared_examples.rb
rubocop-rspec-2.20.0 lib/rubocop/cop/rspec/shared_examples.rb
rubocop-rspec-2.19.0 lib/rubocop/cop/rspec/shared_examples.rb