Class: ThemeCheck::SpaceInsideBraces
Overview
Ensure … % & … } have consistent spaces.
Defined Under Namespace
Classes: BlockMarkup
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
-
#add_offense_for_match(message, match, node, source_offset, &block) ⇒ Object
-
#add_space_missing_after_offense(match, node, source_offset) ⇒ Object
-
#add_space_missing_before_offense(match, node, source_offset) ⇒ Object
-
#add_too_many_spaces_after_offense(match, node, source_offset) ⇒ Object
-
#add_too_many_spaces_before_offense(match, node, source_offset) ⇒ Object
-
#on_node(node) ⇒ Object
-
#on_tag(node) ⇒ Object
-
#on_variable(node) ⇒ Object
#inherited
#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?
#format_json_parse_error, #pretty_json
Instance Method Details
#add_offense_for_match(message, match, node, source_offset, &block) ⇒ Object
155
156
157
158
159
160
161
162
163
|
# File 'lib/theme_check/checks/space_inside_braces.rb', line 155
def add_offense_for_match(message, match, node, source_offset, &block)
add_offense(
message,
node: node,
markup: match[:offense],
node_markup_offset: source_offset + match.begin(:offense),
&block
)
end
|
#add_space_missing_after_offense(match, node, source_offset) ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/theme_check/checks/space_inside_braces.rb', line 91
def add_space_missing_after_offense(match, node, source_offset)
add_offense_for_match(
"Space missing after '#{match[:token]}'",
match,
node,
source_offset
) do |corrector|
corrector.insert_after(
node,
' ',
(node.start_index + source_offset + match.begin(:token))...
(node.start_index + source_offset + match.end(:token))
)
end
end
|
#add_space_missing_before_offense(match, node, source_offset) ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/theme_check/checks/space_inside_braces.rb', line 123
def add_space_missing_before_offense(match, node, source_offset)
add_offense_for_match(
"Space missing before '#{match[:token]}'",
match,
node,
source_offset
) do |corrector|
corrector.insert_before(
node,
' ',
(node.start_index + source_offset + match.begin(:token))...
(node.start_index + source_offset + match.end(:token))
)
end
end
|
#add_too_many_spaces_after_offense(match, node, source_offset) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/theme_check/checks/space_inside_braces.rb', line 107
def add_too_many_spaces_after_offense(match, node, source_offset)
add_offense_for_match(
"Too many spaces after '#{match[:token]}'",
match,
node,
source_offset
) do |corrector|
corrector.replace(
node,
' ',
(node.start_index + source_offset + match.begin(:offense))...
(node.start_index + source_offset + match.end(:offense))
)
end
end
|
#add_too_many_spaces_before_offense(match, node, source_offset) ⇒ Object
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/theme_check/checks/space_inside_braces.rb', line 139
def add_too_many_spaces_before_offense(match, node, source_offset)
add_offense_for_match(
"Too many spaces before '#{match[:token]}'",
match,
node,
source_offset
) do |corrector|
corrector.replace(
node,
' ',
(node.start_index + source_offset + match.begin(:offense))...
(node.start_index + source_offset + match.end(:offense))
)
end
end
|
#on_node(node) ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/theme_check/checks/space_inside_braces.rb', line 9
def on_node(node)
return unless node.markup
return if node.literal?
return if node.assigned_or_echoed_variable?
outside_of_strings(node.markup) do |chunk, chunk_start|
chunk.scan(/(?<token>[,:|]|==|<>|<=|>=|<|>|!=)(?<offense> +)/) do |_match|
add_too_many_spaces_after_offense(Regexp.last_match, node, chunk_start)
end
chunk.scan(/(?<offense>(?<token>[,:|]|==|<>|<=|>=|<\b|>\b|!=)(\S|\z))/) do |_match|
add_space_missing_after_offense(Regexp.last_match, node, chunk_start)
end
chunk.scan(/(?<offense>\s{2,})(?<token>\||==|<>|<=|>=|<|>|!=)+/) do |_match|
unless Regexp.last_match(:offense).include?("\n")
add_too_many_spaces_before_offense(Regexp.last_match, node, chunk_start)
end
end
chunk.scan(/(\A|\S)(?<offense>(?<token>\||==|<>|<=|>=|<|\b>|!=))/) do |_match|
add_space_missing_before_offense(Regexp.last_match, node, chunk_start)
end
end
end
|
#on_tag(node) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/theme_check/checks/space_inside_braces.rb', line 34
def on_tag(node)
return if node.inside_liquid_tag?
blocks = [
BlockMarkup.new(node.block_start_markup, node.block_start_start_index - node.start_index),
BlockMarkup.new(node.block_end_markup, node.block_end_start_index - node.start_index),
]
blocks.each do |block|
if block.markup =~ /^(?<token>{%-?)(?<offense>[^ \n\t-])/
add_space_missing_after_offense(Regexp.last_match, node, block.node_markup_offset)
end
if block.markup =~ /^(?<token>{%-?)(?<offense> {2,})\S/
add_too_many_spaces_after_offense(Regexp.last_match, node, block.node_markup_offset)
end
if block.markup =~ /(?<offense>[^ \n\t-])(?<token>-?%})$/
add_space_missing_before_offense(Regexp.last_match, node, block.node_markup_offset)
end
if block.markup =~ /\S(?<offense> {2,})(?<token>-?%})$/
add_too_many_spaces_before_offense(Regexp.last_match, node, block.node_markup_offset)
end
next
end
end
|
#on_variable(node) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/theme_check/checks/space_inside_braces.rb', line 66
def on_variable(node)
return if node.markup.empty?
return if node.assigned_or_echoed_variable?
block_start_offset = node.block_start_start_index - node.start_index
if node.block_start_markup =~ /^(?<token>{{-?)(?<offense>[^ \n\t-])/
add_space_missing_after_offense(Regexp.last_match, node, block_start_offset)
end
if node.block_start_markup =~ /^(?<token>{{-?)(?<offense> {2,})\S/
add_too_many_spaces_after_offense(Regexp.last_match, node, block_start_offset)
end
if node.block_start_markup =~ /(?<offense>[^ \n\t-])(?<token>-?}})$/
add_space_missing_before_offense(Regexp.last_match, node, block_start_offset)
end
if node.block_start_markup =~ /\S(?<offense> {2,})(?<token>-?}})$/
add_too_many_spaces_before_offense(Regexp.last_match, node, block_start_offset)
end
end
|