Module: PlatformosCheck::LanguageServer::VariableLookupTraverser

Extended by:
VariableLookupTraverser
Included in:
VariableLookupTraverser
Defined in:
lib/platformos_check/language_server/variable_lookup_traverser.rb

Instance Method Summary collapse

Instance Method Details

#find_object(object_name) ⇒ Object



63
64
65
66
67
# File 'lib/platformos_check/language_server/variable_lookup_traverser.rb', line 63

def find_object(object_name)
  PlatformosLiquid::SourceIndex
    .objects
    .find { |entry| entry.name == object_name }
end

#find_object_and_generic_type(potential_lookup) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/platformos_check/language_server/variable_lookup_traverser.rb', line 27

def find_object_and_generic_type(potential_lookup)
  generic_type = nil
  object = find_object(potential_lookup.name)

  # Objects like 'product' are a complex structure with fields
  # and their return type is not present.
  #
  # However, we also handle objects that have simple built-in types,
  # like 'current_tags', which is an 'array'. So, we follow them until
  # the source type:
  while object&.return_type
    generic_type = generic_type(object) if generic_type?(object)
    object = find_object(object.return_type)
  end

  [object, generic_type]
end

#find_property(object, property_name) ⇒ Object



57
58
59
60
61
# File 'lib/platformos_check/language_server/variable_lookup_traverser.rb', line 57

def find_property(object, property_name)
  object
    &.properties
    &.find { |property| property.name == property_name }
end

#generic_type(object) ⇒ Object

Currently, we’re handling generic types only for arrays, so we get the array type



47
48
49
# File 'lib/platformos_check/language_server/variable_lookup_traverser.rb', line 47

def generic_type(object)
  object.array_type
end

#generic_type?(object) ⇒ Boolean

Currently, we’re handling generic types only for arrays, so we check if it’s an array type

Returns:

  • (Boolean)


53
54
55
# File 'lib/platformos_check/language_server/variable_lookup_traverser.rb', line 53

def generic_type?(object)
  object.array_type?
end

#lookup_object_and_property(potential_lookup) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/platformos_check/language_server/variable_lookup_traverser.rb', line 8

def lookup_object_and_property(potential_lookup)
  object, generic_type = find_object_and_generic_type(potential_lookup)
  property = nil

  potential_lookup.lookups.each do |name|
    prop = find_property(object, name)

    next unless prop

    generic_type = generic_type(prop) if generic_type?(prop)

    property = prop
    property.return_type = generic_type if prop.generic_type?
    object = find_object(prop.return_type)
  end

  [object, property]
end