rbi 0.1.14 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,65 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module RBI
5
+ module Rewriters
6
+ # Flattens visibility nodes into method nodes
7
+ #
8
+ # Example:
9
+ # ~~~rb
10
+ # class A
11
+ # def m1; end
12
+ # private
13
+ # def m2; end
14
+ # def m3; end
15
+ # end
16
+ # ~~~
17
+ #
18
+ # will be transformed into:
19
+ #
20
+ # ~~~rb
21
+ # class A
22
+ # def m1; end
23
+ # private def m2; end
24
+ # private def m3; end
25
+ # end
26
+ # ~~~
27
+ class FlattenVisibilities < Visitor
28
+ extend T::Sig
29
+
30
+ sig { void }
31
+ def initialize
32
+ super
33
+
34
+ @current_visibility = T.let([Public.new], T::Array[Visibility])
35
+ end
36
+
37
+ sig { override.params(node: T.nilable(Node)).void }
38
+ def visit(node)
39
+ return unless node
40
+
41
+ case node
42
+ when Public, Protected, Private
43
+ @current_visibility[-1] = node
44
+ node.detach
45
+ when Tree
46
+ @current_visibility << Public.new
47
+ visit_all(node.nodes.dup)
48
+ @current_visibility.pop
49
+ when Attr, Method
50
+ node.visibility = T.must(@current_visibility.last)
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ class Tree
57
+ extend T::Sig
58
+
59
+ sig { void }
60
+ def flatten_visibilities!
61
+ visitor = Rewriters::FlattenVisibilities.new
62
+ visitor.visit(self)
63
+ end
64
+ end
65
+ end
@@ -359,6 +359,12 @@ module RBI
359
359
  case self
360
360
  when Module
361
361
  Module.new(name, loc: loc, comments: comments)
362
+ when TEnum
363
+ TEnum.new(name, loc: loc, comments: comments)
364
+ when TEnumBlock
365
+ TEnumBlock.new(loc: loc, comments: comments)
366
+ when TStruct
367
+ TStruct.new(name, loc: loc, comments: comments)
362
368
  when Class
363
369
  Class.new(name, superclass_name: superclass_name, loc: loc, comments: comments)
364
370
  when Struct
@@ -551,20 +557,6 @@ module RBI
551
557
  end
552
558
  end
553
559
 
554
- class TEnumBlock
555
- extend T::Sig
556
-
557
- sig { override.params(other: Node).void }
558
- def merge_with(other)
559
- return unless other.is_a?(TEnumBlock)
560
-
561
- super
562
- other.names.each do |name|
563
- names << name unless names.include?(name)
564
- end
565
- end
566
- end
567
-
568
560
  class TStructProp
569
561
  extend T::Sig
570
562
 
@@ -3,7 +3,7 @@
3
3
 
4
4
  module RBI
5
5
  module Rewriters
6
- class NestNonPublicMethods < Visitor
6
+ class NestNonPublicMembers < Visitor
7
7
  extend T::Sig
8
8
 
9
9
  sig { override.params(node: T.nilable(Node)).void }
@@ -18,7 +18,7 @@ module RBI
18
18
 
19
19
  node.nodes.dup.each do |child|
20
20
  visit(child)
21
- next unless child.is_a?(Method)
21
+ next unless child.is_a?(Attr) || child.is_a?(Method)
22
22
 
23
23
  child.detach
24
24
  case child.visibility
@@ -43,8 +43,8 @@ module RBI
43
43
  extend T::Sig
44
44
 
45
45
  sig { void }
46
- def nest_non_public_methods!
47
- visitor = Rewriters::NestNonPublicMethods.new
46
+ def nest_non_public_members!
47
+ visitor = Rewriters::NestNonPublicMembers.new
48
48
  visitor.visit(self)
49
49
  end
50
50
  end
@@ -0,0 +1,68 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module RBI
5
+ module Rewriters
6
+ # This rewriter moves top-level members into a top-level Object class
7
+ #
8
+ # Example:
9
+ # ~~~rb
10
+ # def foo; end
11
+ # attr_reader :bar
12
+ # ~~~
13
+ #
14
+ # will be rewritten to:
15
+ #
16
+ # ~~~rb
17
+ # class Object
18
+ # def foo; end
19
+ # attr_reader :bar
20
+ # end
21
+ # ~~~
22
+ class NestTopLevelMembers < Visitor
23
+ extend T::Sig
24
+
25
+ sig { void }
26
+ def initialize
27
+ super
28
+
29
+ @top_level_object_class = T.let(nil, T.nilable(Class))
30
+ end
31
+
32
+ sig { override.params(node: T.nilable(Node)).void }
33
+ def visit(node)
34
+ return unless node
35
+
36
+ case node
37
+ when Tree
38
+ visit_all(node.nodes.dup)
39
+ else
40
+ scope = node.parent_scope
41
+ unless scope
42
+ parent = node.parent_tree
43
+ raise unless parent
44
+
45
+ node.detach
46
+
47
+ unless @top_level_object_class
48
+ @top_level_object_class = Class.new("Object")
49
+ parent.nodes << @top_level_object_class
50
+ end
51
+
52
+ @top_level_object_class << node
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ class Tree
60
+ extend T::Sig
61
+
62
+ sig { void }
63
+ def nest_top_level_members!
64
+ visitor = Rewriters::NestTopLevelMembers.new
65
+ visitor.visit(self)
66
+ end
67
+ end
68
+ end