This repository has been archived by the owner on Jul 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
/
space_inside_braces.rb
165 lines (144 loc) · 5.38 KB
/
space_inside_braces.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# frozen_string_literal: true
module ThemeCheck
# Ensure {% ... %} & {{ ... }} have consistent spaces.
class SpaceInsideBraces < LiquidCheck
severity :style
category :liquid
doc docs_url(__FILE__)
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
BlockMarkup = Struct.new(:markup, :node_markup_offset)
def on_tag(node)
return if node.inside_liquid_tag?
# Both the start and end tags
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|
# Looking at spaces after the start token
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
# Looking at spaces before the end token
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
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
# Looking at spaces after the start token
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
# Looking at spaces before the end token
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
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
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
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
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
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
end
end