Sha256: ab16d61b785be86fb60c76a1142c8878837abeabde9a9e3065fc9a61b997d895

Contents?: true

Size: 1.76 KB

Versions: 14

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

module PlatformosCheck
  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)
          LiquidFile.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 = VARIABLE_START.match?(lines.last) ? ' }}' : ' %}'

          content + end_tag
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
platformos-check-0.4.14 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.4.13 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.4.12 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.4.11 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.4.10 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.4.9 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.4.8 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.4.7 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.4.6 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.4.5 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.4.4 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.4.3 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.4.2 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.4.1 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb