# frozen_string_literal: true

require_relative 'base'
require 'yard'

module Solargraph
  module Rspec
    module Correctors
      # Corrects RSpec DSL methods in the specs by adding superclass references to the namespace pins (ie. contexts).
      # The DSLs methods are generated by rspec-core YARD annotations defined in [RSpec::Core::ExampleGroup]
      class DslMethodsCorrector < Base
        # @param _source_map [Solargraph::SourceMap]
        # @return [void]
        def correct(_source_map)
          rspec_walker.after_walk do
            namespace_pins.each_with_index do |namespace_pin, index|
              super_class = if index.zero?
                              'RSpec::Core::ExampleGroup'
                            else
                              namespace_pins[index - 1].path
                            end
              add_pin(
                Pin::Reference::Superclass.new(
                  location: namespace_pin.location,
                  closure: namespace_pin,
                  name: super_class
                )
              )
            end
          end
        end
      end
    end
  end
end
