rbi 0.0.8 → 0.0.12

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: 37be6daaf86810c60a865b98ac94ff089184e990e168bc405a0d167c641477ed
4
- data.tar.gz: 95de3a3ffb035c40d9dd8da3055d808b1b0ae365add26bbc0754ae997c1c09c9
3
+ metadata.gz: ff552cac7ef282e9673502662c239c6e60e370d4d9e6d137db16ed03bd3c5a32
4
+ data.tar.gz: 6ca736d5854621dde27bafffd8ad4b393d2c60d5f7b9e4eba8ab938e7f9123f4
5
5
  SHA512:
6
- metadata.gz: 3b794985ed143c257080ed69ab6ac8458e0cfcad5e988bfe412566dd2146ea009a8ceabfb274759089e3a687eb42e15b27c31a884c04e78a6b413b424d97d0b1
7
- data.tar.gz: bc41f792fd147ceb04dee6852e046a68e5f90f886ce4b2937ceaa642ef4d9d46996f1560067177acfedc52b73b60878aeb03cd64deda17be516d04516e37ed63
6
+ metadata.gz: 9540175cf17bd99421f4c0e2d81dae2a6b79a0ba3528409c1aeb0fbc4923eb01b2b9a5d6d28a17c1b7d0ac4a4b06f10775ecabc460f54ad39fa5099256554730
7
+ data.tar.gz: 75a8657c94d508c5b53793be7b53fe6570850c6088cd67fc75c85b5d42555824b248d6f2c7a62527e8e50555276eb4e32ba33bb406c948d66a1f0a11f7f16014
@@ -0,0 +1,66 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module RBI
5
+ class Formatter
6
+ extend T::Sig
7
+
8
+ sig { returns(T::Boolean) }
9
+ attr_accessor :add_sig_templates, :group_nodes, :nest_singleton_methods, :nest_non_public_methods, :sort_nodes
10
+
11
+ sig { returns(T.nilable(Integer)) }
12
+ attr_accessor :max_line_length
13
+
14
+ sig do
15
+ params(
16
+ add_sig_templates: T::Boolean,
17
+ group_nodes: T::Boolean,
18
+ max_line_length: T.nilable(Integer),
19
+ nest_singleton_methods: T::Boolean,
20
+ nest_non_public_methods: T::Boolean,
21
+ sort_nodes: T::Boolean
22
+ ).void
23
+ end
24
+ def initialize(
25
+ add_sig_templates: false,
26
+ group_nodes: false,
27
+ max_line_length: nil,
28
+ nest_singleton_methods: false,
29
+ nest_non_public_methods: false,
30
+ sort_nodes: false
31
+ )
32
+ @add_sig_templates = add_sig_templates
33
+ @group_nodes = group_nodes
34
+ @max_line_length = max_line_length
35
+ @nest_singleton_methods = nest_singleton_methods
36
+ @nest_non_public_methods = nest_non_public_methods
37
+ @sort_nodes = sort_nodes
38
+ end
39
+
40
+ sig { params(file: RBI::File).returns(String) }
41
+ def print_file(file)
42
+ format_file(file)
43
+ file.string(max_line_length: @max_line_length)
44
+ end
45
+
46
+ sig { params(tree: RBI::Tree).returns(String) }
47
+ def print_tree(tree)
48
+ format_tree(tree)
49
+ tree.string(max_line_length: @max_line_length)
50
+ end
51
+
52
+ sig { params(file: RBI::File).void }
53
+ def format_file(file)
54
+ format_tree(file.root)
55
+ end
56
+
57
+ sig { params(tree: RBI::Tree).void }
58
+ def format_tree(tree)
59
+ tree.add_sig_templates! if @add_sig_templates
60
+ tree.nest_singleton_methods! if @nest_singleton_methods
61
+ tree.nest_non_public_methods! if @nest_non_public_methods
62
+ tree.group_nodes! if @group_nodes
63
+ tree.sort_nodes! if @sort_nodes
64
+ end
65
+ end
66
+ end
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
@@ -1026,7 +1026,7 @@ module RBI
1026
1026
  attr_accessor :return_type
1027
1027
 
1028
1028
  sig { returns(T::Boolean) }
1029
- attr_accessor :is_abstract, :is_override, :is_overridable
1029
+ attr_accessor :is_abstract, :is_override, :is_overridable, :is_final
1030
1030
 
