Sha256: 30cc40a538ecb71f5503fb51523e72d6f425e24d4915570f7938f7054c989a40

Contents?: true

Size: 847 Bytes

Versions: 23

Compression:

Stored size: 847 Bytes

Contents

# frozen_string_literal: true
require "set"

module ThemeCheck
  class UnusedSnippet < LiquidCheck
    severity :suggestion
    category :liquid
    doc docs_url(__FILE__)

    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

23 entries across 23 versions & 1 rubygems

Version Path
theme-check-1.5.2 lib/theme_check/checks/unused_snippet.rb
theme-check-1.5.1 lib/theme_check/checks/unused_snippet.rb
theme-check-1.5.0 lib/theme_check/checks/unused_snippet.rb
theme-check-1.4.0 lib/theme_check/checks/unused_snippet.rb
theme-check-1.3.0 lib/theme_check/checks/unused_snippet.rb
theme-check-1.2.0 lib/theme_check/checks/unused_snippet.rb
theme-check-1.1.0 lib/theme_check/checks/unused_snippet.rb
theme-check-1.0.0 lib/theme_check/checks/unused_snippet.rb
theme-check-0.10.2 lib/theme_check/checks/unused_snippet.rb
theme-check-0.10.1 lib/theme_check/checks/unused_snippet.rb
theme-check-0.10.0 lib/theme_check/checks/unused_snippet.rb
theme-check-0.9.1 lib/theme_check/checks/unused_snippet.rb
theme-check-0.9.0 lib/theme_check/checks/unused_snippet.rb
theme-check-0.8.3 lib/theme_check/checks/unused_snippet.rb
theme-check-0.8.2 lib/theme_check/checks/unused_snippet.rb
theme-check-0.8.1 lib/theme_check/checks/unused_snippet.rb
theme-check-0.8.0 lib/theme_check/checks/unused_snippet.rb
theme-check-0.7.3 lib/theme_check/checks/unused_snippet.rb
theme-check-0.7.2 lib/theme_check/checks/unused_snippet.rb
theme-check-0.7.1 lib/theme_check/checks/unused_snippet.rb