Class: Solargraph::Rspec::Correctors::ContextBlockNamespaceCorrector

Inherits:
Base
  • Object
show all
Defined in:
lib/solargraph/rspec/correctors/context_block_namespace_corrector.rb

Overview

RSpec generates a namespace class for each context block. This corrector add the pins for those namespaces.

Instance Attribute Summary

Attributes inherited from Base

#added_pins, #namespace_pins, #rspec_walker

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Solargraph::Rspec::Correctors::Base

Instance Method Details

#correct(source_map) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/solargraph/rspec/correctors/context_block_namespace_corrector.rb', line 11

def correct(source_map)
  # @param location_range [Solargraph::Range]
  rspec_walker.on_each_context_block do |namespace_name, location_range|
    original_block_pin = source_map.locate_block_pin(location_range.start.line, location_range.start.column)
    location = PinFactory.build_location(location_range, source_map.filename)

    # Define a dynamic module for the example group block
    # Example:
    #   RSpec.describe Foo::Bar do  # => module RSpec::ExampleGroups::FooBar
    #     context 'some context' do # => module RSpec::ExampleGroups::FooBar::SomeContext
    #     end
    #   end
    namespace_pin = Solargraph::Pin::Namespace.new(
      name: namespace_name,
      location: location
    )

    override_closure(original_block_pin, namespace_pin)
    override_block_location(original_block_pin, source_map)

    # Include DSL methods in the example group block
    # TODO: This does not work on solagraph! Class methods are not included from parent class.
    namespace_extend_pin = PinFactory.build_module_extend(
      namespace_pin,
      root_example_group_namespace_pin.name,
      location
    )

    # Include parent example groups to share let definitions
    parent_namespace_name = namespace_name.split('::')[0..-2].join('::')
    namespace_include_pin = PinFactory.build_module_include(
      namespace_pin,
      parent_namespace_name,
      location
    )

    namespace_pins << namespace_pin

    add_pins(namespace_extend_pin, namespace_include_pin)
  end
end