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

Parameters:

  • source_map (Solargraph::SourceMap)


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
52
53
54
55
56
57
58
# 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)
    original_block_pin_index = source_map.pins.index(original_block_pin)
    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
    )

    fixed_namespace_block_pin = Solargraph::Pin::Block.new(
      closure: namespace_pin,
      location: original_block_pin.location,
      receiver: original_block_pin.receiver,
      scope: original_block_pin.scope
    )

    source_map.pins[original_block_pin_index] = fixed_namespace_block_pin

    # Include DSL methods in the example group block
    # TOOD: 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