Class: Lowkey::SourceFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/factories/source_factory.rb

Class Method Summary collapse

Class Method Details

.body_source(method_source:) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/factories/source_factory.rb', line 48

def body_source(method_source:)
  # TODO: Handle multi-line return proxy which increases body start line.
  start_line = method_source.start_line + 1
  end_line = method_source.end_line - 1

  Source.new(file_path: method_source.file_path, scope: method_source.scope, lines: method_source.lines, start_line:, end_line:)
end

.class_source(node:, file_path:, lines:) ⇒ Object



23
24
25
26
27
28
# File 'lib/factories/source_factory.rb', line 23

def class_source(node:, file_path:, lines:)
  start_line = node.respond_to?(:class_keyword_loc) ? node.class_keyword_loc.start_line : 1
  end_line = node.respond_to?(:end_keyword_loc) ? node.end_keyword_loc.end_line : start_line

  Source.new(file_path:, scope: node.name, lines:, start_line:, end_line:)
end

.file_source(root_node:, file_path:) ⇒ Object



8
9
10
11
12
# File 'lib/factories/source_factory.rb', line 8

def file_source(root_node:, file_path:)
  end_line = root_node.respond_to?(:end_line) ? root_node.end_line : nil # TODO: Get file line count.

  Source.new(file_path:, scope: 'file', lines: root_node.script_lines, start_line: 1, end_line:)
end

.method_call_source(method_call_node:, arguments_node:, file_path:, lines:) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/factories/source_factory.rb', line 56

def method_call_source(method_call_node:, arguments_node:, file_path:, lines:)
  pattern = arguments_node.arguments.first.content
  scope = "#{method_call_node.name.upcase} #{pattern}"
  start_line = method_call_node.start_line
  end_line = method_call_node.end_line

  Source.new(file_path:, scope:, lines:, start_line:, end_line:)
end

.method_source(method_node:, file_path:, lines:) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/factories/source_factory.rb', line 30

def method_source(method_node:, file_path:, lines:)
  scope = method_node.name
  start_line = method_node.start_line
  end_line = method_node.end_line
  # TODO: Brittle conditional. What we really want to identify is if the method/rest of file was able to be parsed into an AST.
  end_line = end_line_from_indent(method_node:, lines:) if end_line >= lines.count

  Source.new(file_path:, scope:, lines:, start_line:, end_line:)
end

.module_source(node:, namespace:, file_path:, lines:) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/factories/source_factory.rb', line 14

def module_source(node:, namespace:, file_path:, lines:)
  start_line = node.respond_to?(:class_keyword_loc) ? node.class_keyword_loc.start_line : 1
  end_line = node.respond_to?(:end_keyword_loc) ? node.end_keyword_loc.end_line : start_line
  end_line = node.end_line if namespace == 'Object'
  scope = node.respond_to?(:name) ? node.name : 'Object'

  Source.new(file_path:, scope:, lines:, start_line:, end_line:)
end

.param_source(param_node:, file_path:, lines:) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/factories/source_factory.rb', line 40

def param_source(param_node:, file_path:, lines:)
  scope = param_node.name
  start_line = param_node.start_line
  end_line = param_node.end_line

  Source.new(file_path:, scope:, lines:, start_line:, end_line:)
end