Class: PlatformosCheck::LanguageServer::VariableLookupFinder::AssignmentsFinder::NodeHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/platformos_check/language_server/variable_lookup_finder/assignments_finder/node_handler.rb

Instance Method Summary collapse

Instance Method Details

#on_assign(node, scope) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/platformos_check/language_server/variable_lookup_finder/assignments_finder/node_handler.rb', line 8

def on_assign(node, scope)
  # When a variable is redefined in a new scope we
  # no longer can guarantee the type in the global scope
  #
  # Example:
  # ```liquid
  # {%- liquid
  #   assign var1 = some_value
  #
  #   if condition
  #     assign var1 = another_value
  #            ^^^^ from here we no longer can guarantee
  #                 the type of `var1` in the global scope
  # -%}
  # ```
  p_scope = scope
  while (p_scope = p_scope.parent)
    p_scope.variables.delete(node.value.to)
  end

  scope << node
  scope
end

#on_block_body(node, scope) ⇒ Object

Define a new scope every time a new block is created



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/platformos_check/language_server/variable_lookup_finder/assignments_finder/node_handler.rb', line 52

def on_block_body(node, scope)
  scope = scope.new_child

  ##
  # 'for' tags handle blocks flattenly and differently
  # than the other tags (if, unless, case).
  #
  # The scope of 'for' tags exists only in the first
  # block, as the following one refers to the else
  # statement of the iteration.
  parent = node.parent

  scope << parent if parent.type_name == :for && parent.children.first == node
  scope
end

#on_function(node, scope) ⇒ Object



32
33
34
# File 'lib/platformos_check/language_server/variable_lookup_finder/assignments_finder/node_handler.rb', line 32

def on_function(node, scope)
  on_assign(node, scope)
end

#on_graphql(node, scope) ⇒ Object



36
37
38
# File 'lib/platformos_check/language_server/variable_lookup_finder/assignments_finder/node_handler.rb', line 36

def on_graphql(node, scope)
  on_assign(node, scope)
end

#on_table_row(node, scope) ⇒ Object

Table row tags do not rely on blocks to define scopes, so we index their value here



43
44
45
46
47
48
# File 'lib/platformos_check/language_server/variable_lookup_finder/assignments_finder/node_handler.rb', line 43

def on_table_row(node, scope)
  scope = scope.new_child

  scope << node
  scope
end