From 70ea223a6810801a50951eabdaa98fec03b3b464 Mon Sep 17 00:00:00 2001 From: Reese Williams Date: Tue, 22 Nov 2022 09:45:05 -0700 Subject: [PATCH 1/2] Add fixtures --- fixtures/small/dyna_symbol_with_escapes_actual.rb | 10 ++++++++++ fixtures/small/dyna_symbol_with_escapes_expected.rb | 10 ++++++++++ 2 files changed, 20 insertions(+) create mode 100644 fixtures/small/dyna_symbol_with_escapes_actual.rb create mode 100644 fixtures/small/dyna_symbol_with_escapes_expected.rb diff --git a/fixtures/small/dyna_symbol_with_escapes_actual.rb b/fixtures/small/dyna_symbol_with_escapes_actual.rb new file mode 100644 index 00000000..04c24f00 --- /dev/null +++ b/fixtures/small/dyna_symbol_with_escapes_actual.rb @@ -0,0 +1,10 @@ +:'"foo"' + +:"'#{<<~LOL}'" + I'm so sorry for \n writing this + #{:'"雷神の"'} + 少し響みて + さし曇り + 雨も降らぬか + 君を留めむ +LOL diff --git a/fixtures/small/dyna_symbol_with_escapes_expected.rb b/fixtures/small/dyna_symbol_with_escapes_expected.rb new file mode 100644 index 00000000..39cdf90f --- /dev/null +++ b/fixtures/small/dyna_symbol_with_escapes_expected.rb @@ -0,0 +1,10 @@ +:"\"foo\"" + +:"'#{<<~LOL}'" + I'm so sorry for \n writing this + #{:"\"\u96F7\u795E\u306E\""} + 少し響みて + さし曇り + 雨も降らぬか + 君を留めむ +LOL From b1cd8e9e7639348a9d8b48d1b30f4f804fe044de Mon Sep 17 00:00:00 2001 From: Reese Williams Date: Tue, 22 Nov 2022 09:45:35 -0700 Subject: [PATCH 2/2] Clean string contents for dyna_symbol expressions just like normal string literals --- librubyfmt/rubyfmt_lib.rb | 114 ++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 54 deletions(-) diff --git a/librubyfmt/rubyfmt_lib.rb b/librubyfmt/rubyfmt_lib.rb index ac475e52..682379e8 100644 --- a/librubyfmt/rubyfmt_lib.rb +++ b/librubyfmt/rubyfmt_lib.rb @@ -433,58 +433,7 @@ def on_string_literal(*args, &blk) args << [start_line, end_line] - if start_delim && end_delim && start_delim != "\"" - if start_delim == "'" || start_delim.start_with?("%q") - # re-evaluate the string with its own quotes to handle escaping. - if args[0][1] - es = eval("#{start_delim}#{args[0][1][1]}#{end_delim}") - # did the original contain \u's? - have_source_slash_u = args[0][1][1].include?("\\u") - # if all chars are unicode definitionally none of them are delimiters so we - # can skip inspect - have_all_unicode = es.chars.all? { |x| x.bytes.first >= 128 } - - if have_all_unicode && !have_source_slash_u - "#{start_delim}#{args[0][1][1]}#{end_delim}" - else - args[0][1][1] = es.inspect[1..-2] - end - # Match at word boundaries and beginning/end of the string - # so that things like `'\n'` correctly become `"\\n"` - # instead of rendering as actual whitespace - # - # About this regex: `(?= 128 } + + if have_all_unicode && !have_source_slash_u + "#{start_delim}#{string_contents[1][1]}#{end_delim}" + else + string_contents[1][1] = es.inspect[1..-2] + end + # Match at word boundaries and beginning/end of the string + # so that things like `'\n'` correctly become `"\\n"` + # instead of rendering as actual whitespace + # + # About this regex: `(?