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 LiquidCheck

LiquidCheck::ATTR, LiquidCheck::HTML_ATTRIBUTE, LiquidCheck::HTML_ATTRIBUTES, LiquidCheck::QUOTED_LIQUID_ATTRIBUTE, LiquidCheck::START_OR_END_QUOTE, LiquidCheck::TAG, LiquidCheck::VARIABLE

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?, categories, #categories, category, #code_name, #doc, doc, docs_url, #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.



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

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

Instance Method Details

#on_document(node) ⇒ Object



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

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

#on_endObject



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

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



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

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