1031
1031
  sig { returns(T::Array[String]) }
1032
1032
  attr_reader :type_params
@@ -1041,6 +1041,7 @@ module RBI
1041
1041
  is_abstract: T::Boolean,
1042
1042
  is_override: T::Boolean,
1043
1043
  is_overridable: T::Boolean,
1044
+ is_final: T::Boolean,
1044
1045
  type_params: T::Array[String],
1045
1046
  checked: T.nilable(Symbol),
1046
1047
  loc: T.nilable(Loc),
@@ -1053,6 +1054,7 @@ module RBI
1053
1054
  is_abstract: false,
1054
1055
  is_override: false,
1055
1056
  is_overridable: false,
1057
+ is_final: false,
1056
1058
  type_params: [],
1057
1059
  checked: nil,
1058
1060
  loc: nil,
@@ -1064,6 +1066,7 @@ module RBI
1064
1066
  @is_abstract = is_abstract
1065
1067
  @is_override = is_override
1066
1068
  @is_overridable = is_overridable
1069
+ @is_final = is_final
1067
1070
  @type_params = type_params
1068
1071
  @checked = checked
1069
1072
  block&.call(self)
@@ -1078,7 +1081,7 @@ module RBI
1078
1081
  def ==(other)
1079
1082
  return false unless other.is_a?(Sig)
1080
1083
  params == other.params && return_type == other.return_type && is_abstract == other.is_abstract &&
1081
- is_override == other.is_override && is_overridable == other.is_overridable &&
1084
+ is_override == other.is_override && is_overridable == other.is_overridable && is_final == other.is_final &&
1082
1085
  type_params == other.type_params && checked == other.checked
1083
1086
  end
1084
1087
  end
@@ -1361,4 +1364,28 @@ module RBI
1361
1364
  "#{parent_scope&.fully_qualified_name}.mixes_in_class_methods(#{names.join(", ")})"
1362
1365
  end
1363
1366
  end
1367
+
1368
+ class RequiresAncestor < NodeWithComments
1369
+ extend T::Sig
1370
+
1371
+ sig { returns(String) }
1372
+ attr_reader :name
1373
+
1374
+ sig do
1375
+ params(
1376
+ name: String,
1377
+ loc: T.nilable(Loc),
1378
+ comments: T::Array[Comment]
1379
+ ).void
1380
+ end
1381
+ def initialize(name, loc: nil, comments: [])
1382
+ super(loc: loc, comments: comments)
1383
+ @name = name
1384
+ end
1385
+
1386
+ sig { override.returns(String) }
1387
+ def to_s
1388
+ "#{parent_scope&.fully_qualified_name}.requires_ancestor(#{name})"
1389
+ end
1390
+ end
1364
1391
  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)
@@ -289,7 +289,14 @@ module RBI
289
289
  names = node.children[2..-1].map { |child| parse_name(child) }
290
290
  MixesInClassMethods.new(*names, loc: loc, comments: comments)
291
291
  when :public, :protected, :private
292
- visibility = Visibility.new(method_name, loc: loc)
292
+ visibility = case method_name
293
+ when :protected
294
+ Protected.new(loc: loc)
295
+ when :private
296
+ Private.new(loc: loc)
297
+ else
298
+ Public.new(loc: loc)
299
+ end
293
300
  nested_node = node.children[2]
294
301
  case nested_node&.type
295
302
  when :def, :defs
@@ -344,6 +351,8 @@ module RBI
344
351
  parse_sig(node)
345
352
  when :enums
346
353
  parse_enum(node)
354
+ when :requires_ancestor
355
+ parse_requires_ancestor(node)
347
356
  else
348
357
  raise ParseError.new("Unsupported block node type `#{name}`", node_loc(node))
349
358
  end
@@ -427,6 +436,14 @@ module RBI
427
436
  enum
428
437
  end
429
438
 
439
+ sig { params(node: AST::Node).returns(RequiresAncestor) }
440
+ def parse_requires_ancestor(node)
441
+ name = parse_name(node.children[2])
442
+ ra = RequiresAncestor.new(name)
443
+ ra.loc = node_loc(node)
444
+ ra
445
+ end
446
+
430
447
  sig { params(node: AST::Node).returns(Loc) }
431
448
  def node_loc(node)
432
449
  Loc.from_ast_loc(@file, node.location)
@@ -551,7 +568,7 @@ module RBI
551
568
  sig { params(node: AST::Node).returns(Sig) }
552
569
  def self.build(node)
553
570
  v = SigBuilder.new
554
- v.visit_all(node.children[2..-1])
571
+ v.visit_all(node.children)
555
572
  v.current
556
573
  end
557
574
 
@@ -578,6 +595,9 @@ module RBI
578
595
  visit(node.children[0]) if node.children[0]
579
596
  name = node.children[1]
580
597
  case name
598
+ when :sig
599
+ arg = node.children[2]
600
+ @current.is_final = arg && arg.children[0] == :final
581
601
  when :abstract
582
602
  @current.is_abstract = true
583
603
  when :override
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
@@ -103,16 +117,23 @@ module RBI
103
117
  end
104
118
  end
105
119
 
106
- sig { params(out: T.any(IO, StringIO), indent: Integer, print_locs: T::Boolean).void }
107
- def print(out: $stdout, indent: 0, print_locs: false)
108
- p = Printer.new(out: out, indent: indent, print_locs: print_locs)
120
+ sig do
121
+ params(
122
+ out: T.any(IO, StringIO),
123
+ indent: Integer,
124
+ print_locs: T::Boolean,
125
+ max_line_length: T.nilable(Integer)
126
+ ).void
127
+ end
128
+ def print(out: $stdout, indent: 0, print_locs: false, max_line_length: nil)
129
+ p = Printer.new(out: out, indent: indent, print_locs: print_locs, max_line_length: max_line_length)
109
130
  p.visit_file(self)
110
131
  end
111
132
 
112
- sig { params(indent: Integer, print_locs: T::Boolean).returns(String) }
113
- def string(indent: 0, print_locs: false)
133
+ sig { params(indent: Integer, print_locs: T::Boolean, max_line_length: T.nilable(Integer)).returns(String) }
134
+ def string(indent: 0, print_locs: false, max_line_length: nil)
114
135
  out = StringIO.new
115
- print(out: out, indent: indent, print_locs: print_locs)
136
+ print(out: out, indent: indent, print_locs: print_locs, max_line_length: max_line_length)
116
137
  out.string
117
138
  end
118
139
  end
@@ -123,16 +144,23 @@ module RBI
123
144
  sig { abstract.params(v: Printer).void }
124
145
  def accept_printer(v); end
125
146
 
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)
147
+ sig do
148
+ params(
149
+ out: T.any(IO, StringIO),
150
+ indent: Integer,
151
+ print_locs: T::Boolean,
152
+ max_line_length: T.nilable(Integer)
153
+ ).void
154
+ end
155
+ def print(out: $stdout, indent: 0, print_locs: false, max_line_length: nil)
156
+ p = Printer.new(out: out, indent: indent, print_locs: print_locs, max_line_length: max_line_length)
129
157
  p.visit(self)
130
158
  end
131
159
 
132
- sig { params(indent: Integer, print_locs: T::Boolean).returns(String) }
133
- def string(indent: 0, print_locs: false)
160
+ sig { params(indent: Integer, print_locs: T::Boolean, max_line_length: T.nilable(Integer)).returns(String) }
161
+ def string(indent: 0, print_locs: false, max_line_length: nil)
134
162
  out = StringIO.new
135
- print(out: out, indent: indent, print_locs: print_locs)
163
+ print(out: out, indent: indent, print_locs: print_locs, max_line_length: max_line_length)
136
164
  out.string
137
165
  end
138
166
 
@@ -172,7 +200,7 @@ module RBI
172
200
  end
173
201
 
174
202
  lines.each do |line|
175
- text = line.strip
203
+ text = line.rstrip
176
204
  v.printt("#")
177
205
  v.print(" #{text}") unless text.empty?
178
206
  v.printn
@@ -427,7 +455,7 @@ module RBI
427
455
 
428
456
  sig { returns(T::Array[String]) }
429
457
  def comments_lines
430
- comments.flat_map { |comment| comment.text.lines.map(&:strip) }
458
+ comments.flat_map { |comment| comment.text.lines.map(&:rstrip) }
431
459
  end
432
460
  end
433
461
 
@@ -602,66 +630,18 @@ module RBI
602
630
  sig { override.params(v: Printer).void }
603
631
  def accept_printer(v)
604
632
  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(").")
