Skip to content

Commit 11b9a04

Browse files
committed
Use Syntax Tree for quoting strings
1 parent 52bb296 commit 11b9a04

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

lib/syntax_tree/haml/format.rb

+21-25
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ def visit_silent_script(node)
123123

124124
LiteralHashValue = Struct.new(:value)
125125

126+
StringHashValue = Struct.new(:value, :quote)
127+
126128
# When formatting a tag, there are a lot of different kinds of things that
127129
# can be printed out. There's the tag name, the attributes, the content,
128130
# etc. This object is responsible for housing all of those parts.
@@ -220,19 +222,34 @@ def length
220222
private
221223

222224
def format_value(q, hash, level = 0)
225+
quote = SyntaxTree::Formatter::OPTIONS[:quote]
226+
223227
q.group do
224228
q.text("{")
225229
q.indent do
226230
q.group do
227231
q.breakable(level == 0 ? "" : " ")
228232
q.seplist(hash, nil, :each_pair) do |key, value|
229-
q.text(Format.hash_key(key))
233+
if key.match?(/^@|[-:]/)
234+
q.text("#{quote}#{Quotes.normalize(key, quote)}#{quote}:")
235+
else
236+
q.text("#{key}:")
237+
end
238+
230239
q.text(" ")
231240

232-
if value.is_a?(Hash)
241+
case value
242+
when Hash
233243
format_value(q, value, level + 1)
244+
when LiteralHashValue
245+
q.text(value.value)
246+
when StringLiteral
247+
qq = Formatter.new("")
248+
qq.with_target(q.target) { value.format(qq) }
249+
when String
250+
q.text("#{quote}#{Quotes.normalize(value, quote)}#{quote}")
234251
else
235-
q.text(Format.hash_value(value))
252+
q.text(value.to_s)
236253
end
237254
end
238255
end
@@ -244,27 +261,6 @@ def format_value(q, hash, level = 0)
244261
end
245262
end
246263

247-
def self.hash_key(key)
248-
if key.match?(/^@|[-:]/)
249-
quote = SyntaxTree::Formatter::OPTIONS[:quote]
250-
"#{quote}#{Quotes.normalize(key, quote)}#{quote}:"
251-
else
252-
"#{key}:"
253-
end
254-
end
255-
256-
def self.hash_value(value)
257-
case value
258-
when LiteralHashValue
259-
value.value
260-
when String
261-
quote = SyntaxTree::Formatter::OPTIONS[:quote]
262-
"#{quote}#{Quotes.normalize(value, quote)}#{quote}"
263-
else
264-
value.to_s
265-
end
266-
end
267-
268264
# Visit a tag node.
269265
def visit_tag(node)
270266
parts = PartList.new(node)
@@ -405,7 +401,7 @@ def parse_attributes(source)
405401
::Haml::AttributeParser.parse(source)
406402
parsed.to_h { |key, value| [key, parse_attributes(value)] }
407403
in [:program, [[:string_literal, *], *]]
408-
source[1...-1]
404+
SyntaxTree.parse(source).statements.body[0]
409405
else
410406
LiteralHashValue.new(source)
411407
end

test/tag_test.rb

+9-4
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,15 @@ def test_long_declaration_before_text_without_parser
133133
end
134134

135135
def test_quotes_in_strings
136-
assert_format(
137-
"%div{title: 'escape \" quotes'}",
138-
"%div{title: \"escape \\\" quotes\"}"
139-
)
136+
assert_format("%div{title: 'escape \" quotes'}")
137+
end
138+
139+
def test_interpolation_in_strings
140+
with_single_quotes do
141+
assert_format(<<~HAML)
142+
%div{style: "background: center/cover url(\#{url_for(page.resource.file)})"}
143+
HAML
144+
end
140145
end
141146

142147
def test_interpolation_in_value

0 commit comments

Comments
 (0)