Class: ThemeCheck::LiquidTag

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

Overview

Recommends using liquid … % if 4 or more consecutive … % are found.

Constant Summary

Constants inherited from LiquidCheck

ThemeCheck::LiquidCheck::ATTR, ThemeCheck::LiquidCheck::HTML_ATTRIBUTE, ThemeCheck::LiquidCheck::HTML_ATTRIBUTES, ThemeCheck::LiquidCheck::QUOTED_LIQUID_ATTRIBUTE, ThemeCheck::LiquidCheck::START_OR_END_QUOTE, ThemeCheck::LiquidCheck::TAG, ThemeCheck::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(min_consecutive_statements: 4) ⇒ LiquidTag

Returns a new instance of LiquidTag.



9
10
11
12
13
# File 'lib/theme_check/checks/liquid_tag.rb', line 9

def initialize(min_consecutive_statements: 4)
  @first_statement = nil
  @consecutive_statements = 0
  @min_consecutive_statements = min_consecutive_statements
end

Instance Method Details

#after_document(_node) ⇒ Object



31
32
33
# File 'lib/theme_check/checks/liquid_tag.rb', line 31

def after_document(_node)
  reset_consecutive_statements
end

#increment_consecutive_statements(node) ⇒ Object



35
36
37
38
# File 'lib/theme_check/checks/liquid_tag.rb', line 35

def increment_consecutive_statements(node)
  @first_statement ||= node
  @consecutive_statements += 1
end

#on_string(node) ⇒ Object



24
25
26
27
28
29
# File 'lib/theme_check/checks/liquid_tag.rb', line 24

def on_string(node)
  # Only reset the counter on outputted strings, and ignore empty line-breaks
  if node.parent.block? && !node.value.strip.empty?
    reset_consecutive_statements
  end
end

#on_tag(node) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/theme_check/checks/liquid_tag.rb', line 15

def on_tag(node)
  if !node.inside_liquid_tag?
    reset_consecutive_statements
  # Ignore comments
  elsif !node.comment?
    increment_consecutive_statements(node)
  end
end

#reset_consecutive_statementsObject



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

def reset_consecutive_statements
  if @consecutive_statements >= @min_consecutive_statements
    add_offense("Use {% liquid ... %} to write multiple tags", node: @first_statement)
  end
  @first_statement = nil
  @consecutive_statements = 0
end