diff options
-rw-r--r-- | lib/prism/translation/parser/lexer.rb | 6 | ||||
-rw-r--r-- | test/prism/fixtures/heredoc.txt | 2 | ||||
-rw-r--r-- | test/prism/fixtures/heredoc_with_carriage_returns.txt | 2 | ||||
-rw-r--r-- | test/prism/snapshots/heredoc.txt | 11 | ||||
-rw-r--r-- | test/prism/snapshots/heredoc_with_carriage_returns.txt | 11 |
5 files changed, 31 insertions, 1 deletions
diff --git a/lib/prism/translation/parser/lexer.rb b/lib/prism/translation/parser/lexer.rb index 7585aadd7b..b28273b03f 100644 --- a/lib/prism/translation/parser/lexer.rb +++ b/lib/prism/translation/parser/lexer.rb @@ -304,7 +304,11 @@ module Prism when :tSTRING_DVAR value = nil when :tSTRING_END - if token.type == :REGEXP_END + if token.type == :HEREDOC_END && value.end_with?("\n") + newline_length = value.end_with?("\r\n") ? 2 : 1 + value = value.sub(/\r?\n\z/, '') + location = Range.new(source_buffer, offset_cache[token.location.start_offset], offset_cache[token.location.end_offset - newline_length]) + elsif token.type == :REGEXP_END value = value[0] location = Range.new(source_buffer, offset_cache[token.location.start_offset], offset_cache[token.location.start_offset + 1]) end diff --git a/test/prism/fixtures/heredoc.txt b/test/prism/fixtures/heredoc.txt new file mode 100644 index 0000000000..f94fd912df --- /dev/null +++ b/test/prism/fixtures/heredoc.txt @@ -0,0 +1,2 @@ +<<TEXT +TEXT diff --git a/test/prism/fixtures/heredoc_with_carriage_returns.txt b/test/prism/fixtures/heredoc_with_carriage_returns.txt new file mode 100644 index 0000000000..32a2d87b24 --- /dev/null +++ b/test/prism/fixtures/heredoc_with_carriage_returns.txt @@ -0,0 +1,2 @@ +<<TEXT
+TEXT
diff --git a/test/prism/snapshots/heredoc.txt b/test/prism/snapshots/heredoc.txt new file mode 100644 index 0000000000..3ca66dd989 --- /dev/null +++ b/test/prism/snapshots/heredoc.txt @@ -0,0 +1,11 @@ +@ ProgramNode (location: (1,0)-(1,6)) +├── locals: [] +└── statements: + @ StatementsNode (location: (1,0)-(1,6)) + └── body: (length: 1) + └── @ StringNode (location: (1,0)-(1,6)) + ├── flags: ∅ + ├── opening_loc: (1,0)-(1,6) = "<<TEXT" + ├── content_loc: (2,0)-(2,0) = "" + ├── closing_loc: (2,0)-(3,0) = "TEXT\n" + └── unescaped: "" diff --git a/test/prism/snapshots/heredoc_with_carriage_returns.txt b/test/prism/snapshots/heredoc_with_carriage_returns.txt new file mode 100644 index 0000000000..134f863234 --- /dev/null +++ b/test/prism/snapshots/heredoc_with_carriage_returns.txt @@ -0,0 +1,11 @@ +@ ProgramNode (location: (1,0)-(1,6)) +├── locals: [] +└── statements: + @ StatementsNode (location: (1,0)-(1,6)) + └── body: (length: 1) + └── @ StringNode (location: (1,0)-(1,6)) + ├── flags: ∅ + ├── opening_loc: (1,0)-(1,6) = "<<TEXT" + ├── content_loc: (2,0)-(2,0) = "" + ├── closing_loc: (2,0)-(3,0) = "TEXT\r\n" + └── unescaped: "" |