rbi 0.0.16 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rbi/printer.rb CHANGED
@@ -22,7 +22,7 @@ module RBI
22
22
  out: T.any(IO, StringIO),
23
23
  indent: Integer,
24
24
  print_locs: T::Boolean,
25
- max_line_length: T.nilable(Integer)
25
+ max_line_length: T.nilable(Integer),
26
26
  ).void
27
27
  end
28
28
  def initialize(out: $stdout, indent: 0, print_locs: false, max_line_length: nil)
@@ -82,6 +82,7 @@ module RBI
82
82
  sig { override.params(node: T.nilable(Node)).void }
83
83
  def visit(node)
84
84
  return unless node
85
+
85
86
  node.accept_printer(self)
86
87
  end
87
88
 
@@ -122,7 +123,7 @@ module RBI
122
123
  out: T.any(IO, StringIO),
123
124
  indent: Integer,
124
125
  print_locs: T::Boolean,
125
- max_line_length: T.nilable(Integer)
126
+ max_line_length: T.nilable(Integer),
126
127
  ).void
127
128
  end
128
129
  def print(out: $stdout, indent: 0, print_locs: false, max_line_length: nil)
@@ -149,7 +150,7 @@ module RBI
149
150
  out: T.any(IO, StringIO),
150
151
  indent: Integer,
151
152
  print_locs: T::Boolean,
152
- max_line_length: T.nilable(Integer)
153
+ max_line_length: T.nilable(Integer),
153
154
  ).void
154
155
  end
155
156
  def print(out: $stdout, indent: 0, print_locs: false, max_line_length: nil)
@@ -170,6 +171,7 @@ module RBI
170
171
  return unless previous_node
171
172
  return if previous_node.is_a?(BlankLine)
172
173
  return if previous_node.oneline? && oneline?
174
+
173
175
  v.printn
174
176
  end
175
177
 
@@ -36,7 +36,7 @@ module RBI
36
36
 
37
37
  attr.sigs << Sig.new(
38
38
  params: params,
39
- return_type: "T.untyped"
39
+ return_type: "T.untyped",
40
40
  )
41
41
  add_todo_comment(attr)
42
42
  end
@@ -47,7 +47,7 @@ module RBI
47
47
 
48
48
  method.sigs << Sig.new(
49
49
  params: method.params.map { |param| SigParam.new(param.name, "T.untyped") },
50
- return_type: "T.untyped"
50
+ return_type: "T.untyped",
51
51
  )
52
52
  add_todo_comment(method)
53
53
  end
@@ -30,6 +30,7 @@ module RBI
30
30
  sig { params(node: NodeWithComments).void }
31
31
  def annotate_node(node)
32
32
  return if node.annotations.one?(@annotation)
33
+
33
34
  node.comments << Comment.new("@#{@annotation}")
34
35
  end
35
36
 
@@ -49,7 +50,7 @@ module RBI
49
50
  visitor = Rewriters::Annotate.new(
50
51
  annotation,
51
52
  annotate_scopes: annotate_scopes,
52
- annotate_properties: annotate_properties
53
+ annotate_properties: annotate_properties,
53
54
  )
54
55
  visitor.visit(self)
55
56
  end
@@ -26,6 +26,7 @@ module RBI
26
26
  sig { params(node: NodeWithComments).void }
27
27
  def deannotate_node(node)
28
28
  return unless node.annotations.one?(@annotation)
29
+
29
30
  node.comments.reject! do |comment|
30
31
  comment.text == "@#{@annotation}"
31
32
  end
@@ -47,16 +47,20 @@ module RBI
47
47
  end
48
48
  end
49
49
 
50
- sig { params(left: Tree, right: Tree, left_name: String, right_name: String, keep: Keep).returns(MergeTree) }
51
- def self.merge_trees(left, right, left_name: "left", right_name: "right", keep: Keep::NONE)
52
- left.nest_singleton_methods!
53
- right.nest_singleton_methods!
54
- rewriter = Rewriters::Merge.new(left_name: left_name, right_name: right_name, keep: keep)
55
- rewriter.merge(left)
56
- rewriter.merge(right)
57
- tree = rewriter.tree
58
- ConflictTreeMerger.new.visit(tree)
59
- tree
50
+ class << self
51
+ extend T::Sig
52
+
53
+ sig { params(left: Tree, right: Tree, left_name: String, right_name: String, keep: Keep).returns(MergeTree) }
54
+ def merge_trees(left, right, left_name: "left", right_name: "right", keep: Keep::NONE)
55
+ left.nest_singleton_methods!
56
+ right.nest_singleton_methods!
57
+ rewriter = Rewriters::Merge.new(left_name: left_name, right_name: right_name, keep: keep)
58
+ rewriter.merge(left)
59
+ rewriter.merge(right)
60
+ tree = rewriter.tree
61
+ ConflictTreeMerger.new.visit(tree)
62
+ tree
63
+ end
60
64
  end
