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

Fix parser translator ast for regex with fake newlines #3419

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/prism/translation/parser/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ def visit_integer_node(node)
def visit_interpolated_regular_expression_node(node)
builder.regexp_compose(
token(node.opening_loc),
visit_all(node.parts),
string_nodes_from_interpolation(node, node.opening),
[node.closing[0], srange_offsets(node.closing_loc.start_offset, node.closing_loc.start_offset + 1)],
builder.regexp_options([node.closing[1..], srange_offsets(node.closing_loc.start_offset + 1, node.closing_loc.end_offset)])
)
Expand Down Expand Up @@ -2109,6 +2109,7 @@ def string_nodes_from_line_continuations(unescaped, escaped, start_offset, openi
unescaped = unescaped.lines
escaped = escaped.lines
percent_array = opening&.start_with?("%w", "%W", "%i", "%I")
regex = opening == "/" || opening&.start_with?("%r")

# Non-interpolating strings
if opening&.end_with?("'") || opening&.start_with?("%q", "%s", "%w", "%i")
Expand Down Expand Up @@ -2144,7 +2145,8 @@ def string_nodes_from_line_continuations(unescaped, escaped, start_offset, openi
.each do |lines|
escaped_lengths << lines.sum(&:bytesize)
unescaped_lines_count = lines.sum do |line|
line.scan(/(\\*)n/).count { |(backslashes)| backslashes&.length&.odd? || false }
next 0 if regex # Will always be preserved as is
line.scan(/(\\*)n/).count { |(backslashes)| backslashes&.length&.odd?}
end
extra = 1
extra = lines.count if percent_array # Account for line continuations in percent arrays
Expand Down
41 changes: 41 additions & 0 deletions test/prism/fixtures/regex_with_fake_newlines.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/
\n
\n
exit
\\n
\n\n\n\n
argh
\\
\\\
foo\nbar
\f
ok
/

%r{
\n
\n
exit
\\n
\n\n\n\n
argh
\\
\\\
foo\nbar
\f
ok
}

%r{
#{123}\n
\n
exit\\\
\\#{123}n
\n#{123}\n\n\n
argh\
\\#{123}baz\\
\\\
foo\nbar
\f
ok
}
98 changes: 98 additions & 0 deletions test/prism/snapshots/regex_with_fake_newlines.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading