rbi 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e263b24187c9a9f490f5c29a249d22d2ee564a2a59aaea3f79eb62cbff8ac24f
4
- data.tar.gz: c5bd9aed9462d42b8dddef6b6c8bfe001224747b49ee1ad187721daf9ac946d3
3
+ metadata.gz: b81ff4cdb9cb4776c5592c2838906188b7db79620511303a209d6d7ae395c491
4
+ data.tar.gz: 9da4440f160522287e3e9aca18b4d535759b11ebe59fae994d6f51be473e1e2f
5
5
  SHA512:
6
- metadata.gz: 1174676242d2ed3cfdebc28bc6eb70789d249cf10c4e5d8fceab47c58024b8db4a3f4bad9609c7543e510e6d85b6f321fc0ee65ce8d4204fee417bafb1e44510
7
- data.tar.gz: 29f7a0b48aa474e3f970bba94e85fc6621ed9449f99c7249c2056a79ae5418b0c9b0e5e6ee97b37c5244e83c1514851a59a998a932e3c4ef36ef308b61036662
6
+ metadata.gz: c7b23d94ec81f396057fe0aeb60f75d46799241ec61e097096f6b65cd9d1730fe6ab3bab0442e2f44c62c82e31481d5c26d127b84715ec8fd74fcee939d96857
7
+ data.tar.gz: 1fee917b50d5931574db3159349765d5a0d3b50ed12628afdf458eb011fd37bd140025588fdceb2124afc3ff7c5e8073b45bdd2ecfc3eb601ee2be6788f0fd29
data/lib/rbi/index.rb CHANGED
@@ -151,6 +151,16 @@ module RBI
151
151
  end
152
152
  end
153
153
 
154
+ class RequiresAncestor
155
+ extend T::Sig
156
+ include Indexable
157
+
158
+ sig { override.returns(T::Array[String]) }
159
+ def index_ids
160
+ [to_s]
161
+ end
162
+ end
163
+
154
164
  class Helper
155
165
  extend T::Sig
156
166
  include Indexable
data/lib/rbi/model.rb CHANGED
@@ -1361,4 +1361,28 @@ module RBI
1361
1361
  "#{parent_scope&.fully_qualified_name}.mixes_in_class_methods(#{names.join(", ")})"
1362
1362
  end
1363
1363
  end
1364
+
1365
+ class RequiresAncestor < NodeWithComments
1366
+ extend T::Sig
1367
+
1368
+ sig { returns(String) }
1369
+ attr_reader :name
1370
+
1371
+ sig do
1372
+ params(
1373
+ name: String,
1374
+ loc: T.nilable(Loc),
1375
+ comments: T::Array[Comment]
1376
+ ).void
1377
+ end
1378
+ def initialize(name, loc: nil, comments: [])
1379
+ super(loc: loc, comments: comments)
1380
+ @name = name
1381
+ end
1382
+
1383
+ sig { override.returns(String) }
1384
+ def to_s
1385
+ "#{parent_scope&.fully_qualified_name}.requires_ancestor(#{name})"
1386
+ end
1387
+ end
1364
1388
  end
data/lib/rbi/parser.rb CHANGED
@@ -278,10 +278,10 @@ module RBI
278
278
  symbols = node.children[2..-1].map { |child| child.children[0] }
279
279
  AttrAccessor.new(*symbols, sigs: current_sigs, loc: loc, comments: comments)
280
280
  when :include
281
- names = node.children[2..-1].map { |child| parse_name(child) }
281
+ names = node.children[2..-1].map { |child| parse_expr(child) }
282
282
  Include.new(*names, loc: loc, comments: comments)
283
283
  when :extend
284
- names = node.children[2..-1].map { |child| parse_name(child) }
284
+ names = node.children[2..-1].map { |child| parse_expr(child) }
285
285
  Extend.new(*names, loc: loc, comments: comments)
286
286
  when :abstract!, :sealed!, :interface!
287
287
  Helper.new(method_name.to_s.delete_suffix("!"), loc: loc, comments: comments)
@@ -344,6 +344,8 @@ module RBI
344
344
  parse_sig(node)
345
345
  when :enums
346
346
  parse_enum(node)
