Sha256: 80c043e656b517ef9f206cb26edc0f1690f760c47a2df31e080429d04089ea7c

Contents?: true

Size: 1.89 KB

Versions: 10

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks that `context` docstring starts with an allowed prefix.
      #
      # The default list of prefixes is minimal. Users are encouraged to tailor
      # the configuration to meet project needs. Other acceptable prefixes may
      # include `if`, `unless`, `for`, `before`, `after`, or `during`.
      #
      # @see https://rspec.rubystyle.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 < Base
        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(/\b/).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

10 entries across 10 versions & 1 rubygems

Version Path
rubocop-rspec-2.2.0 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-2.1.0 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-2.0.1 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-2.0.0 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-2.0.0.pre lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.44.1 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.44.0 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.43.2 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.43.1 lib/rubocop/cop/rspec/context_wording.rb
rubocop-rspec-1.43.0 lib/rubocop/cop/rspec/context_wording.rb