633
+ max_line_length = v.max_line_length
634
+ if oneline? && max_line_length.nil?
635
+ print_as_line(v)
636
+ elsif max_line_length
637
+ line = string(indent: v.current_indent)
638
+ if line.length <= max_line_length
639
+ v.print(line)
630
640
  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(").")
641
+ print_as_block(v)
649
642
  end
650
- end
651
- if return_type && return_type != "void"
652
- v.print("returns(#{return_type})")
653
643
  else
654
- v.print("void")
655
- end
656
- if checked
657
- v.print(".checked(:#{checked})")
658
- end
659
- if oneline?
660
- v.printn(" }")
661
- else
662
- v.printn
663
- v.dedent
664
- v.printl("end")
644
+ print_as_block(v)
665
645
  end
666
646
  end
667
647
 
@@ -674,6 +654,92 @@ module RBI
674
654
  def inline_params?
675
655
  params.all? { |p| p.comments.empty? }
676
656
  end
657
+
658
+ private
659
+
660
+ sig { returns(T::Array[String]) }
661
+ def sig_modifiers
662
+ modifiers = T.let([], T::Array[String])
663
+ modifiers << "abstract" if is_abstract
664
+ modifiers << "override" if is_override
665
+ modifiers << "overridable" if is_overridable
666
+ modifiers << "type_parameters(#{type_params.map { |type| ":#{type}" }.join(", ")})" if type_params.any?
667
+ modifiers << "checked(:#{checked})" if checked
668
+ modifiers
669
+ end
670
+
671
+ sig { params(v: Printer).void }
672
+ def print_as_line(v)
673
+ v.printt("sig")
674
+ v.print("(:final)") if is_final
675
+ v.print(" { ")
676
+ sig_modifiers.each do |modifier|
677
+ v.print("#{modifier}.")
678
+ end
679
+ unless params.empty?
680
+ v.print("params(")
681
+ params.each_with_index do |param, index|
682
+ v.print(", ") if index > 0
683
+ v.visit(param)
684
+ end
685
+ v.print(").")
686
+ end
687
+ if return_type && return_type != "void"
688
+ v.print("returns(#{return_type})")
689
+ else
690
+ v.print("void")
691
+ end
692
+ v.printn(" }")
693
+ end
694
+
695
+ sig { params(v: Printer).void }
696
+ def print_as_block(v)
697
+ modifiers = sig_modifiers
698
+
699
+ v.printl("sig do")
700
+ v.indent
701
+ if modifiers.any?
702
+ v.printl(T.must(modifiers.first))
703
+ v.indent
704
+ modifiers[1..]&.each do |modifier|
705
+ v.printl(".#{modifier}")
706
+ end
707
+ end
708
+
709
+ if params.any?
710
+ v.printt
711
+ v.print(".") if modifiers.any?
712
+ v.printn("params(")
713
+ v.indent
714
+ params.each_with_index do |param, pindex|
715
+ v.printt
716
+ v.visit(param)
717
+ v.print(",") if pindex < params.size - 1
718
+ param.comments_lines.each_with_index do |comment, cindex|
719
+ if cindex == 0
720
+ v.print(" ")
721
+ else
722
+ param.print_comment_leading_space(v, last: pindex == params.size - 1)
723
+ end
724
+ v.print("# #{comment}")
725
+ end
726
+ v.printn
727
+ end
728
+ v.dedent
729
+ v.printt(")")
730
+ end
731
+ v.printt if params.empty?
732
+ v.print(".") if modifiers.any? || params.any?
733
+ if return_type && return_type != "void"
734
+ v.print("returns(#{return_type})")
735
+ else
736
+ v.print("void")
737
+ end
738
+ v.printn
739
+ v.dedent
740
+ v.dedent if modifiers.any?
741
+ v.printl("end")
742
+ end
677
743
  end
678
744
 
679
745
  class SigParam
@@ -694,7 +760,7 @@ module RBI
694
760
 
695
761
  sig { returns(T::Array[String]) }
696
762
  def comments_lines
697
- comments.flat_map { |comment| comment.text.lines.map(&:strip) }
763
+ comments.flat_map { |comment| comment.text.lines.map(&:rstrip) }
698
764
  end
699
765
  end
700
766
 
@@ -794,4 +860,17 @@ module RBI
794
860
  false
795
861
  end
796
862
  end
