Sha256: 373548cc6da9448b3ca38a0bfce7e9dfaea11df76db46931b73bdfcaf7a7e778

Contents?: true

Size: 1.67 KB

Versions: 6

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks that `context` docstring starts with an allowed prefix.
      #
      # @see https://github.com/reachlocal/rspec-style-guide#context-descriptions
      # @see http://www.betterspecs.org/#contexts
      #
      # @example `Prefixes` configuration
      #
      #   # .rubocop.yml
      #   # RSpec/ContextWording:
      #   #   Prefixes:
      #   #     - when
      #   #     - with
      #   #     - without
      #   #     - if
      #   #     - unless
      #   #     - for
      #
      # @example
      #   # bad
      #   context 'the display name not present' do
      #     # ...
      #   end
      #
      #   # good
      #   context 'when the display name is not present' do
      #     # ...
      #   end
      class ContextWording < Cop
        MSG = 'Start context description with %<prefixes>s.'

        def_node_matcher :context_wording, <<-PATTERN
          (block (send #{RSPEC} { :context :shared_context } $(str #bad_prefix?) ...) ...)
        PATTERN

        def on_block(node)
          context_wording(node) do |context|
            add_offense(context,
                        message: format(MSG, prefixes: joined_prefixes))
          end
        end

        private

        def bad_prefix?(description)
          !prefixes.include?(description.split.first)
        end

        def joined_prefixes
          quoted = prefixes.map { |prefix| "'#{prefix}'" }
          return quoted.first if quoted.size == 1

          quoted << "or #{quoted.pop}"
          quoted.join(', ')
        end

        def prefixes
          cop_config['Prefixes'] || []
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rubocop-rspec-1.37.1 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.37.0 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.36.0 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.35.0 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.34.1 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.34.0 lib/rubocop/cop/rspec/context_wording.rb