347
+ when :requires_ancestor
348
+ parse_requires_ancestor(node)
347
349
  else
348
350
  raise ParseError.new("Unsupported block node type `#{name}`", node_loc(node))
349
351
  end
@@ -427,6 +429,14 @@ module RBI
427
429
  enum
428
430
  end
429
431
 
432
+ sig { params(node: AST::Node).returns(RequiresAncestor) }
433
+ def parse_requires_ancestor(node)
434
+ name = parse_name(node.children[2])
435
+ ra = RequiresAncestor.new(name)
436
+ ra.loc = node_loc(node)
437
+ ra
438
+ end
439
+
430
440
  sig { params(node: AST::Node).returns(Loc) }
431
441
  def node_loc(node)
432
442
  Loc.from_ast_loc(@file, node.location)
data/lib/rbi/printer.rb CHANGED
@@ -11,14 +11,28 @@ module RBI
11
11
  sig { returns(T.nilable(Node)) }
12
12
  attr_reader :previous_node
13
13
 
14
- sig { params(out: T.any(IO, StringIO), indent: Integer, print_locs: T::Boolean).void }
15
- def initialize(out: $stdout, indent: 0, print_locs: false)
14
+ sig { returns(Integer) }
15
+ attr_reader :current_indent
16
+
17
+ sig { returns(T.nilable(Integer)) }
18
+ attr_reader :max_line_length
19
+
20
+ sig do
21
+ params(
22
+ out: T.any(IO, StringIO),
23
+ indent: Integer,
24
+ print_locs: T::Boolean,
25
+ max_line_length: T.nilable(Integer)
26
+ ).void
27
+ end
28
+ def initialize(out: $stdout, indent: 0, print_locs: false, max_line_length: nil)
16
29
  super()
17
30
  @out = out
18
31
  @current_indent = indent
19
32
  @print_locs = print_locs
20
33
  @in_visibility_group = T.let(false, T::Boolean)
21
34
  @previous_node = T.let(nil, T.nilable(Node))
35
+ @max_line_length = max_line_length
22
36
  end
23
37
 
24
38
  # Printing
@@ -123,16 +137,23 @@ module RBI
123
137
  sig { abstract.params(v: Printer).void }
124
138
  def accept_printer(v); end
125
139
 
126
- sig { params(out: T.any(IO, StringIO), indent: Integer, print_locs: T::Boolean).void }
127
- def print(out: $stdout, indent: 0, print_locs: false)
128
- p = Printer.new(out: out, indent: indent, print_locs: print_locs)
140
+ sig do
141
+ params(
142
+ out: T.any(IO, StringIO),
143
+ indent: Integer,
144
+ print_locs: T::Boolean,
145
+ max_line_length: T.nilable(Integer)
146
+ ).void
147
+ end
148
+ def print(out: $stdout, indent: 0, print_locs: false, max_line_length: nil)
149
+ p = Printer.new(out: out, indent: indent, print_locs: print_locs, max_line_length: max_line_length)
129
150
  p.visit(self)
130
151
  end
131
152
 
132
- sig { params(indent: Integer, print_locs: T::Boolean).returns(String) }
133
- def string(indent: 0, print_locs: false)
153
+ sig { params(indent: Integer, print_locs: T::Boolean, max_line_length: T.nilable(Integer)).returns(String) }
154
+ def string(indent: 0, print_locs: false, max_line_length: nil)
134
155
  out = StringIO.new
135
- print(out: out, indent: indent, print_locs: print_locs)
156
+ print(out: out, indent: indent, print_locs: print_locs, max_line_length: max_line_length)
136
157
  out.string
137
158
  end
138
159
 
@@ -602,66 +623,18 @@ module RBI
602
623
  sig { override.params(v: Printer).void }
603
624
  def accept_printer(v)
604
625
  v.printl("# #{loc}") if loc && v.print_locs
