Class: ThemeCheck::SpaceInsideBraces

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

Overview

Ensure … % & … } have consistent spaces.

Constant Summary

Constants inherited from Check

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

Instance Attribute Summary

Attributes inherited from Check

#ignored_patterns, #offenses, #options, #theme

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_theme?

Methods included from JsonHelpers

#format_json_parse_error

Constructor Details

#initializeSpaceInsideBraces

Returns a new instance of SpaceInsideBraces.



9
10
11
# File 'lib/theme_check/checks/space_inside_braces.rb', line 9

def initialize
  @ignore = false
end

Instance Method Details

#after_tag(_node) ⇒ Object



49
50
51
# File 'lib/theme_check/checks/space_inside_braces.rb', line 49

def after_tag(_node)
  @ignore = false
end

#on_node(node) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/theme_check/checks/space_inside_braces.rb', line 13

def on_node(node)
  return unless node.markup
  return if :assign == node.type_name

  outside_of_strings(node.markup) do |chunk|
    chunk.scan(/([,:|]|==|<>|<=|>=|<|>|!=)  +/) do |_match|
      add_offense("Too many spaces after '#{Regexp.last_match(1)}'", node: node, markup: Regexp.last_match(0))
    end
    chunk.scan(/([,:|]|==|<>|<=|>=|<\b|>\b|!=)(\S|\z)/) do |_match|
      add_offense("Space missing after '#{Regexp.last_match(1)}'", node: node, markup: Regexp.last_match(0))
    end
    chunk.scan(/  (\||==|<>|<=|>=|<|>|!=)+/) do |_match|
      add_offense("Too many spaces before '#{Regexp.last_match(1)}'", node: node, markup: Regexp.last_match(0))
    end
    chunk.scan(/(\A|\S)(?<match>\||==|<>|<=|>=|<|\b>|!=)/) do |_match|
      add_offense("Space missing before '#{Regexp.last_match(1)}'", node: node, markup: Regexp.last_match(0))
    end
  end
end

#on_tag(node) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/theme_check/checks/space_inside_braces.rb', line 33

def on_tag(node)
  if node.inside_liquid_tag?
    markup = if node.whitespace_trimmed?
      "-%}"
    else
      "%}"
    end
    if node.markup[-1] != " " && node.markup[-1] != "\n"
      add_offense("Space missing before '#{markup}'", node: node, markup: node.markup[-1] + markup)
    elsif node.markup =~ /(\n?)(  +)\z/m && Regexp.last_match(1) != "\n"
      add_offense("Too many spaces before '#{markup}'", node: node, markup: Regexp.last_match(2) + markup)
    end
  end
  @ignore = true
end

#on_variable(node) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/theme_check/checks/space_inside_braces.rb', line 53

def on_variable(node)
  return if @ignore || node.markup.empty?
  if node.markup[0] != " "
    add_offense("Space missing after '{{'", node: node) do |corrector|
      corrector.insert_before(node, " ")
    end
  end
  if node.markup[-1] != " "
    add_offense("Space missing before '}}'", node: node) do |corrector|
      corrector.insert_after(node, " ")
    end
  end
  if node.markup[0] == " " && node.markup[1] == " "
    add_offense("Too many spaces after '{{'", node: node) do |corrector|
      corrector.replace(node, " #{node.markup.lstrip}")
    end
  end
  if node.markup[-1] == " " && node.markup[-2] == " "
    add_offense("Too many spaces before '}}'", node: node) do |corrector|
      corrector.replace(node, "#{node.markup.rstrip} ")
    end
  end
end