Skip to content

Commit 7d45fb1

Browse files
committed
Fix a token incompatibility for Prism::Translation::Parser::Lexer
This PR fixes a token incompatibility between Parser gem and `Prism::Translation::Parser` for the heredocs_leading_whitespace.txt test.
1 parent da1d484 commit 7d45fb1

File tree

4 files changed

+36
-21
lines changed

4 files changed

+36
-21
lines changed

lib/prism/translation/parser/lexer.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ def to_a
217217
index = 0
218218
length = lexed.length
219219

220+
heredoc_identifier_stack = []
221+
220222
while index < length
221223
token, state = lexed[index]
222224
index += 1
@@ -275,6 +277,9 @@ def to_a
275277
when :tSPACE
276278
value = nil
277279
when :tSTRING_BEG
280+
if token.type == :HEREDOC_START
281+
heredoc_identifier_stack.push(value.match(/<<[-~]?["']?(?<heredoc_identifier>.*?)["']?\z/)[:heredoc_identifier])
282+
end
278283
if ["\"", "'"].include?(value) && (next_token = lexed[index][0]) && next_token.type == :STRING_END
279284
next_location = token.location.join(next_token.location)
280285
type = :tSTRING
@@ -322,7 +327,7 @@ def to_a
322327
when :tSTRING_END
323328
if token.type == :HEREDOC_END && value.end_with?("\n")
324329
newline_length = value.end_with?("\r\n") ? 2 : 1
325-
value = value.sub(/\r?\n\z/, '')
330+
value = heredoc_identifier_stack.pop
326331
location = Range.new(source_buffer, offset_cache[token.location.start_offset], offset_cache[token.location.end_offset - newline_length])
327332
elsif token.type == :REGEXP_END
328333
value = value[0]

test/prism/fixtures/heredocs_leading_whitespace.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ a
33
b
44
FOO
55

6+
<<-" FOO"
7+
a
8+
b
9+
FOO
10+
611
<<-' FOO'
712
a
813
b

test/prism/parser_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ class ParserTest < TestCase
7373
skip_tokens = [
7474
"comments.txt",
7575
"heredoc_with_comment.txt",
76-
"heredocs_leading_whitespace.txt",
7776
"indented_file_end.txt",
7877
"strings.txt",
7978
"xstring_with_backslash.txt"

test/prism/snapshots/heredocs_leading_whitespace.txt

Lines changed: 25 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)