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 LiquidCheck

LiquidCheck::ATTR, LiquidCheck::HTML_ATTRIBUTE, LiquidCheck::HTML_ATTRIBUTES, LiquidCheck::QUOTED_LIQUID_ATTRIBUTE, LiquidCheck::START_OR_END_QUOTE, LiquidCheck::TAG, 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

#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



43
44
45
# File 'lib/theme_check/checks/space_inside_braces.rb', line 43

def after_tag(_node)
  @ignore = false
end

#on_node(node) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# 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(/([,:])\S/) do |_match|
      add_offense("Space missing after '#{Regexp.last_match(1)}'", node: node, markup: Regexp.last_match(0))
    end
  end
end

#on_tag(node) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/theme_check/checks/space_inside_braces.rb', line 27

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/theme_check/checks/space_inside_braces.rb', line 47

def on_variable(node)
  return if @ignore
  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