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 collapse

TAGS_FOR_AUTO_VARIABLE_PREPEND =
Set.new(%i[graphql function background]).freeze
FILTERS_THAT_MODIFY_OBJECT =
Set.new(%w[array_add add_to_array
prepend_to_array array_prepend
assign_to_hash_key hash_add_key add_hash_key
remove_hash_key hash_delete_key delete_hash_key]).freeze
PREPEND_CHARACTER =
'_'

Constants inherited from Check

Check::CATEGORIES, Check::SEVERITIES, Check::SEVERITY_VALUES

Instance Attribute Summary

Attributes inherited from Check

#ignored_patterns, #offenses, #options, #platformos_app

Class Method Summary collapse

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?, #to_s, #whole_platformos_app?

Methods included from JsonHelpers

#format_json_parse_error, #pretty_json

Constructor Details

#initializeUnusedAssign

Returns a new instance of UnusedAssign.



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

def initialize
  @templates = {}
end

Class Method Details

.single_file(**_args) ⇒ Object



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

def self.single_file(**_args)
  true
end

Instance Method Details

#on_assign(node) ⇒ Object



43
44
45
46
47
48
# File 'lib/platformos_check/checks/unused_assign.rb', line 43

def on_assign(node)
  return if ignore_prepended?(node)
  return if node.value.from.filters.any? { |filter_name, *_arguments| FILTERS_THAT_MODIFY_OBJECT.include?(filter_name) }

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

#on_background(node) ⇒ Object



67
68
69
70
71
72
# File 'lib/platformos_check/checks/unused_assign.rb', line 67

def on_background(node)
  return if node.value.to.nil?
  return if ignore_prepended?(node)

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

#on_document(node) ⇒ Object



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

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

#on_endObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/platformos_check/checks/unused_assign.rb', line 89

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|
        if TAGS_FOR_AUTO_VARIABLE_PREPEND.include?(node.type_name)
          prepend_variable(node, corrector)
        else
          corrector.remove(node)
        end
      end
    end
  end
end

#on_function(node) ⇒ Object



54
55
56
57
58
# File 'lib/platformos_check/checks/unused_assign.rb', line 54

def on_function(node)
  return if ignore_prepended?(node)

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

#on_graphql(node) ⇒ Object



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

def on_graphql(node)
  return if node.value.to.nil?
  return if ignore_prepended?(node)

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

#on_include(node) ⇒ Object



74
75
76
77
78
# File 'lib/platformos_check/checks/unused_assign.rb', line 74

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



50
51
52
# File 'lib/platformos_check/checks/unused_assign.rb', line 50

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

#on_variable_lookup(node) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/platformos_check/checks/unused_assign.rb', line 80

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