diff --git a/lib/rbs/parser_aux.rb b/lib/rbs/parser_aux.rb index 974c54e20..ff14f951d 100644 --- a/lib/rbs/parser_aux.rb +++ b/lib/rbs/parser_aux.rb @@ -121,12 +121,14 @@ def self.buffer(source) def self.parse_inline_leading_annotation(source, range, variables: []) buf = buffer(source) - _parse_inline_leading_annotation(buf, range.begin || 0, range.end || buf.last_position, variables) + byte_range = byte_range(range, buf.content) + _parse_inline_leading_annotation(buf, byte_range.begin || 0, byte_range.end || buf.content.bytesize, variables) end def self.parse_inline_trailing_annotation(source, range, variables: []) buf = buffer(source) - _parse_inline_trailing_annotation(buf, range.begin || 0, range.end || buf.last_position, variables) + byte_range = byte_range(range, buf.content) + _parse_inline_trailing_annotation(buf, byte_range.begin || 0, byte_range.end || buf.content.bytesize, variables) end def self.byte_range(char_range, content) diff --git a/test/rbs/inline_annotation_parsing_test.rb b/test/rbs/inline_annotation_parsing_test.rb index b76c8533c..266db36b8 100644 --- a/test/rbs/inline_annotation_parsing_test.rb +++ b/test/rbs/inline_annotation_parsing_test.rb @@ -559,4 +559,28 @@ def test_error__module_self Parser.parse_inline_leading_annotation("@rbs module-self: foo", 0...) end end + + def test_parse__trailing_assertion__multibyte_offset + buffer = Buffer.new(name: Pathname("test.rb"), content: "日本語\n: String") + + Parser.parse_inline_trailing_annotation(buffer, 4...12).tap do |annot| + assert_instance_of AST::Ruby::Annotations::NodeTypeAssertion, annot + assert_equal ": String", annot.location.source + assert_equal ":", annot.prefix_location.source + assert_equal "String", annot.type.location.source + end + end + + def test_parse__leading_annotation__multibyte_offset + buffer = Buffer.new(name: Pathname("test.rb"), content: "日本語のコメント\n@rbs return: String -- 戻り値の説明") + + Parser.parse_inline_leading_annotation(buffer, 9...).tap do |annot| + assert_instance_of AST::Ruby::Annotations::ReturnTypeAnnotation, annot + assert_equal "@rbs return: String -- 戻り値の説明", annot.location.source + assert_equal "return", annot.return_location.source + assert_equal ":", annot.colon_location.source + assert_equal "String", annot.return_type.location.source + assert_equal "-- 戻り値の説明", annot.comment_location.source + end + end end