rbi 0.0.3 → 0.0.7

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: 80af4a72c6b71d1ed72dd5eefc9c2821443cbb7ca00d458bbfd2a381e1297545
4
- data.tar.gz: 8d774a3c7094a748a6fab5623eb6da0f5097e00591696e4e22423f3de6a869ee
3
+ metadata.gz: 97c295f819a7f55175bf02dc1722a7a6ad29c82f92c7f9be2636a47652d0b39f
4
+ data.tar.gz: f0ab6cb29d0e2a1a0ef78452543258761664d081845b042c0f5f1fc2f74f9773
5
5
  SHA512:
6
- metadata.gz: 259201aa73a41ef445010bb89bd14f69df1ef107dcfdf1becda6ead491ce067d6a0cbb9520f751272a879d76d65c74bb524a67c037447f74e243fd429749fbe7
7
- data.tar.gz: b989d25962cca475a307fcf1e76c8c74d74b3079290532e7be0bd3901446dbc800242641557ddc51e3cf2977970059869228f58bef0ff372537ef934b3a46eb2
6
+ metadata.gz: cb97cd66fd4231f3609efede4b98f8590cf7fff1382b4dbae1997161a8b2aed5ed10e6bc388d8388a16dc71792ba5b5ec32561b37f353cc8b77cbfb778542a60
7
+ data.tar.gz: a44902b6c3a669c0d706952459b2ebdbe78bbfdcf75ed50b0d16274f9fafa2cba20adb7ce0b70bf2011765edcfe9ea4cf823bb973618e4babd091fdc5e3fafd8
data/Gemfile CHANGED
@@ -8,9 +8,10 @@ gemspec
8
8
  group(:development, :test) do
9
9
  gem("byebug")
10
10
  gem("minitest")
11
+ gem("rake", "~> 13.0")
11
12
  gem("rubocop", "~> 1.7", require: false)
12
13
  gem("rubocop-shopify", require: false)
13
14
  gem("rubocop-sorbet", require: false)
14
- gem("sorbet", require: false)
15
- gem("tapioca", require: false, github: "Shopify/tapioca", branch: "master")
15
+ gem("sorbet", ">= 0.5.9204", require: false)
16
+ gem("tapioca", "0.5.2", require: false)
16
17
  end
data/lib/rbi/model.rb CHANGED
@@ -66,7 +66,8 @@ module RBI
66
66
  end
67
67
  end
68
68
 
69
- class EmptyComment < Comment
69
+ # An arbitrary blank line that can be added both in trees and comments
70
+ class BlankLine < Comment
70
71
  extend T::Sig
71
72
 
72
73
  sig { params(loc: T.nilable(Loc)).void }
@@ -89,6 +90,13 @@ module RBI
89
90
  super(loc: loc)
90
91
  @comments = comments
91
92
  end
93
+
94
+ sig { returns(T::Array[String]) }
95
+ def annotations
96
+ comments
97
+ .select { |comment| comment.text.start_with?("@") }
98
+ .map { |comment| T.must(comment.text[1..]) }
99
+ end
92
100
  end
93
101
 
94
102
  class Tree < NodeWithComments
@@ -126,10 +134,10 @@ module RBI
126
134
  extend T::Sig
127
135
 
128
136
  sig { returns(Tree) }
129
- attr_reader :root
137
+ attr_accessor :root
130
138
 
131
139
  sig { returns(T.nilable(String)) }
132
- attr_reader :strictness
140
+ attr_accessor :strictness
133
141
 
134
142
  sig { returns(T::Array[Comment]) }
135
143
  attr_accessor :comments
@@ -152,6 +160,11 @@ module RBI
152
160
  def <<(node)
153
161
  @root << node
154
162
  end
163
+
164
+ sig { returns(T::Boolean) }
165
+ def empty?
166
+ @root.empty?
167
+ end
155
168
  end
156
169
 
157
170
  # Scopes
data/lib/rbi/parser.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
- require "unparser"
4
+ require "parser"
5
5
 
6
6
  module RBI
7
7
  class ParseError < StandardError
@@ -27,6 +27,12 @@ module RBI
27
27
  ::Parser::Builders::Default.emit_index = true
28
28
  ::Parser::Builders::Default.emit_arg_inside_procarg0 = true
