ast.rb: Fix bug for source of multibyte characters

first_column and last_column return byte positions, but existing implementations
did not consider multibyte.
This commit is contained in:
alpaca-tc 2023-11-04 23:04:51 +09:00 коммит произвёл Nobuyoshi Nakada
Родитель d896269068
Коммит 8ed733f8f2
2 изменённых файлов: 10 добавлений и 2 удалений

4
ast.rb
Просмотреть файл

@ -265,8 +265,8 @@ module RubyVM::AbstractSyntaxTree
lines = script_lines
if lines
lines = lines[first_lineno - 1 .. last_lineno - 1]
lines[-1] = lines[-1][0...last_column]
lines[0] = lines[0][first_column..-1]
lines[-1] = lines[-1].byteslice(0...last_column)
lines[0] = lines[0].byteslice(first_column..-1)
lines.join
else
nil

Просмотреть файл

@ -746,6 +746,14 @@ dummy
assert_equal("def test_keep_script_lines_for_of\n", node_method.source.lines.first)
end
def test_source_with_multibyte_characters
ast = RubyVM::AbstractSyntaxTree.parse(%{a("\u00a7");b("\u00a9")}, keep_script_lines: true)
a_fcall, b_fcall = ast.children[2].children
assert_equal(%{a("\u00a7")}, a_fcall.source)
assert_equal(%{b("\u00a9")}, b_fcall.source)
end
def test_keep_tokens_for_parse
node = RubyVM::AbstractSyntaxTree.parse(<<~END, keep_tokens: true)
1.times do