diff --git a/lib/parser/lexer/dedenter.rb b/lib/parser/lexer/dedenter.rb index 5f0ff2700..5ff16007a 100644 --- a/lib/parser/lexer/dedenter.rb +++ b/lib/parser/lexer/dedenter.rb @@ -38,7 +38,13 @@ def dedent(string) # Prevent the following error when processing binary encoded source. # "\xC0".split # => ArgumentError (invalid byte sequence in UTF-8) lines = string.force_encoding(Encoding::BINARY).split("\\\n") - lines.map! {|s| s.force_encoding(original_encoding) } + if lines.length == 1 + # If the line continuation sequence was found but there is no second + # line, it was not really a line continuation and must be ignored. + lines = [string] + else + lines.map! {|s| s.force_encoding(original_encoding) } + end if @at_line_begin lines_to_dedent = lines diff --git a/test/test_parser.rb b/test/test_parser.rb index 7aa78c7a1..20d77d725 100644 --- a/test/test_parser.rb +++ b/test/test_parser.rb @@ -439,6 +439,22 @@ def test_parser_bug_640 SINCE_2_3) end + def test_dedenting_non_interpolating_heredoc_line_continuation + assert_parses( + s(:dstr, s(:str, "baz\\\n"), s(:str, "qux\n")), + %Q{<<~'FOO'\n baz\\\n qux\nFOO}, + %q{}, + SINCE_2_3) + end + + def test_dedenting_interpolating_heredoc_fake_line_continuation + assert_parses( + s(:dstr, s(:str, "baz\\\n"), s(:str, "qux\n")), + %Q{<<~'FOO'\n baz\\\\\n qux\nFOO}, + %q{}, + SINCE_2_3) + end + # Symbols def test_symbol_plain