29
29
 
30
+ sig { void }
31
+ def initialize
32
+ # Delay load unparser and only if it has not been loaded already.
33
+ require "unparser" unless defined?(::Unparser)
34
+ end
35
+
30
36
  sig { params(string: String).returns(Tree) }
31
37
  def self.parse_string(string)
32
38
  Parser.new.parse_string(string)
@@ -172,7 +178,7 @@ module RBI
172
178
 
173
179
  if last_line && comment_line > last_line + 1
174
180
  # Preserve empty lines in file headers
175
- tree.comments << EmptyComment.new(loc: loc)
181
+ tree.comments << BlankLine.new(loc: loc)
176
182
  end
177
183
 
178
184
  tree.comments << Comment.new(text, loc: loc)
data/lib/rbi/printer.rb CHANGED
@@ -97,7 +97,7 @@ module RBI
97
97
  v.visit_all(comments)
98
98
  end
99
99
 
100
- unless root.empty?
100
+ unless root.empty? && root.comments.empty?
101
101
  v.printn if strictness || !comments.empty?
102
102
  v.visit(root)
103
103
  end
@@ -136,6 +136,15 @@ module RBI
136
136
  out.string
137
137
  end
138
138
 
139
+ sig { params(v: Printer).void }
140
+ def print_blank_line_before(v)
141
+ previous_node = v.previous_node
142
+ return unless previous_node
143
+ return if previous_node.is_a?(BlankLine)
144
+ return if previous_node.oneline? && oneline?
145
+ v.printn
146
+ end
147
+
139
148
  sig { returns(T::Boolean) }
140
149
  def oneline?
141
150
  true
@@ -156,14 +165,22 @@ module RBI
156
165
 
157
166
  sig { override.params(v: Printer).void }
158
167
  def accept_printer(v)
159
- text = self.text.strip
160
- v.printt("#")
161
- v.print(" #{text}") unless text.empty?
162
- v.printn
168
+ lines = text.lines
169
+
170
+ if lines.empty?
171
+ v.printl("#")
172
+ end
173
+
174
+ lines.each do |line|
175
+ text = line.strip
176
+ v.printt("#")
177
+ v.print(" #{text}") unless text.empty?
178
+ v.printn
179
+ end
163
180
  end
164
181
  end
165
182
 
166
- class EmptyComment
183
+ class BlankLine
167
184
  extend T::Sig
168
185
 
169
186
  sig { override.params(v: Printer).void }
@@ -193,8 +210,7 @@ module RBI
193
210
 
194
211
  sig { override.params(v: Printer).void }
195
212
  def accept_printer(v)
196
- previous_node = v.previous_node
197
- v.printn if previous_node && (!previous_node.oneline? || !oneline?)
213
+ print_blank_line_before(v)
198
214
 
199
215
  v.printl("# #{loc}") if loc && v.print_locs
200
216
  v.visit_all(comments)
@@ -287,8 +303,7 @@ module RBI
287
303
 
288
304
  sig { override.params(v: Printer).void }
289
305
  def accept_printer(v)
290
- previous_node = v.previous_node
291
- v.printn if previous_node && (!previous_node.oneline? || !oneline?)
306
+ print_blank_line_before(v)
292
307
 
293
308
  v.printl("# #{loc}") if loc && v.print_locs
294
309
  v.visit_all(comments)
@@ -301,8 +316,7 @@ module RBI
301
316
 
302
317
  sig { override.params(v: Printer).void }
303
318
  def accept_printer(v)
304
- previous_node = v.previous_node
305
- v.printn if previous_node && (!previous_node.oneline? || !oneline?)
319
+ print_blank_line_before(v)
306
320
 
307
321
  v.visit_all(comments)
308
322
  sigs.each { |sig| v.visit(sig) }
@@ -338,8 +352,7 @@ module RBI
338
352
 
339
353
  sig { override.params(v: Printer).void }
340
354
  def accept_printer(v)
341
- previous_node = v.previous_node
342
- v.printn if previous_node && (!previous_node.oneline? || !oneline?)
355
+ print_blank_line_before(v)
343
356
 
344
357
  v.visit_all(comments)
345
358
  v.visit_all(sigs)
@@ -366,13 +379,14 @@ module RBI
366
379
  v.printt
