Sha256: fba9e7ffae9304c37d044e13edf59b9cdd37096b7469c8ba5d96cd25d09ef718

Contents?: true

Size: 820 Bytes

Versions: 10

Compression:

Stored size: 820 Bytes

Contents

# frozen_string_literal: true
require "set"

module ThemeCheck
  class UnusedSnippet < LiquidCheck
    severity :suggestion
    category :liquid

    def initialize
      @used_templates = Set.new
    end

    def on_include(node)
      if node.value.template_name_expr.is_a?(String)
        @used_templates << "snippets/#{node.value.template_name_expr}"
      else
        # Can't reliably track unused snippets if an expression is used, ignore this check
        @used_templates.clear
        ignore!
      end
    end
    alias_method :on_render, :on_include

    def on_end
      missing_snippets.each do |template|
        add_offense("This template is not used", template: template)
      end
    end

    def missing_snippets
      theme.snippets.reject { |t| @used_templates.include?(t.name) }
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
theme-check-0.4.0 lib/theme_check/checks/unused_snippet.rb
theme-check-0.3.3 lib/theme_check/checks/unused_snippet.rb
theme-check-0.3.2 lib/theme_check/checks/unused_snippet.rb
theme-check-0.3.1 lib/theme_check/checks/unused_snippet.rb
theme-check-0.3.0 lib/theme_check/checks/unused_snippet.rb
theme-check-0.2.2 lib/theme_check/checks/unused_snippet.rb
theme-check-0.2.0 lib/theme_check/checks/unused_snippet.rb
theme-check-0.1.2 lib/theme_check/checks/unused_snippet.rb
theme-check-0.1.1 lib/theme_check/checks/unused_snippet.rb
theme-check-0.1.0 lib/theme_check/checks/unused_snippet.rb