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



74
75
76
# File 'lib/theme_check/checks/space_inside_braces.rb', line 74

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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_start|
    chunk.scan(/([,:|]|==|<>|<=|>=|<|>|!=)(  +)/) do |_match|
      add_offense(
        "Too many spaces after '#{Regexp.last_match(1)}'",
        node: node,
        markup: Regexp.last_match(2),
        node_markup_offset: chunk_start + Regexp.last_match.begin(2)
      )
    end
    chunk.scan(/([,:|]|==|<>|<=|>=|<\b|>\b|!=)(\S|\z)/) do |_match|
      add_offense(
        "Space missing after '#{Regexp.last_match(1)}'",
        node: node,
        markup: Regexp.last_match(1),
        node_markup_offset: chunk_start + Regexp.last_match.begin(0),
      )
    end
    chunk.scan(/(  +)(\||==|<>|<=|>=|<|>|!=)+/) do |_match|
      add_offense(
        "Too many spaces before '#{Regexp.last_match(2)}'",
        node: node,
        markup: Regexp.last_match(1),
        node_markup_offset: chunk_start + Regexp.last_match.begin(1)
      )
    end
    chunk.scan(/(\A|\S)(?<match>\||==|<>|<=|>=|<|\b>|!=)/) do |_match|
      add_offense(
        "Space missing before '#{Regexp.last_match(1)}'",
        node: node,
        markup: Regexp.last_match(:match),
        node_markup_offset: chunk_start + Regexp.last_match.begin(:match)
      )
    end
  end
end

#on_tag(node) ⇒ Object



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

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

#on_variable(node) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/theme_check/checks/space_inside_braces.rb', line 78

def on_variable(node)
  return if @ignore || node.markup.empty?
  if node.markup[0] != " "
    add_offense(
      "Space missing after '#{node.start_token}'",
      node: node,
      markup: node.markup[0]
    ) do |corrector|
      corrector.insert_before(node, " ")
    end
  end
  if node.markup[-1] != " " && node.markup[-1] != "\n"
    add_offense(
      "Space missing before '#{node.end_token}'",
      node: node,
      markup: node.markup[-1],
      node_markup_offset: node.markup.size - 1,
    ) do |corrector|
      corrector.insert_after(node, " ")
    end
  end
  if node.markup =~ /\A(  +)/m
    add_offense(
      "Too many spaces after '#{node.start_token}'",
      node: node,
      markup: Regexp.last_match(1),
    ) do |corrector|
      corrector.replace(node, " #{node.markup.lstrip}")
    end
  end
  if node.markup =~ /(\n?)(  +)\z/m && Regexp.last_match(1) != "\n"
    add_offense(
      "Too many spaces before '#{node.end_token}'",
      node: node,
      markup: Regexp.last_match(2),
      node_markup_offset: node.markup.size - Regexp.last_match(2).size
    ) do |corrector|
      corrector.replace(node, "#{node.markup.rstrip} ")
    end
  end
end