Skip to content

Commit f5ae6d2

Browse files
authored
Merge pull request #3340 from Earlopain/parser-translator-multiline-string-folding
2 parents 7331089 + 076abc1 commit f5ae6d2

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

lib/prism/translation/parser/lexer.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,18 +302,25 @@ def to_a
302302
if token.type == :HEREDOC_START
303303
heredoc_identifier_stack.push(value.match(/<<[-~]?["'`]?(?<heredoc_identifier>.*?)["'`]?\z/)[:heredoc_identifier])
304304
end
305-
if ["\"", "'"].include?(value) && (next_token = lexed[index][0]) && next_token.type == :STRING_END
305+
next_token = lexed[index][0]
306+
next_next_token = lexed[index + 1][0]
307+
basic_quotes = ["\"", "'"].include?(value)
308+
309+
if basic_quotes && next_token&.type == :STRING_END
306310
next_location = token.location.join(next_token.location)
307311
type = :tSTRING
308312
value = ""
309313
location = Range.new(source_buffer, offset_cache[next_location.start_offset], offset_cache[next_location.end_offset])
310314
index += 1
311-
elsif ["\"", "'"].include?(value) && (next_token = lexed[index][0]) && next_token.type == :STRING_CONTENT && next_token.value.lines.count <= 1 && (next_next_token = lexed[index + 1][0]) && next_next_token.type == :STRING_END
312-
next_location = token.location.join(next_next_token.location)
313-
type = :tSTRING
314-
value = next_token.value.gsub("\\\\", "\\")
315-
location = Range.new(source_buffer, offset_cache[next_location.start_offset], offset_cache[next_location.end_offset])
316-
index += 2
315+
elsif basic_quotes && next_token&.type == :STRING_CONTENT && next_token.value.lines.count <= 1 && next_next_token&.type == :STRING_END
316+
# the parser gem doesn't simplify strings when its value ends in a newline
317+
unless (string_value = next_token.value).end_with?("\n")
318+
next_location = token.location.join(next_next_token.location)
319+
value = string_value.gsub("\\\\", "\\")
320+
type = :tSTRING
321+
location = Range.new(source_buffer, offset_cache[next_location.start_offset], offset_cache[next_location.end_offset])
322+
index += 2
323+
end
317324
elsif value.start_with?("<<")
318325
quote = value[2] == "-" || value[2] == "~" ? value[3] : value[2]
319326
if quote == "`"

test/prism/ruby/parser_test.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ class ParserTest < TestCase
104104
"seattlerb/dsym_esc_to_sym.txt",
105105
"seattlerb/heredoc__backslash_dos_format.txt",
106106
"seattlerb/heredoc_backslash_nl.txt",
107-
"seattlerb/heredoc_comma_arg.txt",
108107
"seattlerb/heredoc_squiggly_blank_line_plus_interpolation.txt",
109108
"seattlerb/heredoc_squiggly_blank_lines.txt",
110109
"seattlerb/heredoc_squiggly_interp.txt",
@@ -119,7 +118,6 @@ class ParserTest < TestCase
119118
"seattlerb/heredoc_with_interpolation_and_carriage_return_escapes.txt",
120119
"seattlerb/interpolated_symbol_array_line_breaks.txt",
121120
"seattlerb/interpolated_word_array_line_breaks.txt",
122-
"seattlerb/label_vs_string.txt",
123121
"seattlerb/module_comments.txt",
124122
"seattlerb/non_interpolated_symbol_array_line_breaks.txt",
125123
"seattlerb/non_interpolated_word_array_line_breaks.txt",
@@ -139,10 +137,8 @@ class ParserTest < TestCase
139137
"seattlerb/required_kwarg_no_value.txt",
140138
"seattlerb/slashy_newlines_within_string.txt",
141139
"seattlerb/str_double_escaped_newline.txt",
142-
"seattlerb/str_double_newline.txt",
143140
"seattlerb/str_evstr_escape.txt",
144141
"seattlerb/str_newline_hash_line_number.txt",
145-
"seattlerb/str_single_newline.txt",
146142
"seattlerb/symbols_empty_space.txt",
147143
"seattlerb/TestRubyParserShared.txt",
148144
"unparser/corpus/literal/assignment.txt",

0 commit comments

Comments
 (0)