Class: ThemeCheck::UnusedSnippet
- Inherits:
-
LiquidCheck
- Object
- Check
- LiquidCheck
- ThemeCheck::UnusedSnippet
- 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
-
#initialize ⇒ UnusedSnippet
constructor
A new instance of UnusedSnippet.
- #missing_snippets ⇒ Object
- #on_end ⇒ Object
- #on_render(node) ⇒ Object (also: #on_include)
Methods included from ChecksTracking
Methods included from ParsingHelpers
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, #pretty_json
Constructor Details
#initialize ⇒ UnusedSnippet
Returns a new instance of UnusedSnippet.
11 12 13 |
# File 'lib/theme_check/checks/unused_snippet.rb', line 11 def initialize @used_snippets = Set.new end |
Instance Method Details
#missing_snippets ⇒ Object
40 41 42 |
# File 'lib/theme_check/checks/unused_snippet.rb', line 40 def missing_snippets theme.snippets.reject { |t| @used_snippets.include?(t.name) } end |
#on_end ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/theme_check/checks/unused_snippet.rb', line 32 def on_end missing_snippets.each do |theme_file| add_offense("This snippet is not used", theme_file: theme_file) do |corrector| corrector.remove_file(@theme.storage, theme_file.relative_path.to_s) end end end |
#on_render(node) ⇒ Object Also known as: on_include
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/theme_check/checks/unused_snippet.rb', line 15 def on_render(node) if node.value.template_name_expr.is_a?(String) @used_snippets << "snippets/#{node.value.template_name_expr}" elsif might_have_a_block_as_variable_lookup?(node) # We ignore this case, because that's a "proper" use case for # the render tag with OS 2.0 # {% render block %} shouldn't turn off the UnusedSnippet check else # Can't reliably track unused snippets if an expression is used, ignore this check @used_snippets.clear ignore! end end |