Skip to content

Commit

Permalink
Implement writer
Browse files Browse the repository at this point in the history
  • Loading branch information
soutaro committed Jan 15, 2025
1 parent 6b04fb5 commit 65371a2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
15 changes: 10 additions & 5 deletions lib/rbs/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,19 @@ def write_comment(comment)
end

def write(contents)
dirs = contents.select {|c| c.is_a?(AST::Directives::Base) } #: Array[AST::Directives::t]
resolveds = contents.select { _1.is_a?(AST::Directives::Resolved) } #: Array[AST::Directives::Resolved]
uses = contents.select {|c| c.is_a?(AST::Directives::Use) } #: Array[AST::Directives::Use]
decls = contents.select {|c| c.is_a?(AST::Declarations::Base) } #: Array[AST::Declarations::t]

dirs.each do |dir|
write_directive(dir)
unless resolveds.empty?
puts "# resolved: true"
puts
end

puts unless dirs.empty?
uses.each do |dir|
write_use_directive(dir)
end
puts unless uses.empty?

[nil, *decls].each_cons(2) do |prev, decl|
raise unless decl
Expand All @@ -94,7 +99,7 @@ def write(contents)
end
end

def write_directive(dir)
def write_use_directive(dir)
clauses = dir.clauses.map do |clause|
case clause
when AST::Directives::Use::SingleClause
Expand Down
2 changes: 1 addition & 1 deletion sig/writer.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ module RBS

def format_annotation: (AST::Annotation) -> String

def write_directive: (AST::Directives::t) -> void
def write_use_directive: (AST::Directives::Use) -> void

def write_annotation: (Array[AST::Annotation]) -> void

Expand Down
29 changes: 29 additions & 0 deletions test/rbs/writer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,33 @@ class Foo
end
SIG
end

def test_magic_comment
assert_writer <<-SIG
# resolved: true
class Foo
end
SIG

assert_writer <<-SIG
# resolved: true
use String as S
type s = S
SIG
end

def test_magic_comment_error
# We don't want the duplicated magic comment...

assert_fail_assertion do
assert_writer <<-SIG
# resolved: true
class Foo
end
SIG
end
end
end

0 comments on commit 65371a2

Please sign in to comment.