863
+
864
+ class RequiresAncestor
865
+ extend T::Sig
866
+
867
+ sig { override.params(v: Printer).void }
868
+ def accept_printer(v)
869
+ print_blank_line_before(v)
870
+
871
+ v.printl("# #{loc}") if loc && v.print_locs
872
+ v.visit_all(comments)
873
+ v.printl("requires_ancestor { #{name} }")
874
+ end
875
+ end
797
876
  end
@@ -47,8 +47,12 @@ module RBI
47
47
  sig { returns(Group::Kind) }
48
48
  def group_kind
49
49
  case self
50
+ when Group
51
+ kind
50
52
  when Include, Extend
51
53
  Group::Kind::Mixins
54
+ when RequiresAncestor
55
+ Group::Kind::RequiredAncestors
52
56
  when Helper
53
57
  Group::Kind::Helpers
54
58
  when TypeMember
@@ -57,6 +61,8 @@ module RBI
57
61
  Group::Kind::MixesInClassMethods
58
62
  when Send
59
63
  Group::Kind::Sends
64
+ when Attr
65
+ Group::Kind::Attrs
60
66
  when TStructField
61
67
  Group::Kind::TStructFields
62
68
  when TEnumBlock
@@ -94,10 +100,12 @@ module RBI
94
100
  class Kind < T::Enum
95
101
  enums do
96
102
  Mixins = new
103
+ RequiredAncestors = new
97
104
  Helpers = new
98
105
  TypeMembers = new
99
106
  MixesInClassMethods = new
100
107
  Sends = new
108
+ Attrs = new
101
109
  TStructFields = new
102
110
  TEnums = new
103
111
  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,38 +63,38 @@ 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
- delete_node(node, previous) if previous && node.empty?
87
- when RBI::Tree
86
+ delete_node(node, previous) if previous && can_delete_node?(node, previous)
87
+ when Tree
88
88
  visit_all(node.nodes)
89
- when RBI::Indexable
89
+ when Indexable
90
90
  previous = previous_definition_for(node)
91
- delete_node(node, previous) if previous
91
+ delete_node(node, previous) if previous && can_delete_node?(node, previous)
92
92
  end
93
93
  end
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
@@ -103,7 +103,25 @@ module RBI
103
103
  nil
104
104
  end
105
105
 
106
- sig { params(node: RBI::Node, previous: RBI::Node).void }
106
+ sig { params(node: Node, previous: Node).returns(T::Boolean) }
107
+ def can_delete_node?(node, previous)
108
+ return false unless node.class == previous.class
109
+
110
+ case node
111
+ when Scope
112
+ node.empty?
113
+ when Attr
114
+ previous = T.cast(previous, Attr)
115
+ node.names == previous.names && node.sigs == previous.sigs
116
+ when Method
117
+ previous = T.cast(previous, Method)
118
+ node.params == previous.params && node.sigs == previous.sigs
119
+ else
120
+ true
121
+ end
122
+ end
123
+
124
+ sig { params(node: Node, previous: Node).void }
107
125
  def delete_node(node, previous)
108
126
  node.detach
109
127
  @operations << Operation.new(deleted_node: node, duplicate_of: previous)
@@ -112,8 +130,8 @@ module RBI
112
130
  class Operation < T::Struct
113
131
  extend T::Sig
114
132
 
115
- const :deleted_node, RBI::Node
116
- const :duplicate_of, RBI::Node
133
+ const :deleted_node, Node
134
+ const :duplicate_of, Node
117
135
 
118
136
  sig { returns(String) }
119
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.8"
5
+ VERSION = "0.0.12"
6
6
  end
data/lib/rbi.rb CHANGED
@@ -23,4 +23,5 @@ require "rbi/rewriters/remove_known_definitions"
23
23
  require "rbi/rewriters/sort_nodes"
24
24
  require "rbi/parser"
25
25
  require "rbi/printer"
26
+ require "rbi/formatter"
26
27
  require "rbi/version"
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.8
4
+ version: 0.0.12
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-24 00:00:00.000000000 Z
11
+ date: 2022-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ast
@@ -77,6 +77,7 @@ files:
77
77
  - README.md
78
78
  - Rakefile
79
79
  - lib/rbi.rb
80
+ - lib/rbi/formatter.rb
80
81
  - lib/rbi/index.rb
81
82
  - lib/rbi/loc.rb
82
83
  - lib/rbi/model.rb