rbi 0.2.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -46,27 +46,18 @@ module RBI
46
46
  # OPERATIONS
47
47
  # ~~~
48
48
  class RemoveKnownDefinitions < Visitor
49
- extend T::Sig
50
-
51
- sig { returns(T::Array[Operation]) }
49
+ #: Array[Operation]
52
50
  attr_reader :operations
53
51
 
54
- sig { params(index: Index).void }
52
+ #: (Index index) -> void
55
53
  def initialize(index)
56
54
  super()
57
55
  @index = index
58
- @operations = T.let([], T::Array[Operation])
56
+ @operations = [] #: Array[Operation]
59
57
  end
60
58
 
61
59
  class << self
62
- extend T::Sig
63
-
64
- sig do
65
- params(
66
- tree: Tree,
67
- index: Index,
68
- ).returns([Tree, T::Array[Operation]])
69
- end
60
+ #: (Tree tree, Index index) -> [Tree, Array[Operation]]
70
61
  def remove(tree, index)
71
62
  v = RemoveKnownDefinitions.new(index)
72
63
  v.visit(tree)
@@ -74,12 +65,13 @@ module RBI
74
65
  end
75
66
  end
76
67
 
77
- sig { params(nodes: T::Array[Node]).void }
68
+ #: (Array[Node] nodes) -> void
78
69
  def visit_all(nodes)
79
70
  nodes.dup.each { |node| visit(node) }
80
71
  end
81
72
 
82
- sig { override.params(node: T.nilable(Node)).void }
73
+ # @override
74
+ #: (Node? node) -> void
83
75
  def visit(node)
84
76
  return unless node
85
77
 
@@ -98,7 +90,7 @@ module RBI
98
90
 
99
91
  private
100
92
 
101
- sig { params(node: Indexable).returns(T.nilable(Node)) }
93
+ #: (Indexable node) -> Node?
102
94
  def previous_definition_for(node)
103
95
  node.index_ids.each do |id|
104
96
  previous = @index[id].first
@@ -107,7 +99,7 @@ module RBI
107
99
  nil
108
100
  end
109
101
 
110
- sig { params(node: Node, previous: Node).returns(T::Boolean) }
102
+ #: (Node node, Node previous) -> bool
111
103
  def can_delete_node?(node, previous)
112
104
  return false unless node.class == previous.class
113
105
 
@@ -115,29 +107,27 @@ module RBI
115
107
  when Scope
116
108
  node.empty?
117
109
  when Attr
118
- previous = T.cast(previous, Attr)
110
+ previous = previous #: as Attr
119
111
  node.names == previous.names && node.sigs == previous.sigs
120
112
  when Method
121
- previous = T.cast(previous, Method)
113
+ previous = previous #: as Method
122
114
  node.params == previous.params && node.sigs == previous.sigs
123
115
  else
124
116
  true
125
117
  end
126
118
  end
127
119
 
128
- sig { params(node: Node, previous: Node).void }
120
+ #: (Node node, Node previous) -> void
129
121
  def delete_node(node, previous)
130
122
  node.detach
131
123
  @operations << Operation.new(deleted_node: node, duplicate_of: previous)
132
124
  end
133
125
 
134
126
  class Operation < T::Struct
135
- extend T::Sig
136
-
137
127
  const :deleted_node, Node
138
128
  const :duplicate_of, Node
139
129
 
140
- sig { returns(String) }
130
+ #: -> String
141
131
  def to_s
142
132
  "Deleted #{duplicate_of} at #{deleted_node.loc} (duplicate from #{duplicate_of.loc})"
143
133
  end
@@ -4,9 +4,8 @@
4
4
  module RBI
5
5
  module Rewriters
6
6
  class SortNodes < Visitor
7
- extend T::Sig
8
-
9
- sig { override.params(node: T.nilable(Node)).void }
7
+ # @override
8
+ #: (Node? node) -> void
10
9
  def visit(node)
11
10
  sort_node_names!(node) if node
12
11
 
@@ -32,7 +31,9 @@ module RBI
32
31
  next res if res && res != 0 # we can sort the nodes by their name, let's stop here
33
32
 
34
33
  # Finally, if the two nodes have the same rank and the same name or at least one node is anonymous then,
35
- T.must(original_order[a]) <=> T.must(original_order[b]) # we keep the original order
34
+ original_order_a = original_order[a] #: as !nil
35
+ original_order_b = original_order[b] #: as !nil
36
+ original_order_a <=> original_order_b # we keep the original order
36
37
  end
