Sha256: 04eb94b1aac97e993c8cd45c7834fd57b4b248d3160a7449dd9995548c1f17ed

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 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, hash)
            symbols += hash.pairs.map(&:key) unless hash.nil?
            offenses = symbols.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.2.0 lib/rubocop/cop/rspec/extra/restrict_block_tag.rb