Skip to content

Commit

Permalink
Support module declarations with self types
Browse files Browse the repository at this point in the history
  • Loading branch information
soutaro committed Apr 25, 2024
1 parent 2c1a8e6 commit db7f4a6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/rbs/inline/ast/declarations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,28 @@ class ModuleDecl < Base
attr_reader :node
attr_reader :members
attr_reader :comments
attr_reader :inner_comments

def initialize(node, comments)
@node = node
@comments = comments
@members = []
@inner_comments = []
end

def module_name
type_name(node.constant_path)
end

def module_selfs
inner_comments.flat_map do |comment|
comment.annotations.filter_map do |ann|
if ann.is_a?(AST::Annotations::ModuleSelf)
ann
end
end
end
end
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions lib/rbs/inline/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ def visit_module_node(node)
push_class_module_decl(module_decl) do
visit node.body
end

annotation_range = node.location.start_line .. node.location.end_line
annotations = comments.each_value.select do |annotation|
range = annotation.line_range
annotation_range.begin < range.begin && range.end < annotation_range.end
end

annotations.each do |annot|
comments.delete(annot.line_range.end)
module_decl.inner_comments.push(annot)
end
end

def visit_def_node(node)
Expand Down
4 changes: 3 additions & 1 deletion lib/rbs/inline/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ def translate_module_decl(decl)
end
end

self_types = decl.module_selfs.map { _1.constraint }.compact

RBS::AST::Declarations::Module.new(
name: decl.module_name,
type_params: [],
members: members,
self_types: [],
self_types: self_types,
annotations: [],
location: nil,
comment: comment
Expand Down
4 changes: 4 additions & 0 deletions sig/rbs/inline/ast/declarations.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ module RBS

attr_reader comments: AnnotationParser::ParsingResult?

attr_reader inner_comments: Array[AnnotationParser::ParsingResult]

def initialize: (Prism::ModuleNode, AnnotationParser::ParsingResult?) -> void

%a{pure} def module_name: () -> TypeName?

%a{pure} def module_selfs: () -> Array[Annotations::ModuleSelf]
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions test/rbs/inline/writer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class RBS::Inline::WriterTest < Minitest::Test
include RBS::Inline

def translate(src)
src = "# rbs_inline: enabled\n\n" + src
uses, decls = Parser.parse(Prism.parse(src, filepath: "a.rb"))
Writer.write(uses, decls)
end
Expand Down Expand Up @@ -348,4 +349,21 @@ def length: ...
end
RBS
end

def test_module_self
output = translate(<<~RUBY)
module Foo
# @rbs module-self BasicObject
def foo
end
end
RUBY

assert_equal <<~RBS, output
module Foo : BasicObject
def foo: () -> untyped
end
RBS
end
end

0 comments on commit db7f4a6

Please sign in to comment.