Sha256: 59a9271f81befdda0012d6334ce77f48edd017fabc8453233367bce60f37db35

Contents?: true

Size: 1.91 KB

Versions: 3

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

module PlatformosCheck
  module LanguageServer
    module VariableLookupFinder
      class AssignmentsFinder
        class Scope < Struct.new(:variables, :parent)
          include TypeHelper

          def new_child
            child_scope = dup
            child_scope.variables = variables.dup
            child_scope.parent = self
            child_scope
          end

          def <<(node)
            tag = node.value

            case tag
            when Liquid::Assign
              variable_name = tag.to
              variables[variable_name] = as_potential_lookup(tag.from.name)
            when Tags::Function, Tags::Graphql
              variable_name = tag.to
              variables[variable_name] = literal_lookup(tag.from)
            when Liquid::For, Liquid::TableRow
              variable_name = tag.variable_name
              variables[variable_name] = as_potential_lookup(tag.collection_name, ['first'])
            end
          end

          private

          def as_potential_lookup(variable_lookup, default_lookups = [])
            case variable_lookup
            when Liquid::VariableLookup
              potential_lookup(variable_lookup, default_lookups)
            when Liquid::RangeLookup
              as_potential_lookup(variable_lookup.start_obj)
            when Enumerable
              as_potential_lookup(variable_lookup.first)
            else
              literal_lookup(variable_lookup)
            end
          end

          def literal_lookup(variable_lookup)
            name = input_type_of(variable_lookup)
            PotentialLookup.new(name, [], variables)
          end

          def potential_lookup(variable_lookup, default_lookups)
            name = variable_lookup.name
            lookups = variable_lookup.lookups.concat(default_lookups)

            PotentialLookup.new(name, lookups, variables)
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
platformos-check-0.4.3 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder/scope.rb
platformos-check-0.4.2 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder/scope.rb
platformos-check-0.4.1 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder/scope.rb