Class: Solargraph::Rspec::SpecWalker

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/rspec/spec_walker.rb

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)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/solargraph/rspec/spec_walker.rb', line 10

def initialize(source_map:, config:)
  @source_map = source_map
  @config = config
  @walker = Rspec::Walker.from_source(source_map.source)
  @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:



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

def config
  @config
end

#walkerWalker (readonly)

Returns:



27
28
29
# File 'lib/solargraph/rspec/spec_walker.rb', line 27

def walker
  @walker
end

Instance Method Details

#after_walk(&block) ⇒ void

This method returns an undefined value.

Parameters:

  • block (Proc)


77
78
79
# File 'lib/solargraph/rspec/spec_walker.rb', line 77

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

#on_blocks_in_examples(&block) ⇒ void

This method returns an undefined value.

Parameters:

  • block (Proc)


71
72
73
# File 'lib/solargraph/rspec/spec_walker.rb', line 71

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

#on_described_class(&block) ⇒ void

This method returns an undefined value.

Parameters:

  • block (Proc)


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

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

#on_each_context_block(&block) ⇒ void

This method returns an undefined value.

Parameters:

  • block (Proc)


52
53
54
# File 'lib/solargraph/rspec/spec_walker.rb', line 52

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

#on_example_block(&block) ⇒ void

This method returns an undefined value.

Parameters:

  • block (Proc)


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

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

#on_hook_block(&block) ⇒ void

This method returns an undefined value.

Parameters:

  • block (Proc)


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

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

#on_let_method(&block) ⇒ void

This method returns an undefined value.

Parameters:

  • block (Proc)


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

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

#on_subject(&block) ⇒ void

This method returns an undefined value.

Parameters:

  • block (Proc)


46
47
48
# File 'lib/solargraph/rspec/spec_walker.rb', line 46

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

#walk!void

This method returns an undefined value.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
# File 'lib/solargraph/rspec/spec_walker.rb', line 82

def walk!
  each_context_block(@walker.ast, Rspec::ROOT_NAMESPACE) do |namespace_name, ast|
    @handlers[:on_each_context_block].each do |handler|
      handler.call(namespace_name, ast)
    end
  end

  rspec_const = ::Parser::AST::Node.new(:const, [nil, :RSpec])
  walker.on :send, [rspec_const, :describe, :any] do |ast|
    @handlers[:on_described_class].each do |handler|
      class_ast = ast.children[2]
      next unless class_ast

      class_name = full_constant_name(class_ast)
      handler.call(class_ast, class_name)
    end
  end

  config.let_methods.each do |let_method|
    walker.on :send, [nil, let_method] do |ast|
      @handlers[:on_let_method].each do |handler|
        handler.call(ast)
      end
    end
  end

  walker.on :block do |block_ast|
    next if block_ast.children.first.type != :send

    method_ast = block_ast.children.first
    method_name = method_ast.children[1]
    next unless Rspec::SUBJECT_METHODS.include?(method_name.to_s)

    @handlers[:on_subject].each do |handler|
      handler.call(method_ast)
    end
  end

  walker.on :block do |block_ast|
    next if block_ast.children.first.type != :send

    method_ast = block_ast.children.first
    method_name = method_ast.children[1]
    next unless Rspec::EXAMPLE_METHODS.include?(method_name.to_s)

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

    # @param blocks_in_examples [Parser::AST::Node]
    each_block(block_ast.children[2]) do |blocks_in_examples|
      @handlers[:on_blocks_in_examples].each do |handler|
        handler.call(blocks_in_examples)
      end
    end
  end

  walker.on :block do |block_ast|
    next if block_ast.children.first.type != :send

    method_ast = block_ast.children.first
    method_name = method_ast.children[1]
    next unless Rspec::HOOK_METHODS.include?(method_name.to_s)

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

    # @param blocks_in_examples [Parser::AST::Node]
    each_block(block_ast.children[2]) do |blocks_in_examples|
      @handlers[:on_blocks_in_examples].each do |handler|
        handler.call(blocks_in_examples)
      end
    end
  end

  walker.walk

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