forked from Shopify/theme-check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_length.rb
44 lines (36 loc) · 1.12 KB
/
template_length.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
# frozen_string_literal: true
module ThemeCheck
class TemplateLength < LiquidCheck
severity :suggestion
category :liquid
doc docs_url(__FILE__)
def initialize(max_length: 600, exclude_schema: true, exclude_stylesheet: true, exclude_javascript: true)
@max_length = max_length
@exclude_schema = exclude_schema
@exclude_stylesheet = exclude_stylesheet
@exclude_javascript = exclude_javascript
end
def on_document(_node)
@excluded_lines = 0
end
def on_schema(node)
exclude_node_lines(node) if @exclude_schema
end
def on_stylesheet(node)
exclude_node_lines(node) if @exclude_stylesheet
end
def on_javascript(node)
exclude_node_lines(node) if @exclude_javascript
end
def after_document(node)
lines = node.theme_file.source.count("\n") - @excluded_lines
if lines > @max_length
add_offense("Template has too many lines [#{lines}/#{@max_length}]", theme_file: node.theme_file)
end
end
private
def exclude_node_lines(node)
@excluded_lines += node.value.nodelist.join.count("\n")
end
end
end