Sha256: 779ac189cf5ca528e065109f9bfe0a7e17f2f844a7810b8571c8ae4cc35fc1cc

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      module Extra
        # Restrict to only allowed block tags.
        #
        # @example AllowTags: ['foo', 'bar']
        #   # bad
        #   RSpec.describe 'Something', :baz do
        #   end
        #
        #   # bad
        #   RSpec.describe 'Something' do
        #     context 'when something', baz: :foo do
        #     end
        #   end
        #
        #   # good
        #   RSpec.describe 'Something', :foo do
        #   end
        #
        #   # good
        #   RSpec.describe 'Something' do
        #     context 'when something', bar: :foo do
        #     end
        #   end
        #
        class RestrictBlockTag < Base
          include Metadata

          def on_metadata(symbols, pairs)
            offenses = (symbols + pairs.map(&:key)).filter do |symbol|
              !allow_tags.include?(symbol.value.to_s)
            end

            return unless offenses.any?

            offenses.each do |offense|
              add_offense(offense, message: offense_message)
            end
          end

          private

          def offense_message
            if allow_tags.any?
              format("This tag is not allowed. Allowed tags: %<allow_tags>s.", allow_tags: allow_tags.join(", "))
            else
              "Tags are not allowed."
            end
          end

          def allow_tags
            cop_config.fetch("AllowTags", [])
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-rspec-extra-0.1.0 lib/rubocop/cop/rspec/extra/restrict_block_tag.rb