605
- if oneline?
606
- v.printt("sig { ")
607
- else
608
- v.printl("sig do")
609
- v.indent
610
- end
611
- v.print("abstract.") if is_abstract
612
- v.print("override.") if is_override
613
- v.print("overridable.") if is_overridable
614
- unless type_params.empty?
615
- v.print("type_parameters(")
616
- type_params.each_with_index do |param, index|
617
- v.print(":#{param}")
618
- v.print(", ") if index < type_params.length - 1
619
- end
620
- v.print(").")
621
- end
622
- unless params.empty?
623
- if inline_params?
624
- v.print("params(")
625
- params.each_with_index do |param, index|
626
- v.print(", ") if index > 0
627
- v.visit(param)
628
- end
629
- v.print(").")
626
+ max_line_length = v.max_line_length
627
+ if oneline? && max_line_length.nil?
628
+ print_as_line(v)
629
+ elsif max_line_length
630
+ line = string(indent: v.current_indent)
631
+ if line.length <= max_line_length
632
+ v.print(line)
630
633
  else
631
- v.printl("params(")
632
- v.indent
633
- params.each_with_index do |param, pindex|
634
- v.printt
635
- v.visit(param)
636
- v.print(",") if pindex < params.size - 1
637
- param.comments_lines.each_with_index do |comment, cindex|
638
- if cindex == 0
639
- v.print(" ")
640
- else
641
- param.print_comment_leading_space(v, last: pindex == params.size - 1)
642
- end
643
- v.print("# #{comment}")
644
- end
645
- v.printn
646
- end
647
- v.dedent
648
- v.printt(").")
634
+ print_as_block(v)
649
635
  end
650
- end
651
- if return_type && return_type != "void"
652
- v.print("returns(#{return_type})")
653
- else
654
- v.print("void")
655
- end
656
- if checked
657
- v.print(".checked(:#{checked})")
658
- end
659
- if oneline?
660
- v.printn(" }")
661
636
  else
662
- v.printn
663
- v.dedent
664
- v.printl("end")
637
+ print_as_block(v)
665
638
  end
666
639
  end
667
640
 
@@ -674,6 +647,90 @@ module RBI
674
647
  def inline_params?
675
648
  params.all? { |p| p.comments.empty? }
676
649
  end
650
+
651
+ private
652
+
653
+ sig { returns(T::Array[String]) }
654
+ def sig_modifiers
655
+ modifiers = T.let([], T::Array[String])
656
+ modifiers << "abstract" if is_abstract
657
+ modifiers << "override" if is_override
658
+ modifiers << "overridable" if is_overridable
659
+ modifiers << "type_parameters(#{type_params.map { |type| ":#{type}" }.join(", ")})" if type_params.any?
660
+ modifiers << "checked(:#{checked})" if checked
661
+ modifiers
662
+ end
663
+
664
+ sig { params(v: Printer).void }
665
+ def print_as_line(v)
666
+ v.printt("sig { ")
667
+ sig_modifiers.each do |modifier|
668
+ v.print("#{modifier}.")
669
+ end
670
+ unless params.empty?
671
+ v.print("params(")
672
+ params.each_with_index do |param, index|
673
+ v.print(", ") if index > 0
674
+ v.visit(param)
675
+ end
676
+ v.print(").")
677
+ end
678
+ if return_type && return_type != "void"
679
+ v.print("returns(#{return_type})")
680
+ else
681
+ v.print("void")
682
+ end
683
+ v.printn(" }")
684
+ end
685
+
686
+ sig { params(v: Printer).void }
687
+ def print_as_block(v)
688
+ modifiers = sig_modifiers
689
+
690
+ v.printl("sig do")
691
+ v.indent
692
+ if modifiers.any?
693
+ v.printl(T.must(modifiers.first))
694
+ v.indent
695
+ modifiers[1..]&.each do |modifier|
696
+ v.printl(".#{modifier}")
697
+ end
698
+ end
699
+
700
+ if params.any?
701
+ v.printt
702
+ v.print(".") if modifiers.any?
703
+ v.printn("params(")
704
+ v.indent
705
+ params.each_with_index do |param, pindex|
706
+ v.printt
707
+ v.visit(param)
708
+ v.print(",") if pindex < params.size - 1
709
+ param.comments_lines.each_with_index do |comment, cindex|
710
+ if cindex == 0
711
+ v.print(" ")
712
+ else
713
+ param.print_comment_leading_space(v, last: pindex == params.size - 1)
714
+ end
715
+ v.print("# #{comment}")
716
+ end
717
+ v.printn
718
+ end
719
+ v.dedent
720
+ v.printt(")")
721
+ end
722
+ v.printt if params.empty?
723
+ v.print(".") if modifiers.any? || params.any?
724
+ if return_type && return_type != "void"
725
+ v.print("returns(#{return_type})")
726
+ else
727
+ v.print("void")
728
+ end
729
+ v.printn
730
+ v.dedent
731
+ v.dedent if modifiers.any?
732
+ v.printl("end")
733
+ end
677
734
  end
