From c7de3f653cec55ac5aa9d71b46ccca19430e00d8 Mon Sep 17 00:00:00 2001
From: nobuyo <longzechangsheng@gmail.com>
Date: Mon, 12 Jun 2023 13:52:21 +0900
Subject: [PATCH] Fix formatting for CHAR node when single_quotes plugin is
 enabled

---
 lib/syntax_tree/node.rb           | 2 +-
 test/plugin/single_quotes_test.rb | 8 ++++++++
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/lib/syntax_tree/node.rb b/lib/syntax_tree/node.rb
index a2c78677..3c5f93da 100644
--- a/lib/syntax_tree/node.rb
+++ b/lib/syntax_tree/node.rb
@@ -288,7 +288,7 @@ def format(q)
         q.text(value)
       else
         q.text(q.quote)
-        q.text(value[1] == "\"" ? "\\\"" : value[1])
+        q.text(value[1] == q.quote ? "\\#{q.quote}" : value[1])
         q.text(q.quote)
       end
     end
diff --git a/test/plugin/single_quotes_test.rb b/test/plugin/single_quotes_test.rb
index 6ce10448..b1359ac7 100644
--- a/test/plugin/single_quotes_test.rb
+++ b/test/plugin/single_quotes_test.rb
@@ -8,6 +8,14 @@ def test_empty_string_literal
       assert_format("''\n", "\"\"")
     end
 
+    def test_character_literal_with_double_quote
+      assert_format("'\"'\n", "?\"")
+    end
+
+    def test_character_literal_with_singlee_quote
+      assert_format("'\\''\n", "?'")
+    end
+
     def test_string_literal
       assert_format("'string'\n", "\"string\"")
     end