rbi 0.0.1 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,71 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module RBI
5
+ module Rewriters
6
+ class AddSigTemplates < Visitor
7
+ extend T::Sig
8
+
9
+ sig { params(with_todo_comment: T::Boolean).void }
10
+ def initialize(with_todo_comment: true)
11
+ super()
12
+ @with_todo_comment = with_todo_comment
13
+ end
14
+
15
+ sig { override.params(node: T.nilable(Node)).void }
16
+ def visit(node)
17
+ case node
18
+ when Tree
19
+ visit_all(node.nodes)
20
+ when Attr
21
+ add_attr_sig(node)
22
+ when Method
23
+ add_method_sig(node)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ sig { params(attr: Attr).void }
30
+ def add_attr_sig(attr)
31
+ return unless attr.sigs.empty?
32
+ return if attr.names.size > 1
33
+
34
+ params = []
35
+ params << SigParam.new(attr.names.first.to_s, "T.untyped") if attr.is_a?(AttrWriter)
36
+
37
+ attr.sigs << Sig.new(
38
+ params: params,
39
+ return_type: "T.untyped"
40
+ )
41
+ add_todo_comment(attr)
42
+ end
43
+
44
+ sig { params(method: Method).void }
45
+ def add_method_sig(method)
46
+ return unless method.sigs.empty?
47
+
48
+ method.sigs << Sig.new(
49
+ params: method.params.map { |param| SigParam.new(param.name, "T.untyped") },
50
+ return_type: "T.untyped"
51
+ )
52
+ add_todo_comment(method)
53
+ end
54
+
55
+ sig { params(node: NodeWithComments).void }
56
+ def add_todo_comment(node)
57
+ node.comments << Comment.new("TODO: fill in signature with appropriate type information") if @with_todo_comment
58
+ end
59
+ end
60
+ end
61
+
62
+ class Tree
63
+ extend T::Sig
64
+
65
+ sig { params(with_todo_comment: T::Boolean).void }
66
+ def add_sig_templates!(with_todo_comment: true)
67
+ visitor = Rewriters::AddSigTemplates.new(with_todo_comment: with_todo_comment)
68
+ visitor.visit(self)
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,104 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module RBI
5
+ module Rewriters
6
+ class GroupNodes < Visitor
7
+ extend T::Sig
8
+
9
+ sig { override.params(node: T.nilable(Node)).void }
10
+ def visit(node)
11
+ return unless node
12
+
13
+ case node
14
+ when Tree
15
+ kinds = node.nodes.map(&:group_kind)
16
+ kinds.compact!
17
+ kinds.uniq!
18
+
19
+ groups = {}
20
+ kinds.each { |kind| groups[kind] = Group.new(kind) }
21
+
22
+ node.nodes.dup.each do |child|
23
+ visit(child)
24
+ child.detach
25
+ groups[child.group_kind] << child
26
+ end
27
+
28
+ groups.each { |_, group| node << group }
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ class Tree
35
+ extend T::Sig
36
+
37
+ sig { void }
38
+ def group_nodes!
39
+ visitor = Rewriters::GroupNodes.new
40
+ visitor.visit(self)
41
+ end
42
+ end
43
+
44
+ class Node
45
+ extend T::Sig
46
+
47
+ sig { returns(Group::Kind) }
48
+ def group_kind
49
+ case self
50
+ when Include, Extend
51
+ Group::Kind::Mixins
52
+ when Helper
53
+ Group::Kind::Helpers
54
+ when TypeMember
55
+ Group::Kind::TypeMembers
56
+ when MixesInClassMethods
57
+ Group::Kind::MixesInClassMethods
58
+ when TStructField
59
+ Group::Kind::TStructFields
60
+ when TEnumBlock
61
+ Group::Kind::TEnums
62
+ when VisibilityGroup
63
+ Group::Kind::Methods
64
+ when Method
65
+ if name == "initialize"
66
+ Group::Kind::Inits
67
+ else
68
+ Group::Kind::Methods
69
+ end
70
+ when Scope, Const
71
+ Group::Kind::Consts
72
+ else
73
+ raise "Unknown group for #{self}"
74
+ end
75
+ end
76
+ end
77
+
78
+ class Group < Tree
79
+ extend T::Sig
80
+
81
+ sig { returns(Kind) }
82
+ attr_reader :kind
83
+
84
+ sig { params(kind: Kind).void }
85
+ def initialize(kind)
86
+ super()
87
+ @kind = kind
88
+ end
89
+
90
+ class Kind < T::Enum
91
+ enums do
92
+ Mixins = new
93
+ Helpers = new
94
+ TypeMembers = new
95
+ MixesInClassMethods = new
96
+ TStructFields = new
97
+ TEnums = new
98
+ Inits = new
99
+ Methods = new
100
+ Consts = new
101
+ end
102
+ end
103
+ end
104
+ end