Class: ThemeCheck::UnusedSnippet

Inherits:
LiquidCheck show all
Defined in:
lib/theme_check/checks/unused_snippet.rb

Constant Summary

Constants inherited from Check

Check::CATEGORIES, Check::SEVERITIES

Instance Attribute Summary

Attributes inherited from Check

#offenses, #options, #theme

Instance Method Summary collapse

Methods inherited from LiquidCheck

#add_offense

Methods included from ChecksTracking

#inherited

Methods included from ParsingHelpers

#outside_of_strings

Methods inherited from Check

all, category, #category, #code_name, doc, #doc, #ignore!, #ignored?, severity, #severity, #to_s, #unignore!

Methods included from JsonHelpers

#format_json_parse_error

Constructor Details

#initializeUnusedSnippet

Returns a new instance of UnusedSnippet.



9
10
11
# File 'lib/theme_check/checks/unused_snippet.rb', line 9

def initialize
  @used_templates = Set.new
end

Instance Method Details

#missing_snippetsObject



30
31
32
# File 'lib/theme_check/checks/unused_snippet.rb', line 30

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

#on_endObject



24
25
26
27
28
# File 'lib/theme_check/checks/unused_snippet.rb', line 24

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

#on_include(node) ⇒ Object Also known as: on_render



13
14
15
16
17
18
19
20
21
# File 'lib/theme_check/checks/unused_snippet.rb', line 13

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