61
65
 
62
66
  sig { returns(MergeTree) }
@@ -293,6 +297,7 @@ module RBI
293
297
  parent = T.let(parent_tree, T.nilable(Node))
294
298
  while parent
295
299
  return parent if parent.is_a?(ConflictTree)
300
+
296
301
  parent = parent.parent_tree
297
302
  end
298
303
  nil
@@ -305,6 +310,7 @@ module RBI
305
310
  sig { override.params(other: Node).void }
306
311
  def merge_with(other)
307
312
  return unless other.is_a?(NodeWithComments)
313
+
308
314
  other.comments.each do |comment|
309
315
  comments << comment unless comments.include?(comment)
310
316
  end
@@ -332,7 +338,7 @@ module RBI
332
338
  loc: T.nilable(Loc),
333
339
  comments: T::Array[Comment],
334
340
  conflicts: T::Array[Rewriters::Merge::Conflict],
335
- block: T.nilable(T.proc.params(node: Tree).void)
341
+ block: T.nilable(T.proc.params(node: Tree).void),
336
342
  ).void
337
343
  end
338
344
  def initialize(loc: nil, comments: [], conflicts: [], &block)
@@ -406,12 +412,14 @@ module RBI
406
412
  def compatible_with?(other)
407
413
  return false unless other.is_a?(Attr)
408
414
  return false unless names == other.names
415
+
409
416
  sigs.empty? || other.sigs.empty? || sigs == other.sigs
410
417
  end
411
418
 
412
419
  sig { override.params(other: Node).void }
413
420
  def merge_with(other)
414
421
  return unless other.is_a?(Attr)
422
+
415
423
  super
416
424
  other.sigs.each do |sig|
417
425
  sigs << sig unless sigs.include?(sig)
@@ -454,12 +462,14 @@ module RBI
454
462
  return false unless other.is_a?(Method)
455
463
  return false unless name == other.name
456
464
  return false unless params == other.params
465
+
457
466
  sigs.empty? || other.sigs.empty? || sigs == other.sigs
458
467
  end
459
468
 
460
469
  sig { override.params(other: Node).void }
461
470
  def merge_with(other)
462
471
  return unless other.is_a?(Method)
472
+
463
473
  super
464
474
  other.sigs.each do |sig|
465
475
  sigs << sig unless sigs.include?(sig)
@@ -545,6 +555,7 @@ module RBI
545
555
  sig { override.params(other: Node).void }
546
556
  def merge_with(other)
547
557
  return unless other.is_a?(TEnumBlock)
558
+
548
559
  super
549
560
  other.names.each do |name|
550
561
  names << name unless names.include?(name)
@@ -625,7 +636,7 @@ module RBI
625
636
  left: Scope,
626
637
  right: Scope,
627
638
  left_name: String,
628
- right_name: String
639
+ right_name: String,
629
640
  ).void
630
641
  end
631
642
  def initialize(left:, right:, left_name: "left", right_name: "right")
@@ -19,6 +19,7 @@ module RBI
19
19
  node.nodes.dup.each do |child|
20
20
  visit(child)
21
21
  next unless child.is_a?(Method)
22
+
22
23
  child.detach
23
24
  case child.visibility
24
25
  when Protected
@@ -17,6 +17,7 @@ module RBI
17
17
  node.nodes.dup.each do |child|
18
18
  visit(child)
19
19
  next unless child.is_a?(Method) && child.is_singleton
20
+
20
21
  child.detach
21
22
  child.is_singleton = false
22
23
  singleton_class << child
@@ -48,18 +48,6 @@ module RBI
48
48
  class RemoveKnownDefinitions < Visitor
49
49
  extend T::Sig
50
50
 
51
- sig do
52
- params(
53
- tree: Tree,
54
- index: Index
55
- ).returns([Tree, T::Array[Operation]])
56
- end
57
- def self.remove(tree, index)
58
- v = RemoveKnownDefinitions.new(index)
59
- v.visit(tree)
60
- [tree, v.operations]
61
- end
62
-
63
51
  sig { returns(T::Array[Operation]) }
64
52
  attr_reader :operations
65
53
 
@@ -70,6 +58,22 @@ module RBI
70
58
  @operations = T.let([], T::Array[Operation])
71
59
  end
72
60
 
61
+ 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
70
+ def remove(tree, index)
71
+ v = RemoveKnownDefinitions.new(index)
72
+ v.visit(tree)
73
+ [tree, v.operations]
74
+ end
75
+ end
76
+
73
77
  sig { params(nodes: T::Array[Node]).void }
