Skip to content

Commit c1b1cc5

Browse files
committed
Update AST
1 parent 99f7e2c commit c1b1cc5

File tree

5 files changed

+83
-11
lines changed

5 files changed

+83
-11
lines changed

lib/rbs/ast/ruby/declarations.rb

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,60 @@ def initialize(buffer)
1616
end
1717

1818
class ClassDecl < Base
19+
class SuperClass
20+
attr_reader :type_name_location
21+
22+
attr_reader :operator_location
23+
24+
attr_reader :type_name
25+
26+
attr_reader :type_annotation
27+
28+
def initialize(type_name_location, operator_location, type_name, type_annotation)
29+
@type_name_location = type_name_location
30+
@operator_location = operator_location
31+
@type_name = type_name
32+
@type_annotation = type_annotation
33+
end
34+
35+
def type_args
36+
if type_annotation
37+
type_annotation.type_args
38+
else
39+
[]
40+
end
41+
end
42+
43+
def location
44+
if type_annotation
45+
Location.new(
46+
type_name_location.buffer,
47+
type_name_location.start_pos,
48+
type_annotation.location.end_pos
49+
)
50+
else
51+
type_name_location
52+
end
53+
end
54+
55+
alias name type_name
56+
alias args type_args
57+
end
58+
1959
attr_reader :class_name
2060

2161
attr_reader :members
2262

2363
attr_reader :node
2464

25-
def initialize(buffer, name, node)
65+
attr_reader :super_class
66+
67+
def initialize(buffer, name, node, super_class)
2668
super(buffer)
2769
@class_name = name
2870
@node = node
2971
@members = []
72+
@super_class = super_class
3073
end
3174

3275
def each_decl(&block)
@@ -39,8 +82,6 @@ def each_decl(&block)
3982
end
4083
end
4184

42-
def super_class = nil
43-
4485
def type_params = []
4586

4687
def location

lib/rbs/environment.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,16 @@ def resolve_ruby_decl(resolver, decl, context:, prefix:)
679679
inner_context = [context, full_name] #: Resolver::context
680680
inner_prefix = full_name.to_namespace
681681

682-
AST::Ruby::Declarations::ClassDecl.new(decl.buffer, full_name, decl.node).tap do |resolved|
682+
super_class = decl.super_class&.yield_self do |super_class|
683+
AST::Ruby::Declarations::ClassDecl::SuperClass.new(
684+
super_class.type_name_location,
685+
super_class.operator_location,
686+
absolute_type_name(resolver, nil, super_class.name, context: context),
687+
super_class.type_annotation&.map_type_name {|name, _, _| absolute_type_name(resolver, nil, name, context: context) }
688+
)
689+
end
690+
691+
AST::Ruby::Declarations::ClassDecl.new(decl.buffer, full_name, decl.node, super_class).tap do |resolved|
683692
decl.members.each do |member|
684693
case member
685694
when AST::Ruby::Declarations::Base

lib/rbs/inline_parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def visit_class_node(node)
100100
return
101101
end
102102

103-
class_decl = AST::Ruby::Declarations::ClassDecl.new(buffer, class_name, node)
103+
class_decl = AST::Ruby::Declarations::ClassDecl.new(buffer, class_name, node, nil)
104104
insert_declaration(class_decl)
105105
push_module_nesting(class_decl) do
106106
visit_child_nodes(node)

sig/ast/ruby/declarations.rbs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,41 @@ module RBS
1414
end
1515

1616
class ClassDecl < Base
17+
class SuperClass
18+
attr_reader operator_location: Location
19+
20+
attr_reader type_name_location: Location
21+
22+
attr_reader type_name: TypeName
23+
24+
attr_reader type_annotation: Annotations::TypeApplicationAnnotation?
25+
26+
def type_args: () -> Array[Types::t]
27+
28+
alias name type_name
29+
30+
alias args type_args
31+
32+
def initialize: (Location type_name_location, Location operator_location, TypeName, RBS::AST::Ruby::Annotations::TypeApplicationAnnotation?) -> void
33+
34+
def location: () -> Location
35+
end
36+
1737
type member = t | Members::t
1838

1939
attr_reader class_name: TypeName
2040

2141
attr_reader node: Prism::ClassNode
2242

43+
attr_reader super_class: SuperClass?
44+
2345
attr_reader members: Array[member]
2446

25-
def initialize: (Buffer, TypeName, Prism::ClassNode) -> void
47+
def initialize: (Buffer, TypeName, Prism::ClassNode, SuperClass?) -> void
2648

2749
def each_decl: () { (t) -> void } -> void
2850
| () -> Enumerator[t]
2951

30-
def super_class: () -> nil
31-
3252
def type_params: () -> Array[AST::TypeParam]
3353

3454
def location: () -> Location

sig/errors.rbs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,19 +308,21 @@ module RBS
308308
# InheritModuleError is raised if a class definition inherits a module (not a class)
309309
#
310310
class InheritModuleError < DefinitionError
311+
type super_class = AST::Declarations::Class::Super | AST::Ruby::Declarations::ClassDecl::SuperClass
312+
311313
include DetailedMessageable
312314

313-
attr_reader super_decl: AST::Declarations::Class::Super
315+
attr_reader super_decl: super_class
314316

315-
def initialize: (AST::Declarations::Class::Super) -> void
317+
def initialize: (super_class) -> void
316318

317319
def location: () -> Location[untyped, untyped]?
318320

319321
# Confirms if `super` inherits specifies a class
320322
#
321323
# Automatically normalize the name of super.
322324
#
323-
def self.check!: (AST::Declarations::Class::Super, env: Environment) -> void
325+
def self.check!: (super_class, env: Environment) -> void
324326
end
325327

326328
class RecursiveTypeAliasError < BaseError

0 commit comments

Comments
 (0)