Sha256: a75963264dcc0ca46ef917235d7c22718a85f36784f089619f47f26363f26e27

Contents?: true

Size: 1.74 KB

Versions: 3

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module RuboCop
  module RSpec
    # Wrapper for RSpec hook
    class Hook < Concept
      def_node_matcher :extract_metadata, <<~PATTERN
        (block
          (send _ _ #valid_scope? ? $...) ...
        )
      PATTERN

      def name
        node.method_name
      end

      def knowable_scope?
        scope_argument.nil? ||
          scope_argument.sym_type? ||
          scope_argument.hash_type?
      end

      def example?
        scope.equal?(:each)
      end

      def scope
        return :each if scope_argument&.hash_type?

        case scope_name
        when nil, :each, :example then :each
        when :context, :all       then :context
        when :suite               then :suite
        end
      end

      def metadata
        (extract_metadata(node) || [])
          .map { |meta| transform_metadata(meta) }
          .flatten
          .inject(&:merge)
      end

      private

      def valid_scope?(node)
        node&.sym_type? && Hooks::Scopes::ALL.include?(node.value)
      end

      def transform_metadata(meta)
        if meta.sym_type?
          { meta => true }
        else
          # This check is to be able to compare those two hooks:
          #
          #   before(:example, :special) { ... }
          #   before(:example, special: true) { ... }
          #
          # In the second case it's a node with a pair that has a value
          # of a `true_type?`.
          meta.pairs.map { |pair| { pair.key => transform_true(pair.value) } }
        end
      end

      def transform_true(node)
        node.true_type? ? true : node
      end

      def scope_name
        scope_argument.to_a.first
      end

      def scope_argument
        node.send_node.first_argument
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-rspec-2.0.0.pre lib/rubocop/rspec/hook.rb
rubocop-rspec-1.44.1 lib/rubocop/rspec/hook.rb
rubocop-rspec-1.44.0 lib/rubocop/rspec/hook.rb