Sha256: aa87dcfe2df6c947930eb8817ddeb5c09ce36c20284b9e981e784fa51e1ffc2c

Contents?: true

Size: 1.75 KB

Versions: 5

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

module ThemeCheck
  module LanguageServer
    module VariableLookupFinder
      class AssignmentsFinder
        include RegexHelpers

        attr_reader :content, :scope_visitor

        def initialize(content)
          @content = close_tag(content)
          @scope_visitor = ScopeVisitor.new
        end

        def find!
          template = parse(content)

          if template
            visit_template(template)
            return
          end

          liquid_tags.each do |tag|
            visit_template(last_line_parse(tag))
          end
        end

        def assignments
          current_scope = scope_visitor.current_scope
          current_scope.variables
        end

        private

        def visit_template(template)
          scope_visitor.visit_template(template)
        end

        def liquid_tags
          matches(content, LIQUID_TAG_OR_VARIABLE)
            .flat_map { |match| match[0] }
        end

        def parse(content)
          regular_parse(content) || tolerant_parse(content)
        end

        def regular_parse(content)
          Liquid::Template.parse(content)
        rescue Liquid::SyntaxError
          # Ignore syntax errors at the regular parse phase
        end

        def tolerant_parse(content)
          TolerantParser::Template.parse(content)
        rescue StandardError
          # Ignore any error at the tolerant parse phase
        end

        def last_line_parse(content)
          parsable_content = LiquidFixer.new(content).parsable

          regular_parse(parsable_content)
        end

        def close_tag(content)
          lines = content.lines
          end_tag = lines.last =~ VARIABLE_START ? ' }}' : ' %}'

          content + end_tag
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

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