Skip to content
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/rbs/parser_aux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions test/rbs/inline_annotation_parsing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading