Sha256: ab7b9d7dc74c51108a61f53e817fd02ac6d306db12e3f226cd6313021683bfdc

Contents?: true

Size: 1.18 KB

Versions: 12

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true
module ThemeCheck
  class Visitor
    def initialize(checks)
      @checks = checks
    end

    def visit_template(template)
      @disabled_checks = DisabledChecks.new
      visit(Node.new(template.root, nil, template))
    rescue Liquid::Error => exception
      exception.template_name = template.name
      call_checks(:on_error, exception)
    end

    private

    def visit(node)
      call_checks(:on_node, node)
      call_checks(:on_tag, node) if node.tag?
      call_checks(:"on_#{node.type_name}", node)
      node.children.each { |child| visit(child) }
      unless node.literal?
        call_checks(:"after_#{node.type_name}", node)
        call_checks(:after_tag, node) if node.tag?
        call_checks(:after_node, node)
      end

      @disabled_checks.update(node) if node.comment?
    end

    def visit_children(node)
      node.children.each { |child| visit(child) }
    end

    def call_checks(method, *args)
      checks.call(method, *args)
    end

    def checks
      return @checks unless @disabled_checks.any?

      return @checks.always_enabled if @disabled_checks.all_disabled?

      @checks.except_for(@disabled_checks)
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
theme-check-0.8.0 lib/theme_check/visitor.rb
theme-check-0.7.3 lib/theme_check/visitor.rb
theme-check-0.7.2 lib/theme_check/visitor.rb
theme-check-0.7.1 lib/theme_check/visitor.rb
theme-check-0.7.0 lib/theme_check/visitor.rb
theme-check-0.6.0 lib/theme_check/visitor.rb
theme-check-0.5.0 lib/theme_check/visitor.rb
theme-check-0.4.0 lib/theme_check/visitor.rb
theme-check-0.3.3 lib/theme_check/visitor.rb
theme-check-0.3.2 lib/theme_check/visitor.rb
theme-check-0.3.1 lib/theme_check/visitor.rb
theme-check-0.3.0 lib/theme_check/visitor.rb