678
735
 
679
736
  class SigParam
@@ -794,4 +851,17 @@ module RBI
794
851
  false
795
852
  end
796
853
  end
854
+
855
+ class RequiresAncestor
856
+ extend T::Sig
857
+
858
+ sig { override.params(v: Printer).void }
859
+ def accept_printer(v)
860
+ print_blank_line_before(v)
861
+
862
+ v.printl("# #{loc}") if loc && v.print_locs
863
+ v.visit_all(comments)
864
+ v.printl("requires_ancestor { #{name} }")
865
+ end
866
+ end
797
867
  end
@@ -49,6 +49,8 @@ module RBI
49
49
  case self
50
50
  when Include, Extend
51
51
  Group::Kind::Mixins
52
+ when RequiresAncestor
53
+ Group::Kind::RequiredAncestors
52
54
  when Helper
53
55
  Group::Kind::Helpers
54
56
  when TypeMember
@@ -57,6 +59,8 @@ module RBI
57
59
  Group::Kind::MixesInClassMethods
58
60
  when Send
59
61
  Group::Kind::Sends
62
+ when Attr
63
+ Group::Kind::Attrs
60
64
  when TStructField
61
65
  Group::Kind::TStructFields
62
66
  when TEnumBlock
@@ -94,10 +98,12 @@ module RBI
94
98
  class Kind < T::Enum
95
99
  enums do
96
100
  Mixins = new
101
+ RequiredAncestors = new
97
102
  Helpers = new
98
103
  TypeMembers = new
99
104
  MixesInClassMethods = new
100
105
  Sends = new
106
+ Attrs = new
101
107
  TStructFields = new
102
108
  TEnums = new
103
109
  Inits = new
@@ -50,9 +50,9 @@ module RBI
50
50
 
51
51
  sig do
52
52
  params(
53
- tree: RBI::Tree,
54
- index: RBI::Index
55
- ).returns([RBI::Tree, T::Array[Operation]])
53
+ tree: Tree,
54
+ index: Index
55
+ ).returns([Tree, T::Array[Operation]])
56
56
  end
57
57
  def self.remove(tree, index)
58
58
  v = RemoveKnownDefinitions.new(index)
@@ -63,30 +63,30 @@ module RBI
63
63
  sig { returns(T::Array[Operation]) }
64
64
  attr_reader :operations
65
65
 
66
- sig { params(index: RBI::Index).void }
66
+ sig { params(index: Index).void }
67
67
  def initialize(index)
68
68
  super()
69
69
  @index = index
70
70
  @operations = T.let([], T::Array[Operation])
71
71
  end
72
72
 
73
- sig { params(nodes: T::Array[RBI::Node]).void }
73
+ sig { params(nodes: T::Array[Node]).void }
74
74
  def visit_all(nodes)
75
75
  nodes.dup.each { |node| visit(node) }
76
76
  end
77
77
 
78
- sig { override.params(node: T.nilable(RBI::Node)).void }
78
+ sig { override.params(node: T.nilable(Node)).void }
79
79
  def visit(node)
80
80
  return unless node
81
81
 
82
82
  case node
83
- when RBI::Scope
83
+ when Scope
84
84
  visit_all(node.nodes)
85
85
  previous = previous_definition_for(node)
86
86
  delete_node(node, previous) if previous && can_delete_node?(node, previous)
87
- when RBI::Tree
87
+ when Tree
88
88
  visit_all(node.nodes)
89
- when RBI::Indexable
89
+ when Indexable
90
90
  previous = previous_definition_for(node)
91
91
  delete_node(node, previous) if previous && can_delete_node?(node, previous)
92
92
  end
@@ -94,7 +94,7 @@ module RBI
94
94
 