367
380
  v.visit(param)
368
381
  v.print(",") if pindex < params.size - 1
369
- param.comments.each_with_index do |comment, cindex|
382
+
383
+ param.comments_lines.each_with_index do |comment, cindex|
370
384
  if cindex > 0
371
385
  param.print_comment_leading_space(v, last: pindex == params.size - 1)
372
386
  else
373
387
  v.print(" ")
374
388
  end
375
- v.print("# #{comment.text.strip}")
389
+ v.print("# #{comment}")
376
390
  end
377
391
  v.printn
378
392
  end
@@ -410,6 +424,11 @@ module RBI
410
424
  v.print(" " * (name.size + 1))
411
425
  v.print(" ") unless last
412
426
  end
427
+
428
+ sig { returns(T::Array[String]) }
429
+ def comments_lines
430
+ comments.flat_map { |comment| comment.text.lines.map(&:strip) }
431
+ end
413
432
  end
414
433
 
415
434
  class OptParam
@@ -507,8 +526,7 @@ module RBI
507
526
 
508
527
  sig { override.params(v: Printer).void }
509
528
  def accept_printer(v)
510
- previous_node = v.previous_node
511
- v.printn if previous_node && (!previous_node.oneline? || !oneline?)
529
+ print_blank_line_before(v)
512
530
 
513
531
  v.printl("# #{loc}") if loc && v.print_locs
514
532
  v.visit_all(comments)
@@ -529,8 +547,7 @@ module RBI
529
547
 
530
548
  sig { override.params(v: Printer).void }
531
549
  def accept_printer(v)
532
- previous_node = v.previous_node
533
- v.printn if previous_node && (!previous_node.oneline? || !oneline?)
550
+ print_blank_line_before(v)
534
551
 
535
552
  v.printl("# #{loc}") if loc && v.print_locs
536
553
  v.visit_all(comments)
@@ -576,13 +593,13 @@ module RBI
576
593
  v.printt
577
594
  v.visit(param)
578
595
  v.print(",") if pindex < params.size - 1
579
- param.comments.each_with_index do |comment, cindex|
596
+ param.comments_lines.each_with_index do |comment, cindex|
580
597
  if cindex == 0
581
598
  v.print(" ")
582
599
  else
583
600
  param.print_comment_leading_space(v, last: pindex == params.size - 1)
584
601
  end
585
- v.print("# #{comment.text.strip}")
602
+ v.print("# #{comment}")
586
603
  end
587
604
  v.printn
588
605
  end
@@ -633,6 +650,11 @@ module RBI
633
650
  v.print(" " * (name.size + type.size + 3))
634
651
  v.print(" ") unless last
635
652
  end
653
+
654
+ sig { returns(T::Array[String]) }
655
+ def comments_lines
656
+ comments.flat_map { |comment| comment.text.lines.map(&:strip) }
657
+ end
636
658
  end
637
659
 
638
660
  class TStructField
@@ -640,8 +662,7 @@ module RBI
640
662
 
641
663
  sig { override.params(v: Printer).void }
642
664
  def accept_printer(v)
643
- previous_node = v.previous_node
644
- v.printn if previous_node && (!previous_node.oneline? || !oneline?)
665
+ print_blank_line_before(v)
645
666
 
646
667
  v.printl("# #{loc}") if loc && v.print_locs
647
668
  v.visit_all(comments)
@@ -680,8 +701,7 @@ module RBI
680
701
 
681
702
  sig { override.params(v: Printer).void }
682
703
  def accept_printer(v)
683
- previous_node = v.previous_node
684
- v.printn if previous_node && (!previous_node.oneline? || !oneline?)
704
+ print_blank_line_before(v)
685
705
 
686
706
  v.printl("# #{loc}") if loc && v.print_locs
687
707
  v.visit_all(comments)
@@ -694,8 +714,7 @@ module RBI
694
714
 
695
715
  sig { override.params(v: Printer).void }
696
716
  def accept_printer(v)
697
- previous_node = v.previous_node
698
- v.printn if previous_node && (!previous_node.oneline? || !oneline?)
717
+ print_blank_line_before(v)
699
718
 
700
719
  v.printl("# #{loc}") if loc && v.print_locs
