rbi 0.0.6 → 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: 1232511f52f703972d6946a815937759221dcae1b15f762dad7e214760c5551b
4
- data.tar.gz: 66df06e9f8f8975b6260eca5b3a8756a5126fa6f76e3ffcdd2a9d58cf4f179a2
3
+ metadata.gz: 97c295f819a7f55175bf02dc1722a7a6ad29c82f92c7f9be2636a47652d0b39f
4
+ data.tar.gz: f0ab6cb29d0e2a1a0ef78452543258761664d081845b042c0f5f1fc2f74f9773
5
5
  SHA512:
6
- metadata.gz: ec7d1e8a75d67d1ff57f13d29e60e25c71f650ef0b948e0b617ab9bed4a4fc7ccbf0cb44d4746532077378dfc848a8eef9a4e244e082e7aef3e6971036a8f7c5
7
- data.tar.gz: ac6d329c267dd116df762972b7baa4f3d43277b4a26e3c05e42290a55aca33182149d9361ca4622d50bc63587661f4d99af284626ee72f7f809f8d7f3c78c251
6
+ metadata.gz: cb97cd66fd4231f3609efede4b98f8590cf7fff1382b4dbae1997161a8b2aed5ed10e6bc388d8388a16dc71792ba5b5ec32561b37f353cc8b77cbfb778542a60
7
+ data.tar.gz: a44902b6c3a669c0d706952459b2ebdbe78bbfdcf75ed50b0d16274f9fafa2cba20adb7ce0b70bf2011765edcfe9ea4cf823bb973618e4babd091fdc5e3fafd8
data/Gemfile CHANGED
@@ -13,5 +13,5 @@ group(:development, :test) do
13
13
  gem("rubocop-shopify", require: false)
14
14
  gem("rubocop-sorbet", require: false)
15
15
  gem("sorbet", ">= 0.5.9204", require: false)
16
- gem("tapioca", require: false, github: "Shopify/tapioca", branch: "master")
16
+ gem("tapioca", "0.5.2", require: false)
17
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
@@ -178,7 +178,7 @@ module RBI
178
178
 
179
179
  if last_line && comment_line > last_line + 1
180
180
  # Preserve empty lines in file headers
181
- tree.comments << EmptyComment.new(loc: loc)
181
+ tree.comments << BlankLine.new(loc: loc)
182
182
  end
183
183
 
184
184
  tree.comments << Comment.new(text, loc: loc)
data/lib/rbi/printer.rb CHANGED
@@ -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
@@ -171,7 +180,7 @@ module RBI
171
180
  end
172
181
  end
173
182
 
174
- class EmptyComment
183
+ class BlankLine
175
184
  extend T::Sig
176
185
 
177
186
  sig { override.params(v: Printer).void }
@@ -201,8 +210,7 @@ module RBI
201
210
 
202
211
  sig { override.params(v: Printer).void }
203
212
  def accept_printer(v)
204
- previous_node = v.previous_node
205
- v.printn if previous_node && (!previous_node.oneline? || !oneline?)
213
+ print_blank_line_before(v)
206
214
 
207
215
  v.printl("# #{loc}") if loc && v.print_locs
208
216
  v.visit_all(comments)
@@ -295,8 +303,7 @@ module RBI
295
303
 
296
304
  sig { override.params(v: Printer).void }
297
305
  def accept_printer(v)
298
- previous_node = v.previous_node
299
- v.printn if previous_node && (!previous_node.oneline? || !oneline?)
306
+ print_blank_line_before(v)
300
307
 
301
308
  v.printl("# #{loc}") if loc && v.print_locs
302
309
  v.visit_all(comments)
@@ -309,8 +316,7 @@ module RBI
309
316
 
310
317
  sig { override.params(v: Printer).void }
311
318
  def accept_printer(v)
312
- previous_node = v.previous_node
313
- v.printn if previous_node && (!previous_node.oneline? || !oneline?)
319
+ print_blank_line_before(v)
314
320
 
315
321
  v.visit_all(comments)
316
322
  sigs.each { |sig| v.visit(sig) }
@@ -346,8 +352,7 @@ module RBI
346
352
 
347
353
  sig { override.params(v: Printer).void }
348
354
  def accept_printer(v)
349
- previous_node = v.previous_node
350
- v.printn if previous_node && (!previous_node.oneline? || !oneline?)
355
+ print_blank_line_before(v)
351
356
 
352
357
  v.visit_all(comments)