74
78
  def visit_all(nodes)
75
79
  nodes.dup.each { |node| visit(node) }
@@ -11,20 +11,32 @@ module RBI
11
11
  sort_node_names!(node) if node
12
12
 
13
13
  return unless node.is_a?(Tree)
14
+
14
15
  visit_all(node.nodes)
15
16
  original_order = node.nodes.map.with_index.to_h
16
- node.nodes.sort! do |a, b|
17
- # First we try to compare the nodes by their node rank (based on the node type)
18
- res = node_rank(a) <=> node_rank(b)
19
- next res if res != 0 # we can sort the nodes by their rank, let's stop here
20
17
 
21
- # Then, if the nodes ranks are the same (res == 0), we try to compare the nodes by their name
22
- res = node_name(a) <=> node_name(b)
23
- next res if res && res != 0 # we can sort the nodes by their name, let's stop here
18
+ # The child nodes could contain private/protected markers. If so, they should not be moved in the file.
19
+ # Otherwise, some methods could see their privacy change. To avoid that problem, divide the array of child
20
+ # nodes into chunks based on whether any Visibility nodes appear, and sort the chunks independently. This
21
+ # applies the ordering rules from the node_rank method as much as possible, while preserving visibility.
22
+ sorted_nodes = node.nodes.chunk do |n|
23
+ n.is_a?(Visibility)
24
+ end.flat_map do |_, nodes|
25
+ nodes.sort! do |a, b|
26
+ # First we try to compare the nodes by their node rank (based on the node type)
27
+ res = node_rank(a) <=> node_rank(b)
28
+ next res if res != 0 # we can sort the nodes by their rank, let's stop here
29
+
30
+ # Then, if the nodes ranks are the same (res == 0), we try to compare the nodes by their name
31
+ res = node_name(a) <=> node_name(b)
32
+ next res if res && res != 0 # we can sort the nodes by their name, let's stop here
24
33
 
25
- # Finally, if the two nodes have the same rank and the same name or at least one node is anonymous then,
26
- T.must(original_order[a]) <=> T.must(original_order[b]) # we keep the original order
34
+ # 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
36
+ end
27
37
  end
38
+
39
+ node.nodes.replace(sorted_nodes)
28
40
  end
29
41
 
30
42
  private
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.16"
5
+ VERSION = "0.1.0"
6
6
  end
data/lib/rbi.rb CHANGED
@@ -3,11 +3,6 @@
3
3
 
4
4
  require "sorbet-runtime"
5
5
  require "stringio"
6
-
7
- module RBI
8
- class Error < StandardError; end
9
- end
10
-
11
6
  require "rbi/loc"
12
7
  require "rbi/model"
13
8
  require "rbi/visitor"
metadata CHANGED
@@ -1,43 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.16
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandre Terrasa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-04 00:00:00.000000000 Z
11
+ date: 2023-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: ast
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: parser
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: 2.6.4.0
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: 2.6.4.0
41
13
  - !ruby/object:Gem::Dependency
42
14
  name: sorbet-runtime
43
15
  requirement: !ruby/object:Gem::Requirement
@@ -53,19 +25,19 @@ dependencies:
53
25
  - !ruby/object:Gem::Version
54
26
  version: 0.5.9204
55
27
  - !ruby/object:Gem::Dependency
56
- name: unparser
28
+ name: yarp
57
29
  requirement: !ruby/object:Gem::Requirement
58
30
  requirements:
59
31
  - - ">="
60
32
  - !ruby/object:Gem::Version
61
- version: '0'
33
+ version: 0.10.0
62
34
  type: :runtime
63
35
  prerelease: false
64
36
  version_requirements: !ruby/object:Gem::Requirement
65
37
  requirements:
66
38
  - - ">="
67
39
  - !ruby/object:Gem::Version
68
- version: '0'
40
+ version: 0.10.0
69
41
  description:
70
42
  email:
71
43
  - ruby@shopify.com
@@ -107,14 +79,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
107
79
  requirements:
108
80
  - - ">="
109
81
  - !ruby/object:Gem::Version
110
- version: 2.7.0
82
+ version: 3.0.0
111
83
  required_rubygems_version: !ruby/object:Gem::Requirement
112
84
  requirements:
113
85
  - - ">="
114
86
  - !ruby/object:Gem::Version
115
87
  version: '0'
116
88
  requirements: []
117
- rubygems_version: 3.3.3
89
+ rubygems_version: 3.4.19
118
90
  signing_key:
119
91
  specification_version: 4
120
92
  summary: RBI generation framework