37
38
  end
38
39
 
@@ -41,7 +42,7 @@ module RBI
41
42
 
42
43
  private
43
44
 
44
- sig { params(node: Node).returns(Integer) }
45
+ #: (Node node) -> Integer
45
46
  def node_rank(node)
46
47
  case node
47
48
  when Group then group_rank(node.kind)
@@ -69,7 +70,7 @@ module RBI
69
70
  end
70
71
  end
71
72
 
72
- sig { params(kind: Group::Kind).returns(Integer) }
73
+ #: (Group::Kind kind) -> Integer
73
74
  def group_rank(kind)
74
75
  case kind
75
76
  when Group::Kind::Mixins then 0
@@ -90,7 +91,7 @@ module RBI
90
91
  end
91
92
  end
92
93
 
93
- sig { params(node: Node).returns(T.nilable(String)) }
94
+ #: (Node node) -> String?
94
95
  def node_name(node)
95
96
  case node
96
97
  when Module, Class, Struct, Const, Method, Helper, RequiresAncestor
@@ -102,7 +103,7 @@ module RBI
102
103
  end
103
104
  end
104
105
 
105
- sig { params(node: Node).void }
106
+ #: (Node node) -> void
106
107
  def sort_node_names!(node)
107
108
  case node
108
109
  when Attr
@@ -113,9 +114,7 @@ module RBI
113
114
  end
114
115
 
115
116
  class Tree
116
- extend T::Sig
117
-
118
- sig { void }
117
+ #: -> void
119
118
  def sort_nodes!
120
119
  visitor = Rewriters::SortNodes.new
121
120
  visitor.visit(self)
@@ -0,0 +1,87 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module RBI
5
+ module Rewriters
6
+ # Translate all RBS signature comments to Sorbet RBI signatures
7
+ class TranslateRBSSigs < Visitor
8
+ class Error < RBI::Error; end
9
+
10
+ # @override
11
+ #: (Node? node) -> void
12
+ def visit(node)
13
+ return unless node
14
+
15
+ case node
16
+ when Tree
17
+ visit_all(node.nodes)
18
+ when AttrAccessor, AttrReader, AttrWriter
19
+ rbs_comments = extract_rbs_comments(node)
20
+ rbs_comments.each do |comment|
21
+ node.sigs << translate_rbs_attr_type(node, comment)
22
+ end
23
+ when Method
24
+ rbs_comments = extract_rbs_comments(node)
25
+ rbs_comments.each do |comment|
26
+ node.sigs << translate_rbs_method_type(node, comment)
27
+ end
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ #: (Method | Attr) -> Array[RBSComment]
34
+ def extract_rbs_comments(node)
35
+ comments = node.comments.dup
36
+ node.comments.clear
37
+
38
+ rbs_sigs = [] #: Array[RBSComment]
39
+
40
+ comments.each do |comment|
41
+ case comment
42
+ when RBSComment
43
+ rbs_sigs << comment
44
+ else
45
+ node.comments << comment
46
+ end
47
+ end
48
+
49
+ rbs_sigs
50
+ end
51
+
52
+ #: (Method, RBSComment) -> Sig
53
+ def translate_rbs_method_type(node, comment)
54
+ method_type = ::RBS::Parser.parse_method_type(comment.text)
55
+ translator = RBS::MethodTypeTranslator.new(node)
56
+ translator.visit(method_type)
57
+ translator.result
58
+ end
59
+
60
+ #: (Attr, RBSComment) -> Sig
61
+ def translate_rbs_attr_type(node, comment)
62
+ attr_type = ::RBS::Parser.parse_type(comment.text)
63
+ sig = Sig.new
64
+
65
+ if node.is_a?(AttrWriter)
66
+ if node.names.size != 1
67
+ raise Error, "AttrWriter must have exactly one name"
68
+ end
69
+
70
+ name = node.names.first #: as !nil
71
+ sig.params << SigParam.new(name.to_s, RBS::TypeTranslator.translate(attr_type))
72
+ end
73
+
74
+ sig.return_type = RBS::TypeTranslator.translate(attr_type)
75
+ sig
76
+ end
77
+ end
78
+ end
79
+
80
+ class Tree
81
+ #: -> void
82
+ def translate_rbs_sigs!
83
+ visitor = Rewriters::TranslateRBSSigs.new
84
+ visitor.visit(self)
85
+ end
86
+ end
87
+ end