Skip to content

Commit

Permalink
* do not emit truncated parts of squiggly heredoc (#774)
Browse files Browse the repository at this point in the history
  • Loading branch information
iliabylich authored Dec 9, 2020
1 parent c7a6b3f commit b87613d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
11 changes: 8 additions & 3 deletions lib/parser/builders/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -359,18 +359,23 @@ def dedent_string(node, dedent_level)
if !dedent_level.nil?
dedenter = Lexer::Dedenter.new(dedent_level)

if node.type == :str
case node.type
when :str
str = node.children.first
dedenter.dedent(str)
elsif node.type == :dstr || node.type == :xstr
node.children.each do |str_node|
when :dstr, :xstr
children = node.children.map do |str_node|
if str_node.type == :str
str = str_node.children.first
dedenter.dedent(str)
next nil if str.empty?
else
dedenter.interrupt
end
str_node
end

node = node.updated(nil, children.compact)
end
end

Expand Down
13 changes: 10 additions & 3 deletions test/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ def test_dedenting_heredoc
s(:send, nil, :p,
s(:dstr,
s(:str, " x\n"),
s(:str, ""),
s(:begin,
s(:lvar, :foo)),
s(:str, "\n"))),
Expand All @@ -412,7 +411,6 @@ def test_dedenting_heredoc
s(:send, nil, :p,
s(:xstr,
s(:str, " x\n"),
s(:str, ""),
s(:begin,
s(:lvar, :foo)),
s(:str, "\n"))),
Expand All @@ -424,7 +422,6 @@ def test_dedenting_heredoc
s(:send, nil, :p,
s(:dstr,
s(:str, " x\n"),
s(:str, ""),
s(:begin,
s(:str, " y")),
s(:str, "\n"))),
Expand Down Expand Up @@ -10121,4 +10118,14 @@ def test_endless_method_without_args
%q{},
SINCE_3_0)
end

def test_parser_drops_truncated_parts_of_squiggly_heredoc
assert_parses(
s(:dstr,
s(:begin),
s(:str, "\n")),
%q{<<~HERE! #{}!HERE}.gsub('!', "\n"),
%q{},
SINCE_2_3)
end
end

0 comments on commit b87613d

Please sign in to comment.