Sha256: 0551893beff857dfed4c8589ff518b29d7fd66e08de670aebc132777b6af991b

Contents?: true

Size: 1.62 KB

Versions: 12

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # `context` block descriptions should start with 'when', or 'with'.
      #
      # @see https://github.com/reachlocal/rspec-style-guide#context-descriptions
      # @see http://www.betterspecs.org/#contexts
      #
      # @example `Prefixes` configuration option, defaults: 'when', 'with', and
      # 'without'
      #   Prefixes:
      #     - when
      #     - with
      #     - without
      #     - if
      #
      # @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.'.freeze

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

        def on_block(node)
          context_wording(node) do |context|
            add_offense(context, message: message)
          end
        end

        private

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

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

        def message
          format(MSG, prefixes: joined_prefixes)
        end

        def joined_prefixes
          quoted = prefixes.map { |prefix| "'#{prefix}'" }
          return quoted.first if quoted.size == 1
          quoted << "or #{quoted.pop}"
          quoted.join(', ')
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rubocop-rspec-1.29.1 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.29.0 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.28.0 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.27.0 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.26.0 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.25.1 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.25.0 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.24.0 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.23.0 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.22.2 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.22.1 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.22.0 lib/rubocop/cop/rspec/context_wording.rb