Class: PlatformosCheck::UnusedAssign

Inherits:
LiquidCheck show all
Defined in:
lib/platformos_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, Check::SEVERITY_VALUES

Instance Attribute Summary

Attributes inherited from Check

#ignored_patterns, #offenses, #options, #platformos_app

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_platformos_app?

Methods included from JsonHelpers

#format_json_parse_error, #pretty_json

Constructor Details

#initializeUnusedAssign

Returns a new instance of UnusedAssign.



24
25
26
# File 'lib/platformos_check/checks/unused_assign.rb', line 24

def initialize
  @templates = {}
end

Instance Method Details

#on_assign(node) ⇒ Object



32
33
34
# File 'lib/platformos_check/checks/unused_assign.rb', line 32

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

#on_document(node) ⇒ Object



28
29
30
# File 'lib/platformos_check/checks/unused_assign.rb', line 28

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

#on_endObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/platformos_check/checks/unused_assign.rb', line 67

def on_end
  @templates.each_pair do |_, info|
    used = info.collect_used_assigns(@templates)
    info.assign_nodes.each_pair do |name, node|
      next if used.include?(name)

      add_offense("`#{name}` is never used", node:) do |corrector|
        case node.type_name
        when :graphql
          offset = node.markup.match(/^graphql\s+/)[0].size

          corrector.insert_before(
            node,
            '_',
            (node.start_index + offset)...(node.start_index + offset)
          )
        when :function
          offset = node.markup.match(/^function\s+/)[0].size

          corrector.insert_before(
            node,
            '_',
            (node.start_index + offset)...(node.start_index + offset)
          )
        when :parse_json
          # noop
        else
          corrector.remove(node)
        end
      end
    end
  end
end

#on_function(node) ⇒ Object



40
41
42
43
44
# File 'lib/platformos_check/checks/unused_assign.rb', line 40

def on_function(node)
  return if node.value.to.start_with?('_')

  @templates[node.app_file.name].assign_nodes[node.value.to] = node
end

#on_graphql(node) ⇒ Object



46
47
48
49
50
# File 'lib/platformos_check/checks/unused_assign.rb', line 46

def on_graphql(node)
  return if node.value.to.start_with?('_')

  @templates[node.app_file.name].assign_nodes[node.value.to] = node
end

#on_include(node) ⇒ Object



52
53
54
55
56
# File 'lib/platformos_check/checks/unused_assign.rb', line 52

def on_include(node)
  return unless node.value.template_name_expr.is_a?(String)

  @templates[node.app_file.name].includes << node.value.template_name_expr
end

#on_parse_json(node) ⇒ Object



36
37
38
# File 'lib/platformos_check/checks/unused_assign.rb', line 36

def on_parse_json(node)
  @templates[node.app_file.name].assign_nodes[node.value.to] = node
end

#on_variable_lookup(node) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/platformos_check/checks/unused_assign.rb', line 58

def on_variable_lookup(node)
  @templates[node.app_file.name].used_assigns << case node.value.name
                                                 when Liquid::VariableLookup
                                                   node.value.name.name
                                                 else
                                                   node.value.name
                                                 end
end