95
95
  private
96
96
 
97
- sig { params(node: RBI::Indexable).returns(T.nilable(RBI::Node)) }
97
+ sig { params(node: Indexable).returns(T.nilable(Node)) }
98
98
  def previous_definition_for(node)
99
99
  node.index_ids.each do |id|
100
100
  previous = @index[id].first
@@ -121,7 +121,7 @@ module RBI
121
121
  end
122
122
  end
123
123
 
124
- sig { params(node: RBI::Node, previous: RBI::Node).void }
124
+ sig { params(node: Node, previous: Node).void }
125
125
  def delete_node(node, previous)
126
126
  node.detach
127
127
  @operations << Operation.new(deleted_node: node, duplicate_of: previous)
@@ -130,8 +130,8 @@ module RBI
130
130
  class Operation < T::Struct
131
131
  extend T::Sig
132
132
 
133
- const :deleted_node, RBI::Node
134
- const :duplicate_of, RBI::Node
133
+ const :deleted_node, Node
134
+ const :duplicate_of, Node
135
135
 
136
136
  sig { returns(String) }
137
137
  def to_s
@@ -8,6 +8,8 @@ module RBI
8
8
 
9
9
  sig { override.params(node: T.nilable(Node)).void }
10
10
  def visit(node)
11
+ sort_node_names!(node) if node
12
+
11
13
  return unless node.is_a?(Tree)
12
14
  visit_all(node.nodes)
13
15
  original_order = node.nodes.map.with_index.to_h
@@ -32,12 +34,14 @@ module RBI
32
34
  case node
33
35
  when Group then group_rank(node.kind)
34
36
  when Include, Extend then 10
37
+ when RequiresAncestor then 15
35
38
  when Helper then 20
36
39
  when TypeMember then 30
37
40
  when MixesInClassMethods then 40
38
41
  when Send then 50
39
42
  when TStructField then 60
40
43
  when TEnumBlock then 70
44
+ when Attr then 75
41
45
  when Method
42
46
  if node.name == "initialize"
43
47
  81
@@ -57,16 +61,18 @@ module RBI
57
61
  def group_rank(kind)
58
62
  case kind
59
63
  when Group::Kind::Mixins then 0
60
- when Group::Kind::Helpers then 1
61
- when Group::Kind::TypeMembers then 2
62
- when Group::Kind::MixesInClassMethods then 3
64
+ when Group::Kind::RequiredAncestors then 1
65
+ when Group::Kind::Helpers then 2
66
+ when Group::Kind::TypeMembers then 3
67
+ when Group::Kind::MixesInClassMethods then 4
63
68
  when Group::Kind::Sends then 5
64
69
  when Group::Kind::TStructFields then 6
65
70
  when Group::Kind::TEnums then 7
66
- when Group::Kind::Inits then 8
67
- when Group::Kind::Methods then 9
68
- when Group::Kind::SingletonClasses then 10
69
- when Group::Kind::Consts then 11
71
+ when Group::Kind::Attrs then 8
72
+ when Group::Kind::Inits then 9
73
+ when Group::Kind::Methods then 10
74
+ when Group::Kind::SingletonClasses then 11
75
+ when Group::Kind::Consts then 12
70
76
  else
71
77
  T.absurd(kind)
72
78
  end
@@ -75,8 +81,18 @@ module RBI
75
81
  sig { params(node: Node).returns(T.nilable(String)) }
76
82
  def node_name(node)
77
83
  case node
78
- when Module, Class, Struct, Const, Method, Helper, TStructField
84
+ when Module, Class, Struct, Const, Method, Helper, TStructField, RequiresAncestor
79
85
  node.name
86
+ when Attr
87
+ node.names.first.to_s
88
+ end
89
+ end
90
+
91
+ sig { params(node: Node).void }
92
+ def sort_node_names!(node)
93
+ case node
94
+ when Attr
95
+ node.names.sort!
80
96
  end
81
97
  end
82
98
  end
data/lib/rbi/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module RBI
5
- VERSION = "0.0.9"
5
+ VERSION = "0.0.10"
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandre Terrasa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-30 00:00:00.000000000 Z
11
+ date: 2022-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ast