Sha256: ebd6651a879e58129c87c890e348828d54fa2a010b5eace7a1e109793cd85d04

Contents?: true

Size: 1.63 KB

Versions: 25

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Helper methods to find RSpec metadata.
      module Metadata
        extend RuboCop::NodePattern::Macros

        include RuboCop::RSpec::Language

        # @!method rspec_metadata(node)
        def_node_matcher :rspec_metadata, <<~PATTERN
          (block
            (send
              #rspec? {#Examples.all #ExampleGroups.all #SharedGroups.all #Hooks.all} _ $...)
            ...)
        PATTERN

        # @!method rspec_configure(node)
        def_node_matcher :rspec_configure, <<~PATTERN
          (block (send #rspec? :configure) (args (arg $_)) ...)
        PATTERN

        # @!method metadata_in_block(node)
        def_node_search :metadata_in_block, <<~PATTERN
          (send (lvar %) #Hooks.all _ $...)
        PATTERN

        def on_block(node)
          rspec_configure(node) do |block_var|
            metadata_in_block(node, block_var) do |metadata_arguments|
              on_metadata_arguments(metadata_arguments)
            end
          end

          rspec_metadata(node) do |metadata_arguments|
            on_metadata_arguments(metadata_arguments)
          end
        end
        alias on_numblock on_block

        def on_metadata(_symbols, _hash)
          raise ::NotImplementedError
        end

        private

        def on_metadata_arguments(metadata_arguments)
          *symbols, last = metadata_arguments
          hash = nil
          case last&.type
          when :hash
            hash = last
          when :sym
            symbols << last
          end
          on_metadata(symbols, hash)
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 5 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.3/lib/rubocop/cop/rspec/mixin/metadata.rb
rubocop-rspec-3.3.0 lib/rubocop/cop/rspec/mixin/metadata.rb
rubocop-rspec-3.2.0 lib/rubocop/cop/rspec/mixin/metadata.rb
rubocop-rspec-3.1.0 lib/rubocop/cop/rspec/mixin/metadata.rb
rubocop-rspec-3.0.5 lib/rubocop/cop/rspec/mixin/metadata.rb
rubocop-rspec-3.0.4 lib/rubocop/cop/rspec/mixin/metadata.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/mixin/metadata.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/mixin/metadata.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/mixin/metadata.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/mixin/metadata.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/mixin/metadata.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/mixin/metadata.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/mixin/metadata.rb
rubocop-rspec-2.31.0 lib/rubocop/cop/rspec/mixin/metadata.rb
rubocop-rspec-2.30.0 lib/rubocop/cop/rspec/mixin/metadata.rb
rubocop-rspec-2.29.2 lib/rubocop/cop/rspec/mixin/metadata.rb
rubocop-rspec-2.29.1 lib/rubocop/cop/rspec/mixin/metadata.rb
rubocop-rspec-2.29.0 lib/rubocop/cop/rspec/mixin/metadata.rb
rubocop-rspec-2.28.0 lib/rubocop/cop/rspec/mixin/metadata.rb
rubocop-rspec-2.27.1 lib/rubocop/cop/rspec/mixin/metadata.rb