Skip to content

Commit 7cbaa3b

Browse files
Earlopainkddnewton
authored andcommitted
[ruby/prism] Fix an incompatibility with the parser translator
The offset cache contains an entry for each byte so it can't be accessed via the string length. Adds tests for all variants except for this: ``` "fo o" "ba ’" ``` For some reason, this still has the wrong offset. ruby/prism@a651126458
1 parent d597118 commit 7cbaa3b

File tree

6 files changed

+15
-5
lines changed

6 files changed

+15
-5
lines changed

lib/prism/translation/parser/compiler.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ def visit_interpolated_string_node(node)
10911091
start_offset = node.content_loc.start_offset
10921092

10931093
node.unescaped.lines.map do |line|
1094-
end_offset = start_offset + line.length
1094+
end_offset = start_offset + line.bytesize
10951095
offsets = srange_offsets(start_offset, end_offset)
10961096
start_offset = end_offset
10971097

@@ -1692,7 +1692,7 @@ def visit_string_node(node)
16921692
start_offset = node.content_loc.start_offset
16931693

16941694
[content_lines, unescaped_lines].transpose.map do |content_line, unescaped_line|
1695-
end_offset = start_offset + content_line.length
1695+
end_offset = start_offset + content_line.bytesize
16961696
offsets = srange_offsets(start_offset, end_offset)
16971697
start_offset = end_offset
16981698

@@ -1747,7 +1747,7 @@ def visit_symbol_node(node)
17471747
start_offset = node.value_loc.start_offset
17481748

17491749
node.value.lines.map do |line|
1750-
end_offset = start_offset + line.length
1750+
end_offset = start_offset + line.bytesize
17511751
offsets = srange_offsets(start_offset, end_offset)
17521752
start_offset = end_offset
17531753

@@ -1890,7 +1890,7 @@ def visit_x_string_node(node)
18901890
start_offset = node.content_loc.start_offset
18911891

18921892
node.unescaped.lines.map do |line|
1893-
end_offset = start_offset + line.length
1893+
end_offset = start_offset + line.bytesize
18941894
offsets = srange_offsets(start_offset, end_offset)
18951895
start_offset = end_offset
18961896

lib/prism/translation/parser/lexer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def to_a
343343
adjustment = 0
344344
end
345345

346-
end_offset = start_offset + adjusted_line.length + adjustment
346+
end_offset = start_offset + adjusted_line.bytesize + adjustment
347347
tokens << [:tSTRING_CONTENT, [adjusted_line, Range.new(source_buffer, offset_cache[start_offset], offset_cache[end_offset])]]
348348
start_offset = end_offset
349349
end

test/prism/fixtures/dstring.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ foo\\\\
2727
"
2828
foo\\\\\
2929
"
30+
31+
"
32+
’"

test/prism/fixtures/dsym_str.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
:"foo
22
bar"
3+
4+
:"
5+
’"

test/prism/fixtures/xstring.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@
1111
``
1212

1313
%x{}
14+
15+
`
16+
’`

test/prism/ruby/ruby_parser_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class RubyParserTest < TestCase
4646
# https://github.com/seattlerb/ruby_parser/issues/344
4747
failures = [
4848
"alias.txt",
49+
"dsym_str.txt",
4950
"dos_endings.txt",
5051
"heredocs_with_ignored_newlines.txt",
5152
"method_calls.txt",

0 commit comments

Comments
 (0)