Class: ThemeCheck::NestedSnippet

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

Overview

Reports deeply nested include … % or render … %

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, can_disable, #can_disable?, category, #category, #code_name, doc, #doc, #ignore!, #ignored?, #severity, severity, #to_s, #unignore!

Methods included from JsonHelpers

#format_json_parse_error

Constructor Details

#initialize(max_nesting_level: 2) ⇒ NestedSnippet

Returns a new instance of NestedSnippet.



22
23
24
25
# File 'lib/theme_check/checks/nested_snippet.rb', line 22

def initialize(max_nesting_level: 2)
  @max_nesting_level = max_nesting_level
  @templates = {}
end

Instance Method Details

#on_document(node) ⇒ Object



27
28
29
# File 'lib/theme_check/checks/nested_snippet.rb', line 27

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

#on_endObject



38
39
40
41
42
43
44
# File 'lib/theme_check/checks/nested_snippet.rb', line 38

def on_end
  @templates.each_pair do |_, info|
    info.with_deep_nested(@templates, @max_nesting_level) do |node|
      add_offense("Too many nested snippets", node: node)
    end
  end
end

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



31
32
33
34
35
# File 'lib/theme_check/checks/nested_snippet.rb', line 31

def on_include(node)
  if node.value.template_name_expr.is_a?(String)
    @templates[node.template.name].includes << node
  end
end