forked from Shopify/theme-check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnested_snippet.rb
47 lines (42 loc) · 1.29 KB
/
nested_snippet.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true
module ThemeCheck
# Reports deeply nested {% include ... %} or {% render ... %}
class NestedSnippet < LiquidCheck
severity :suggestion
category :liquid
doc docs_url(__FILE__)
class TemplateInfo < Struct.new(:includes)
def with_deep_nested(templates, max, current_level = 0)
includes.each do |node|
if current_level >= max
yield node
else
template_name = "snippets/#{node.value.template_name_expr}"
templates[template_name]
&.with_deep_nested(templates, max, current_level + 1) { yield node }
end
end
end
end
def initialize(max_nesting_level: 3)
@max_nesting_level = max_nesting_level
@templates = {}
end
def on_document(node)
@templates[node.theme_file.name] = TemplateInfo.new(Set.new)
end
def on_include(node)
if node.value.template_name_expr.is_a?(String)
@templates[node.theme_file.name].includes << node
end
end
alias_method :on_render, :on_include
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
end
end