701
720
  v.visit_all(comments)
@@ -0,0 +1,57 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module RBI
5
+ module Rewriters
6
+ class Annotate < Visitor
7
+ extend T::Sig
8
+
9
+ sig { params(annotation: String, annotate_scopes: T::Boolean, annotate_properties: T::Boolean).void }
10
+ def initialize(annotation, annotate_scopes: false, annotate_properties: false)
11
+ super()
12
+ @annotation = annotation
13
+ @annotate_scopes = annotate_scopes
14
+ @annotate_properties = annotate_properties
15
+ end
16
+
17
+ sig { override.params(node: T.nilable(Node)).void }
18
+ def visit(node)
19
+ case node
20
+ when Scope
21
+ annotate_node(node) if @annotate_scopes || root?(node)
22
+ when Const, Attr, Method, TStructField, TypeMember
23
+ annotate_node(node) if @annotate_properties
24
+ end
25
+ visit_all(node.nodes) if node.is_a?(Tree)
26
+ end
27
+
28
+ private
29
+
30
+ sig { params(node: NodeWithComments).void }
31
+ def annotate_node(node)
32
+ return if node.annotations.one?(@annotation)
33
+ node.comments << Comment.new("@#{@annotation}")
34
+ end
35
+
36
+ sig { params(node: Node).returns(T::Boolean) }
37
+ def root?(node)
38
+ parent = node.parent_tree
39
+ parent.is_a?(Tree) && parent.parent_tree.nil?
40
+ end
41
+ end
42
+ end
43
+
44
+ class Tree
45
+ extend T::Sig
46
+
47
+ sig { params(annotation: String, annotate_scopes: T::Boolean, annotate_properties: T::Boolean).void }
48
+ def annotate!(annotation, annotate_scopes: false, annotate_properties: false)
49
+ visitor = Rewriters::Annotate.new(
50
+ annotation,
51
+ annotate_scopes: annotate_scopes,
52
+ annotate_properties: annotate_properties
53
+ )
54
+ visitor.visit(self)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,45 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module RBI
5
+ module Rewriters
6
+ class Deannotate < Visitor
7
+ extend T::Sig
8
+
9
+ sig { params(annotation: String).void }
10
+ def initialize(annotation)
11
+ super()
12
+ @annotation = annotation
13
+ end
14
+
15
+ sig { override.params(node: T.nilable(Node)).void }
16
+ def visit(node)
17
+ case node
18
+ when Scope, Const, Attr, Method, TStructField, TypeMember
19
+ deannotate_node(node)
20
+ end
21
+ visit_all(node.nodes) if node.is_a?(Tree)
22
+ end
23
+
24
+ private
25
+
26
+ sig { params(node: NodeWithComments).void }
27
+ def deannotate_node(node)
28
+ return unless node.annotations.one?(@annotation)
29
+ node.comments.reject! do |comment|
30
+ comment.text == "@#{@annotation}"
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ class Tree
37
+ extend T::Sig
38
+
39
+ sig { params(annotation: String).void }
40
+ def deannotate!(annotation)
41
+ visitor = Rewriters::Deannotate.new(annotation)
42
+ visitor.visit(self)
43
+ end
44
+ end
45
+ end
@@ -47,8 +47,10 @@ 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(Tree) }
50
+ sig { params(left: Tree, right: Tree, left_name: String, right_name: String, keep: Keep).returns(MergeTree) }
51
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!
52
54
  rewriter = Rewriters::Merge.new(left_name: left_name, right_name: right_name, keep: keep)
53
55
  rewriter.merge(left)
54
56
  rewriter.merge(right)
@@ -57,7 +59,7 @@ module RBI
57
59
  tree
58
60
  end
59
61
 
60
- sig { returns(Tree) }
62
+ sig { returns(MergeTree) }
61
63
  attr_reader :tree
62
64
 
63
65
  sig { params(left_name: String, right_name: String, keep: Keep).void }
@@ -65,15 +67,15 @@ module RBI
65
67
  @left_name = left_name
66
68
  @right_name = right_name
67
69
  @keep = keep
68
- @tree = T.let(Tree.new, Tree)
70
+ @tree = T.let(MergeTree.new, MergeTree)
69
71
  @scope_stack = T.let([@tree], T::Array[Tree])
