Class: Solargraph::Rspec::SpecWalker::FakeLetMethod

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

Constant Summary collapse

MATCH_BODY =
Regexp.union(
  /do(.*)end/m,
  /{(.*)}/m
)

Class Method Summary collapse

Class Method Details

.transform_block(block_ast, code, method_name = nil) ⇒ RubyVM::AbstractSyntaxTree::Node

Parameters:

  • block_ast (RubyVM::AbstractSyntaxTree::Node)

Returns:

  • (RubyVM::AbstractSyntaxTree::Node)


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

def self.transform_block(block_ast, code, method_name = nil)
  method_name ||= NodeTypes.let_method_name(block_ast)
  block_body = block_ast.children[1]
  matches = code.lines[block_body.first_lineno - 1..block_body.last_lineno - 1].join.match(MATCH_BODY)
  method_body = (matches[1] || matches[2]).strip

  ast = RubyVM::AbstractSyntaxTree.parse <<~RUBY
    def #{method_name}
      #{method_body}
    end
  RUBY

  ast.children[2]
rescue SyntaxError
  raise "Failed to build fake let method: #{block_ast.inspect}, message: #{e.message}"
ensure
  nil
end