353
358
  v.visit_all(sigs)
@@ -521,8 +526,7 @@ module RBI
521
526
 
522
527
  sig { override.params(v: Printer).void }
523
528
  def accept_printer(v)
524
- previous_node = v.previous_node
525
- v.printn if previous_node && (!previous_node.oneline? || !oneline?)
529
+ print_blank_line_before(v)
526
530
 
527
531
  v.printl("# #{loc}") if loc && v.print_locs
528
532
  v.visit_all(comments)
@@ -543,8 +547,7 @@ module RBI
543
547
 
544
548
  sig { override.params(v: Printer).void }
545
549
  def accept_printer(v)
546
- previous_node = v.previous_node
547
- v.printn if previous_node && (!previous_node.oneline? || !oneline?)
550
+ print_blank_line_before(v)
548
551
 
549
552
  v.printl("# #{loc}") if loc && v.print_locs
550
553
  v.visit_all(comments)
@@ -659,8 +662,7 @@ module RBI
659
662
 
660
663
  sig { override.params(v: Printer).void }
661
664
  def accept_printer(v)
662
- previous_node = v.previous_node
663
- v.printn if previous_node && (!previous_node.oneline? || !oneline?)
665
+ print_blank_line_before(v)
664
666
 
665
667
  v.printl("# #{loc}") if loc && v.print_locs
666
668
  v.visit_all(comments)
@@ -699,8 +701,7 @@ module RBI
699
701
 
700
702
  sig { override.params(v: Printer).void }
701
703
  def accept_printer(v)
702
- previous_node = v.previous_node
703
- v.printn if previous_node && (!previous_node.oneline? || !oneline?)
704
+ print_blank_line_before(v)
704
705
 
705
706
  v.printl("# #{loc}") if loc && v.print_locs
706
707
  v.visit_all(comments)
@@ -713,8 +714,7 @@ module RBI
713
714
 
714
715
  sig { override.params(v: Printer).void }
715
716
  def accept_printer(v)
716
- previous_node = v.previous_node
717
- v.printn if previous_node && (!previous_node.oneline? || !oneline?)
717
+ print_blank_line_before(v)
718
718
 
719
719
  v.printl("# #{loc}") if loc && v.print_locs
720
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,7 +47,7 @@ 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
52
  left.nest_singleton_methods!
53
53
  right.nest_singleton_methods!
@@ -59,7 +59,7 @@ module RBI
59
59
  tree
60
60
  end
61
61
 
62
- sig { returns(Tree) }
62
+ sig { returns(MergeTree) }
63
63
  attr_reader :tree
64
64
 
65
65
  sig { params(left_name: String, right_name: String, keep: Keep).void }
@@ -67,15 +67,15 @@ module RBI
67
67
  @left_name = left_name
68
68
  @right_name = right_name
69
69
  @keep = keep
70
- @tree = T.let(Tree.new, Tree)
70
+ @tree = T.let(MergeTree.new, MergeTree)
71
71
  @scope_stack = T.let([@tree], T::Array[Tree])
72
72
  end
73
73
 
74
- sig { params(tree: Tree).returns(T::Array[Conflict]) }
74
+ sig { params(tree: Tree).void }
75
75
  def merge(tree)
76
76
  v = TreeMerger.new(@tree, left_name: @left_name, right_name: @right_name, keep: @keep)
77
77
  v.visit(tree)
78
- v.conflicts
78
+ @tree.conflicts.concat(v.conflicts)
79
79
  end
80
80
 
81
81
  # Used for logging / error displaying purpose
@@ -314,9 +314,31 @@ module RBI
314
314
  class Tree
315
315
  extend T::Sig
316
316
 
317
- sig { params(other: Tree).returns(Tree) }
318
- def merge(other)
319
- 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)
320
342
  end
321
343
  end
322
344
 
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.6"
5
+ VERSION = "0.0.7"
6
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.6
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-10-06 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
@@ -83,6 +83,8 @@ files:
83
83
  - lib/rbi/parser.rb
84
84
  - lib/rbi/printer.rb
85
85
  - lib/rbi/rewriters/add_sig_templates.rb
86
+ - lib/rbi/rewriters/annotate.rb
87
+ - lib/rbi/rewriters/deannotate.rb
86
88
  - lib/rbi/rewriters/group_nodes.rb
87
89
  - lib/rbi/rewriters/merge_trees.rb
88
90
  - lib/rbi/rewriters/nest_non_public_methods.rb