70
72
  end
71
73
 
72
- sig { params(tree: Tree).returns(T::Array[Conflict]) }
74
+ sig { params(tree: Tree).void }
73
75
  def merge(tree)
74
76
  v = TreeMerger.new(@tree, left_name: @left_name, right_name: @right_name, keep: @keep)
75
77
  v.visit(tree)
76
- v.conflicts
78
+ @tree.conflicts.concat(v.conflicts)
77
79
  end
78
80
 
79
81
  # Used for logging / error displaying purpose
@@ -312,9 +314,31 @@ module RBI
312
314
  class Tree
313
315
  extend T::Sig
314
316
 
315
- sig { params(other: Tree).returns(Tree) }
316
- def merge(other)
317
- Rewriters::Merge.merge_trees(self, other)
317
+ sig { params(other: Tree, left_name: String, right_name: String, keep: Rewriters::Merge::Keep).returns(MergeTree) }
318
+ def merge(other, left_name: "left", right_name: "right", keep: Rewriters::Merge::Keep::NONE)
319
+ Rewriters::Merge.merge_trees(self, other, left_name: left_name, right_name: right_name, keep: keep)
320
+ end
321
+ end
322
+
323
+ # A tree that _might_ contain conflicts
324
+ class MergeTree < Tree
325
+ extend T::Sig
326
+
327
+ sig { returns(T::Array[Rewriters::Merge::Conflict]) }
328
+ attr_reader :conflicts
329
+
330
+ sig do
331
+ params(
332
+ loc: T.nilable(Loc),
333
+ comments: T::Array[Comment],
334
+ conflicts: T::Array[Rewriters::Merge::Conflict],
335
+ block: T.nilable(T.proc.params(node: Tree).void)
336
+ ).void
337
+ end
338
+ def initialize(loc: nil, comments: [], conflicts: [], &block)
339
+ super(loc: loc, comments: comments)
340
+ @conflicts = conflicts
341
+ block&.call(self)
318
342
  end
319
343
  end
320
344
 
data/lib/rbi/version.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  # typed: true
2
- # typed: false
3
2
  # frozen_string_literal: true
4
3
 
5
4
  module RBI
6
- VERSION = "0.0.3"
5
+ VERSION = "0.0.7"
7
6
  end
data/lib/rbi.rb CHANGED
@@ -13,6 +13,8 @@ require "rbi/model"
13
13
  require "rbi/visitor"
14
14
  require "rbi/index"
15
15
  require "rbi/rewriters/add_sig_templates"
16
+ require "rbi/rewriters/annotate"
17
+ require "rbi/rewriters/deannotate"
16
18
  require "rbi/rewriters/merge_trees"
17
19
  require "rbi/rewriters/nest_singleton_methods"
18
20
  require "rbi/rewriters/nest_non_public_methods"
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.3
4
+ version: 0.0.7
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-08-24 00:00:00.000000000 Z
11
+ date: 2021-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ast
@@ -38,34 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: rake
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '13.0'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '13.0'
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: sorbet-runtime
57
43
  requirement: !ruby/object:Gem::Requirement
58
44
  requirements:
59
45
  - - ">="
60
46
  - !ruby/object:Gem::Version
61
- version: '0'
47
+ version: 0.5.9204
62
48
  type: :runtime
63
49
  prerelease: false
64
50
  version_requirements: !ruby/object:Gem::Requirement
65
51
  requirements:
66
52
  - - ">="
67
53
  - !ruby/object:Gem::Version
68
- version: '0'
54
+ version: 0.5.9204
69
55
  - !ruby/object:Gem::Dependency
70
56
  name: unparser
71
57
  requirement: !ruby/object:Gem::Requirement
@@ -97,6 +83,8 @@ files:
97
83
  - lib/rbi/parser.rb
98
84
  - lib/rbi/printer.rb
99
85
  - lib/rbi/rewriters/add_sig_templates.rb
86
+ - lib/rbi/rewriters/annotate.rb
87
+ - lib/rbi/rewriters/deannotate.rb
100
88
  - lib/rbi/rewriters/group_nodes.rb
101
89
  - lib/rbi/rewriters/merge_trees.rb
102
90
  - lib/rbi/rewriters/nest_non_public_methods.rb