Skip to content

Commit 7518a3b

Browse files
committed
Implement InlineParser
1 parent a9d2a44 commit 7518a3b

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

lib/rbs/inline_parser.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ def initialize(location, message)
2323
end
2424
end
2525

26+
NotImplementedYet = _ = Class.new(Base)
2627
NonConstantClassName = _ = Class.new(Base)
2728
NonConstantModuleName = _ = Class.new(Base)
29+
TopLevelMethodDefinition = _ = Class.new(Base)
2830
end
2931

3032
def self.parse(buffer, prism)
@@ -101,6 +103,27 @@ def visit_module_node(node)
101103
end
102104
end
103105

106+
def visit_def_node(node)
107+
if node.receiver
108+
diagnostics << Diagnostic::NotImplementedYet.new(
109+
rbs_location(node.receiver.location),
110+
"Singleton method definition is not supported yet"
111+
)
112+
return
113+
end
114+
115+
case current = current_module
116+
when AST::Ruby::Declarations::ClassDecl, AST::Ruby::Declarations::ModuleDecl
117+
defn = AST::Ruby::Members::DefMember.new(buffer, node.name, node)
118+
current.members << defn
119+
else
120+
diagnostics << Diagnostic::TopLevelMethodDefinition.new(
121+
rbs_location(node.name_loc),
122+
"Top-level method definition is not supported"
123+
)
124+
end
125+
end
126+
104127
def insert_declaration(decl)
105128
if current_module
106129
current_module.members << decl

sig/inline_parser.rbs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,21 @@ module RBS
2020
def initialize: (Location, String) -> void
2121
end
2222

23+
class NotImplementedYet < Base
24+
end
25+
2326
class NonConstantClassName < Base
2427
end
2528

2629
class NonConstantModuleName < Base
2730
end
2831

29-
type t = NonConstantClassName | NonConstantModuleName
32+
class TopLevelMethodDefinition < Base
33+
end
34+
35+
type t = NotImplementedYet
36+
| NonConstantClassName | NonConstantModuleName
37+
| TopLevelMethodDefinition
3038
end
3139

3240
def self.parse: (Buffer, Prism::ParseResult) -> Result

test/rbs/inline_parser_test.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,56 @@ module World
8787

8888
assert_empty result.declarations
8989
end
90+
91+
def test_parse__def
92+
result = parse(<<~RUBY)
93+
class Foo
94+
def foo; end
95+
end
96+
RUBY
97+
98+
assert_empty result.diagnostics
99+
100+
assert_equal 1, result.declarations.size
101+
result.declarations[0].tap do |decl|
102+
assert_instance_of RBS::AST::Ruby::Declarations::ClassDecl, decl
103+
assert_equal RBS::TypeName.parse("Foo"), decl.class_name
104+
105+
decl.members[0].tap do |member|
106+
assert_instance_of RBS::AST::Ruby::Members::DefMember, member
107+
assert_equal :foo, member.name
108+
assert_equal "def foo; end", member.location.source
109+
end
110+
end
111+
end
112+
113+
def test_error__def__toplevel
114+
result = parse(<<~RUBY)
115+
def foo; end
116+
RUBY
117+
118+
assert_equal 1, result.diagnostics.size
119+
assert_any!(result.diagnostics) do |diagnostic|
120+
assert_instance_of RBS::InlineParser::Diagnostic::TopLevelMethodDefinition, diagnostic
121+
assert_equal "foo", diagnostic.location.source
122+
assert_equal "Top-level method definition is not supported", diagnostic.message
123+
end
124+
125+
assert_empty result.declarations
126+
end
127+
128+
def test_error__def__singleton
129+
result = parse(<<~RUBY)
130+
module Foo
131+
def self.foo; end
132+
end
133+
RUBY
134+
135+
assert_equal 1, result.diagnostics.size
136+
assert_any!(result.diagnostics) do |diagnostic|
137+
assert_instance_of RBS::InlineParser::Diagnostic::NotImplementedYet, diagnostic
138+
assert_equal "self", diagnostic.location.source
139+
assert_equal "Singleton method definition is not supported yet", diagnostic.message
140+
end
141+
end
90142
end

0 commit comments

Comments
 (0)