Class: ThemeCheck::UnusedAssign

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

Overview

Checks unused assign x = … %

Defined Under Namespace

Classes: TemplateInfo

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

#initializeUnusedAssign

Returns a new instance of UnusedAssign.



21
22
23
# File 'lib/theme_check/checks/unused_assign.rb', line 21

def initialize
  @templates = {}
end

Instance Method Details

#on_assign(node) ⇒ Object



29
30
31
# File 'lib/theme_check/checks/unused_assign.rb', line 29

def on_assign(node)
  @templates[node.template.name].assign_nodes[node.value.to] = node
end

#on_document(node) ⇒ Object



25
26
27
# File 'lib/theme_check/checks/unused_assign.rb', line 25

def on_document(node)
  @templates[node.template.name] = TemplateInfo.new(Set.new, {}, Set.new)
end

#on_endObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/theme_check/checks/unused_assign.rb', line 43

def on_end
  @templates.each_pair do |_, info|
    used = info.collect_used_assigns(@templates)
    info.assign_nodes.each_pair do |name, node|
      unless used.include?(name)
        add_offense("`#{name}` is never used", node: node)
      end
    end
  end
end

#on_include(node) ⇒ Object



33
34
35
36
37
# File 'lib/theme_check/checks/unused_assign.rb', line 33

def on_include(node)
  if node.value.template_name_expr.is_a?(String)
    @templates[node.template.name].includes << "snippets/#{node.value.template_name_expr}"
  end
end

#on_variable_lookup(node) ⇒ Object



39
40
41
# File 'lib/theme_check/checks/unused_assign.rb', line 39

def on_variable_lookup(node)
  @templates[node.template.name].used_assigns << node.value.name
end