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, Check::SEVERITY_VALUES

Instance Attribute Summary

Attributes inherited from Check

#ignored_patterns, #offenses, #options, #theme

Instance Method Summary collapse

Methods included from ChecksTracking

#inherited

Methods included from ParsingHelpers

#outside_of_strings

Methods inherited from Check

#==, #add_offense, all, can_disable, #can_disable?, categories, #categories, category, #code_name, doc, #doc, docs_url, #ignore!, #ignored?, #severity, severity, #severity=, #severity_value, severity_value, single_file, #single_file?, #to_s, #whole_theme?

Methods included from JsonHelpers

#format_json_parse_error

Constructor Details

#initializeUnusedSnippet

Returns a new instance of UnusedSnippet.



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

def initialize
  @used_templates = Set.new
end

Instance Method Details

#missing_snippetsObject



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

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

#on_endObject



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

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



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

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