Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3864,9 +3864,9 @@ module Quotes
# whichever quote the user chose. (If they chose single quotes, then double
# quoting would activate the escape sequence, and if they chose double
# quotes, then single quotes would deactivate it.)
def self.locked?(node)
def self.locked?(node, quote)
node.parts.any? do |part|
!part.is_a?(TStringContent) || part.value.match?(/\\|#[@${]/)
!part.is_a?(TStringContent) || part.value.match?(/\\|#[@${]|#{quote}/)
end
end

Expand Down Expand Up @@ -3981,12 +3981,12 @@ def quotes(q)

if matched
[quote, matching]
elsif Quotes.locked?(self)
elsif Quotes.locked?(self, q.quote)
["#{":" unless hash_key}'", "'"]
else
["#{":" unless hash_key}#{q.quote}", q.quote]
end
elsif Quotes.locked?(self)
elsif Quotes.locked?(self, q.quote)
if quote.start_with?(":")
[hash_key ? quote[1..] : quote, quote[1..]]
else
Expand Down Expand Up @@ -8404,7 +8404,7 @@ def format(q)
end

opening_quote, closing_quote =
if !Quotes.locked?(self)
if !Quotes.locked?(self, q.quote)
[q.quote, q.quote]
elsif quote.start_with?("%")
[quote, Quotes.matching(quote[/%[qQ]?(.)/, 1])]
Expand Down
6 changes: 5 additions & 1 deletion test/fixtures/string_literal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@
%
'"foo"'
-
"\"foo\""
'"foo"'
%
"'foo'"
-
"'foo'"
4 changes: 4 additions & 0 deletions test/formatter/single_quotes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def test_dyna_symbol
assert_format(":'symbol'\n", ":\"symbol\"")
end

def test_single_quote_in_string
assert_format("\"str'ing\"\n")
end

def test_label
assert_format(
"{ foo => foo, :'bar' => bar }\n",
Expand Down