Class: Solargraph::Rspec::SpecWalker

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/rspec/spec_walker.rb,
lib/solargraph/rspec/spec_walker/node_types.rb,
lib/solargraph/rspec/spec_walker/fake_let_method.rb,
lib/solargraph/rspec/spec_walker/full_constant_name.rb,
lib/solargraph/rspec/spec_walker/rspec_context_namespace.rb

Defined Under Namespace

Classes: FakeLetMethod, FullConstantName, NodeTypes, RspecContextNamespace

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_map:, config:) ⇒ SpecWalker

Returns a new instance of SpecWalker.

Parameters:

  • source_map (SourceMap)
  • config (Config)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/solargraph/rspec/spec_walker.rb', line 14

def initialize(source_map:, config:)
  @source_map = source_map
  @config = config
  @walker = Rspec::Walker.new(source_map.source.node)
  @handlers = {
    on_described_class: [],
    on_let_method: [],
    on_subject: [],
    on_each_context_block: [],
    on_example_block: [],
    on_hook_block: [],
    on_blocks_in_examples: [],
    after_walk: []
  }
end

Instance Attribute Details

#configConfig (readonly)

Returns:



34
35
36
# File 'lib/solargraph/rspec/spec_walker.rb', line 34

def config
  @config
end

#walkerWalker (readonly)

Returns:



31
32
33
# File 'lib/solargraph/rspec/spec_walker.rb', line 31

def walker
  @walker
end

Instance Method Details

#after_walk(&block) ⇒ void

This method returns an undefined value.

Parameters:

  • block (Proc)


94
95
96
# File 'lib/solargraph/rspec/spec_walker.rb', line 94

def after_walk(&block)
  @handlers[:after_walk] << block
end

#on_blocks_in_examples(&block) {|location_range| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • block (Proc)

Yield Parameters:

  • location_range (Solargraph::Range)


88
89
90
# File 'lib/solargraph/rspec/spec_walker.rb', line 88

def on_blocks_in_examples(&block)
  @handlers[:on_blocks_in_examples] << block
end

#on_described_class(&block) {|class_name, location_range| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • block (Proc)

Yield Parameters:

  • class_name (String)
  • location_range (Solargraph::Range)


40
41
42
# File 'lib/solargraph/rspec/spec_walker.rb', line 40

def on_described_class(&block)
  @handlers[:on_described_class] << block
end

#on_each_context_block(&block) {|namespace_name, location_range| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • block (Proc)

Yield Parameters:

  • namespace_name (String)
  • location_range (Solargraph::Range)


66
67
68
# File 'lib/solargraph/rspec/spec_walker.rb', line 66

def on_each_context_block(&block)
  @handlers[:on_each_context_block] << block
end

#on_example_block(&block) {|location_range| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • block (Proc)

Yield Parameters:

  • location_range (Solargraph::Range)


74
75
76
# File 'lib/solargraph/rspec/spec_walker.rb', line 74

def on_example_block(&block)
  @handlers[:on_example_block] << block
end

#on_hook_block(&block) {|location_range| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • block (Proc)

Yield Parameters:

  • location_range (Solargraph::Range)


81
82
83
# File 'lib/solargraph/rspec/spec_walker.rb', line 81

def on_hook_block(&block)
  @handlers[:on_hook_block] << block
end

#on_let_method(&block) {|method_name, location_range, fake_method_ast| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • block (Proc)

Yield Parameters:

  • method_name (String)
  • location_range (Solargraph::Range)
  • fake_method_ast (RubyVM::AbstractSyntaxTree::Node)


49
50
51
# File 'lib/solargraph/rspec/spec_walker.rb', line 49

def on_let_method(&block)
  @handlers[:on_let_method] << block
end

#on_subject(&block) {|method_name, location_range, fake_method_ast| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • block (Proc)

Yield Parameters:

  • method_name (String)
  • location_range (Solargraph::Range)
  • fake_method_ast (RubyVM::AbstractSyntaxTree::Node)


58
59
60
# File 'lib/solargraph/rspec/spec_walker.rb', line 58

def on_subject(&block)
  @handlers[:on_subject] << block
end

#walk!void

This method returns an undefined value.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/solargraph/rspec/spec_walker.rb', line 99

def walk!
  each_context_block(@walker.ast, Rspec::ROOT_NAMESPACE) do |namespace_name, block_ast|
    desc_node = NodeTypes.context_description_node(block_ast)

    @handlers[:on_each_context_block].each do |handler|
      handler.call(namespace_name, PinFactory.build_location_range(block_ast))
    end

    if NodeTypes.a_constant?(desc_node) # rubocop:disable Style/Next
      @handlers[:on_described_class].each do |handler|
        class_name_ast = NodeTypes.context_description_node(block_ast)
        class_name = FullConstantName.from_ast(class_name_ast)
        handler.call(class_name, PinFactory.build_location_range(class_name_ast))
      end
    end
  end

  walker.on :ITER do |block_ast|
    next unless NodeTypes.a_let_block?(block_ast, config)

    method_name = NodeTypes.let_method_name(block_ast)
    next unless method_name

    fake_method_ast = FakeLetMethod.transform_block(block_ast, @source_map.source.code)

    @handlers[:on_let_method].each do |handler|
      handler.call(method_name, PinFactory.build_location_range(block_ast.children[0]), fake_method_ast)
    end
  end

  walker.on :ITER do |block_ast|
    next unless NodeTypes.a_subject_block?(block_ast)

    method_name = NodeTypes.let_method_name(block_ast)
    fake_method_ast = FakeLetMethod.transform_block(block_ast, @source_map.source.code, method_name || 'subject')

    @handlers[:on_subject].each do |handler|
      handler.call(method_name, PinFactory.build_location_range(block_ast.children[0]), fake_method_ast)
    end
  end

  walker.on :ITER do |block_ast|
    next unless NodeTypes.a_example_block?(block_ast)

    @handlers[:on_example_block].each do |handler|
      handler.call(PinFactory.build_location_range(block_ast))
    end

    # @param blocks_in_examples [RubyVM::AbstractSyntaxTree::Node]
    each_block(block_ast.children[1]) do |blocks_in_examples|
      @handlers[:on_blocks_in_examples].each do |handler|
        handler.call(PinFactory.build_location_range(blocks_in_examples))
      end
    end
  end

  walker.on :ITER do |block_ast|
    next unless NodeTypes.a_hook_block?(block_ast)

    @handlers[:on_hook_block].each do |handler|
      handler.call(PinFactory.build_location_range(block_ast))
    end

    # @param blocks_in_examples [RubyVM::AbstractSyntaxTree::Node]
    each_block(block_ast.children[1]) do |blocks_in_examples|
      @handlers[:on_blocks_in_examples].each do |handler|
        handler.call(PinFactory.build_location_range(blocks_in_examples))
      end
    end
  end

  walker.walk

  @handlers[:after_walk].each(&:call)
end