Class: PlatformosCheck::UnusedPartial

Inherits:
LiquidCheck show all
Defined in:
lib/platformos_check/checks/unused_partial.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, #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

#initializeUnusedPartial

Returns a new instance of UnusedPartial.



9
10
11
# File 'lib/platformos_check/checks/unused_partial.rb', line 9

def initialize
  @used_partials = Set.new
end

Instance Method Details

#missing_partialsObject



48
49
50
# File 'lib/platformos_check/checks/unused_partial.rb', line 48

def missing_partials
  platformos_app.partials.reject { |t| @used_partials.include?(t.name) }
end

#on_endObject



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

def on_end
  missing_partials.each do |app_file|
    add_offense("This partial is not used", app_file:) do |corrector|
      corrector.remove_file(@platformos_app.storage, app_file.relative_path.to_s)
    end
  end
end

#on_function(node) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/platformos_check/checks/unused_partial.rb', line 30

def on_function(node)
  if node.value.from.is_a?(String)
    @used_partials << node.value.from
  else
    # Can't reliably track unused partials if an expression is used, ignore this check
    @used_partials.clear
    ignore!
  end
end

#on_render(node) ⇒ Object Also known as: on_include



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/platformos_check/checks/unused_partial.rb', line 13

def on_render(node)
  if node.value.template_name_expr.is_a?(String)
    @used_partials << 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 UnusedPartial check

  else
    # Can't reliably track unused partials if an expression is used, ignore this check
    @used_partials.clear
    ignore!
  end
end