Class: RubyLsp::Ree::ParsedMethodNode

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_node, contract_node) ⇒ ParsedMethodNode

Returns a new instance of ParsedMethodNode.



7
8
9
10
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 7

def initialize(method_node, contract_node)
  @method_node = method_node
  @contract_node = contract_node
end

Instance Attribute Details

#contract_nodeObject (readonly)

Returns the value of attribute contract_node.



5
6
7
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 5

def contract_node
  @contract_node
end

#method_nodeObject (readonly)

Returns the value of attribute method_node.



5
6
7
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 5

def method_node
  @method_node
end

#nested_local_methodsObject (readonly)

Returns the value of attribute nested_local_methods.



5
6
7
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 5

def nested_local_methods
  @nested_local_methods
end

Instance Method Details

#contract_in_parentheses?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 112

def contract_in_parentheses?
  @contract_node.opening == '(' && @contract_node.closing == ')'
end

#contract_node_contract_end_positionObject



108
109
110
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 108

def contract_node_contract_end_position
  @contract_node.message_loc.end_column - 1
end

#contract_node_end_lineObject



100
101
102
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 100

def contract_node_end_line
  @contract_node.location.end_line - 1
end

#contract_node_end_positionObject



96
97
98
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 96

def contract_node_end_position
  @contract_node.location.end_column - 1
end

#contract_node_start_lineObject



104
105
106
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 104

def contract_node_start_line
  @contract_node.location.start_line - 1
end

#end_lineObject



33
34
35
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 33

def end_line
  @method_node.location.end_line - 1
end

#full_method_bodyObject



41
42
43
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 41

def full_method_body
  @method_node.body
end

#has_contract?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 25

def has_contract?
  !!@contract_node
end

#has_throw_section?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 84

def has_throw_section?
  @contract_node && @contract_node.name == :throws
end

#method_bodyObject



37
38
39
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 37

def method_body
  get_method_body(@method_node)
end

#nameObject



12
13
14
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 12

def name
  @method_node.name
end

#param_namesObject



16
17
18
19
20
21
22
23
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 16

def param_names
  return [] unless @method_node.parameters
  
  @method_node.parameters.requireds.map(&:name) + 
  @method_node.parameters.keywords.map(&:name) + 
  [@method_node.parameters.rest&.name] + 
  [@method_node.parameters.keyword_rest&.name]
end

#parse_call_objectsObject



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 136

def parse_call_objects
  method_body = get_method_body(@method_node)
  return [] unless method_body

  call_nodes = parse_body_call_objects(method_body)
  call_expressions = parse_body_call_expressions(method_body)

  call_node_names = call_nodes.map(&:name) + call_expressions

  call_node_names
end

#parse_local_variablesObject



116
117
118
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 116

def parse_local_variables
  RubyLsp::Ree::LocalVariablesParser.new(self).method_local_variables
end

#parse_nested_local_methods(local_methods) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 120

def parse_nested_local_methods(local_methods)
  unless @method_node.body
    @nested_local_methods = []
    return
  end

  method_body = get_method_body(@method_node)

  call_nodes = parse_body_call_objects(method_body)
  call_expressions = parse_body_call_expressions(method_body)
  call_node_names = call_nodes.map(&:name) + call_expressions
 
  @nested_local_methods = local_methods.select{ call_node_names.include?(_1.name) }
  @nested_local_methods.each{ _1.parse_nested_local_methods(local_methods) }
end

#parse_raised_class_name(raise_node) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 65

def parse_raised_class_name(raise_node)
  return unless raise_node.arguments

  if raise_node.arguments.arguments.first.is_a?(Prism::ConstantReadNode)
    raise_node.arguments.arguments.first.name
  elsif raise_node.arguments.arguments.first.is_a?(Prism::CallNode)
    raise_node.arguments.arguments.first.receiver.name
  else
    nil
  end
end

#raised_errorsObject



56
57
58
59
60
61
62
63
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 56

def raised_errors
  return @raised_errors if @raised_errors
  return [] unless @method_node.body

  call_objects = parse_body_call_objects(get_method_body(@method_node))
  raise_objects = call_objects.select{ _1.name == :raise }
  @raised_errors = raise_objects.map{ parse_raised_class_name(_1) }.compact
end

#raised_errors_nestedObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 45

def raised_errors_nested
  return @raised_errors_nested if @raised_errors_nested
  raised = raised_errors
  
  @nested_local_methods.each do |nested_method|
    raised += nested_method.raised_errors_nested
  end

  @raised_errors_nested = raised
end

#start_lineObject



29
30
31
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 29

def start_line
  @method_node.location.start_line - 1
end

#throw_arguments_end_lineObject



92
93
94
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 92

def throw_arguments_end_line
  @contract_node.arguments.arguments.last.location.end_line - 1
end

#throw_arguments_end_positionObject



88
89
90
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 88

def throw_arguments_end_position
  @contract_node.arguments.arguments.last.location.end_column - 1
end

#throws_errorsObject



77
78
79
80
81
82
# File 'lib/ruby_lsp/ruby_lsp_ree/parsing/parsed_method_node.rb', line 77

def throws_errors
  return [] unless has_contract?
  return [] unless has_throw_section?

  @contract_node.arguments.arguments.map{ _1.name }
end