Sha256: f9bd8039ffae2840111b12b067862dd4f8f64b4adf8268b109fc3320f0541ab6

Contents?: true

Size: 1.76 KB

Versions: 11

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

          content + end_tag
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
platformos-check-0.4.0 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.3.3 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.3.1 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.3.0 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.2.2 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.2.1 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.2.0 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.1.0 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.0.3 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.0.2 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb
platformos-check-0.0.1 lib/platformos_check/language_server/variable_lookup_finder/assignments_finder.rb