Module: Solargraph::Rspec::PinFactory

Defined in:
lib/solargraph/rspec/pin_factory.rb

Overview

Factory class for building pins and references.

Class Method Summary collapse

Class Method Details

.build_location(location_range, path) ⇒ Solargraph::Location

Parameters:

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

Returns:

  • (Solargraph::Location)


113
114
115
116
117
118
# File 'lib/solargraph/rspec/pin_factory.rb', line 113

def self.build_location(location_range, path)
  Solargraph::Location.new(
    File.expand_path(path),
    location_range
  )
end

.build_location_range(ast) ⇒ Solargraph::Range

Given the following code, Solargraph::Parser.node_range returns the following range for block ast:

some_method_with_block do ^ - block start end ^ - block end

Instead we want the range to be:

some_method_with_block do

^ - block start

end ^ - block end

Parameters:

  • ast (::Parser::AST::Node)

Returns:

  • (Solargraph::Range)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/solargraph/rspec/pin_factory.rb', line 94

def self.build_location_range(ast)
  if ast.type == :block
    method_range = Solargraph::Parser.node_range(ast.children[0])
    full_block_range = Solargraph::Parser.node_range(ast)

    Solargraph::Range.from_to(
      method_range.ending.line,
      method_range.ending.character,
      full_block_range.ending.line,
      full_block_range.ending.character
    )
  else
    Solargraph::Parser.node_range(ast)
  end
end

.build_module_extend(namespace, module_name, location) ⇒ Solargraph::Pin::Reference::Extend

Parameters:

  • namespace (Solargraph::Pin::Namespace)
  • module_name (String)
  • location (Solargraph::Location)

Returns:

  • (Solargraph::Pin::Reference::Extend)


61
62
63
64
65
66
67
# File 'lib/solargraph/rspec/pin_factory.rb', line 61

def self.build_module_extend(namespace, module_name, location)
  Solargraph::Pin::Reference::Extend.new(
    closure: namespace,
    name: module_name,
    location: location
  )
end

.build_module_include(namespace, module_name, location) ⇒ Solargraph::Pin::Reference::Include

Parameters:

  • namespace (Solargraph::Pin::Namespace)
  • module_name (String)
  • location (Solargraph::Location)

Returns:

  • (Solargraph::Pin::Reference::Include)


49
50
51
52
53
54
55
# File 'lib/solargraph/rspec/pin_factory.rb', line 49

def self.build_module_include(namespace, module_name, location)
  Solargraph::Pin::Reference::Include.new(
    closure: namespace,
    name: module_name,
    location: location
  )
end

.build_public_method(namespace, name, types: nil, location: nil, comments: [], attribute: false, scope: :instance, node: nil) ⇒ Solargraph::Pin::Method

Parameters:

  • namespace (Solargraph::Pin::Namespace)
  • name (String)
  • types (Array<String>, nil) (defaults to: nil)
  • node (Parser::AST::Node, nil) (defaults to: nil)
  • location (Solargraph::Location, nil) (defaults to: nil)
  • comments (Array<String>) (defaults to: [])
  • attribute (Boolean) (defaults to: false)
  • scope (:instance, :class) (defaults to: :instance)

    # rubocop:disable YARD/TagTypeSyntax

Returns:

  • (Solargraph::Pin::Method)


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
# File 'lib/solargraph/rspec/pin_factory.rb', line 18

def self.build_public_method(
  namespace,
  name,
  types: nil,
  location: nil,
  comments: [],
  attribute: false,
  scope: :instance,
  node: nil
)
  opts = {
    name: name,
    location: location,
    closure: namespace,
    scope: scope,
    attribute: attribute,
    comments: [],
    node: node
  }

  comments << "@return [#{types.join(",")}]" if types

  opts[:comments] = comments.join("\n")

  Solargraph::Pin::Method.new(**opts)
end

.dummy_location(path) ⇒ Solargraph::Location

Parameters:

  • path (String)

Returns:

  • (Solargraph::Location)


71
72
73
74
75
76
# File 'lib/solargraph/rspec/pin_factory.rb', line 71

def self.dummy_location(path)
  Solargraph::Location.new(
    File.expand_path(path),
    Solargraph::Range.from_to(0, 0, 0, 0)
  )
end