Class: PlatformosCheck::LiquidTag

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

Overview

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

Constant Summary

Constants inherited from Check

Check::CATEGORIES, Check::SEVERITIES, Check::SEVERITY_VALUES

Instance Attribute Summary

Attributes inherited from Check

#ignored_patterns, #offenses, #options, #platformos_app

Instance Method Summary collapse

Methods included from ChecksTracking

#inherited

Methods included from ParsingHelpers

#outside_of_strings

Methods inherited from Check

#==, #add_offense, all, can_disable, #can_disable?, categories, #categories, category, #code_name, doc, #doc, docs_url, #ignore!, #ignored?, #severity, severity, #severity=, #severity_value, severity_value, single_file, #single_file?, #to_s, #whole_platformos_app?

Methods included from JsonHelpers

#format_json_parse_error, #pretty_json

Constructor Details

#initialize(min_consecutive_statements: 5) ⇒ LiquidTag

Returns a new instance of LiquidTag.



10
11
12
13
14
# File 'lib/platformos_check/checks/liquid_tag.rb', line 10

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

Instance Method Details

#after_document(_node) ⇒ Object



32
33
34
# File 'lib/platformos_check/checks/liquid_tag.rb', line 32

def after_document(_node)
  reset_consecutive_statements
end

#increment_consecutive_statements(node) ⇒ Object



36
37
38
39
# File 'lib/platformos_check/checks/liquid_tag.rb', line 36

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

#on_string(node) ⇒ Object



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

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

  reset_consecutive_statements
end

#on_tag(node) ⇒ Object



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

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



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

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