Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 13d4faf

Browse files
committed
Fix converting to the other type of quotes when that would lead to escaping in the string
1 parent 3ee5d0c commit 13d4faf

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

lib/syntax_tree/node.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3864,9 +3864,9 @@ module Quotes
38643864
# whichever quote the user chose. (If they chose single quotes, then double
38653865
# quoting would activate the escape sequence, and if they chose double
38663866
# quotes, then single quotes would deactivate it.)
3867-
def self.locked?(node)
3867+
def self.locked?(node, quote)
38683868
node.parts.any? do |part|
3869-
!part.is_a?(TStringContent) || part.value.match?(/\\|#[@${]/)
3869+
!part.is_a?(TStringContent) || part.value.match?(/\\|#[@${]|#{quote}/)
38703870
end
38713871
end
38723872

@@ -3981,12 +3981,12 @@ def quotes(q)
39813981

39823982
if matched
39833983
[quote, matching]
3984-
elsif Quotes.locked?(self)
3984+
elsif Quotes.locked?(self, q.quote)
39853985
["#{":" unless hash_key}'", "'"]
39863986
else
39873987
["#{":" unless hash_key}#{q.quote}", q.quote]
39883988
end
3989-
elsif Quotes.locked?(self)
3989+
elsif Quotes.locked?(self, q.quote)
39903990
if quote.start_with?(":")
39913991
[hash_key ? quote[1..] : quote, quote[1..]]
39923992
else
@@ -8404,7 +8404,7 @@ def format(q)
84048404
end
84058405

84068406
opening_quote, closing_quote =
8407-
if !Quotes.locked?(self)
8407+
if !Quotes.locked?(self, q.quote)
84088408
[q.quote, q.quote]
84098409
elsif quote.start_with?("%")
84108410
[quote, Quotes.matching(quote[/%[qQ]?(.)/, 1])]

test/fixtures/string_literal.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@
4141
%
4242
'"foo"'
4343
-
44-
"\"foo\""
44+
'"foo"'
45+
%
46+
"'foo'"
47+
-
48+
"'foo'"

test/formatter/single_quotes_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def test_dyna_symbol
2626
assert_format(":'symbol'\n", ":\"symbol\"")
2727
end
2828

29+
def test_single_quote_in_string
30+
assert_format("\"str'ing\"\n")
31+
end
32+
2933
def test_label
3034
assert_format(
3135
"{ foo => foo, :'bar' => bar }\n",

0 commit comments

Comments
 (0)