Sha256: 8c21faf91fc364a7ed842eaee97e669120a6da5cc3f83bc4453e1adc0ec38078

Contents?: true

Size: 1.75 KB

Versions: 4

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

module ThemeCheck
  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 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

4 entries across 4 versions & 1 rubygems

Version Path
theme-check-1.15.0 lib/theme_check/language_server/variable_lookup_finder/assignments_finder/scope.rb
theme-check-1.14.0 lib/theme_check/language_server/variable_lookup_finder/assignments_finder/scope.rb
theme-check-1.13.0 lib/theme_check/language_server/variable_lookup_finder/assignments_finder/scope.rb
theme-check-1.12.1 lib/theme_check/language_server/variable_lookup_finder/assignments_finder/scope.rb