Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- dedenter.rb: Fix squiggly heredoc line continuation handling #819

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/parser/lexer/dedenter.rb
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/test_parser.rb
Expand Up @@ -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
Expand Down