rbs-inline 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +10 -7
  3. data/Rakefile +12 -0
  4. data/lib/rbs/inline/annotation_parser/tokenizer.rb +361 -0
  5. data/lib/rbs/inline/annotation_parser.rb +548 -326
  6. data/lib/rbs/inline/ast/annotations.rb +446 -136
  7. data/lib/rbs/inline/ast/comment_lines.rb +32 -18
  8. data/lib/rbs/inline/ast/declarations.rb +67 -28
  9. data/lib/rbs/inline/ast/members.rb +137 -140
  10. data/lib/rbs/inline/ast/tree.rb +104 -5
  11. data/lib/rbs/inline/cli.rb +12 -12
  12. data/lib/rbs/inline/node_utils.rb +4 -0
  13. data/lib/rbs/inline/parser.rb +140 -59
  14. data/lib/rbs/inline/version.rb +1 -1
  15. data/lib/rbs/inline/writer.rb +243 -94
  16. data/lib/rbs/inline.rb +4 -0
  17. data/rbs_collection.lock.yaml +3 -7
  18. data/rbs_collection.yaml +2 -0
  19. data/sig/generated/rbs/inline/annotation_parser/tokenizer.rbs +221 -0
  20. data/sig/generated/rbs/inline/annotation_parser.rbs +148 -92
  21. data/sig/generated/rbs/inline/ast/annotations.rbs +142 -36
  22. data/sig/generated/rbs/inline/ast/comment_lines.rbs +35 -0
  23. data/sig/generated/rbs/inline/ast/declarations.rbs +29 -10
  24. data/sig/generated/rbs/inline/ast/members.rbs +33 -24
  25. data/sig/generated/rbs/inline/ast/tree.rbs +132 -0
  26. data/sig/generated/rbs/inline/cli.rbs +3 -3
  27. data/sig/generated/rbs/inline/node_utils.rbs +11 -0
  28. data/sig/generated/rbs/inline/parser.rbs +38 -18
  29. data/sig/generated/rbs/inline/version.rbs +7 -0
  30. data/sig/generated/rbs/inline/writer.rbs +104 -0
  31. data/sig/generated/rbs/inline.rbs +7 -0
  32. metadata +14 -14
  33. data/sig/rbs/inline/annotation_parser.rbs +0 -0
  34. data/sig/rbs/inline/ast/comment_lines.rbs +0 -27
  35. data/sig/rbs/inline/ast/tree.rbs +0 -98
  36. data/sig/rbs/inline/node_utils.rbs +0 -7
  37. data/sig/rbs/inline/writer.rbs +0 -27
  38. data/sig/rbs/inline.rbs +0 -41
  39. data/yard-samples/hello.rb +0 -6
  40. data/yard-samples/sample1.rb +0 -26
@@ -1,20 +1,35 @@
1
+ # rbs_inline: enabled
2
+
1
3
  module RBS
2
4
  module Inline
3
5
  class Writer
4
- attr_reader :output
5
- attr_reader :writer
6
+ # @rbs!
7
+ # interface _Content
8
+ # def <<: (RBS::AST::Declarations::t | RBS::AST::Members::t) -> void
9
+ #
10
+ # def concat: (Array[RBS::AST::Declarations::t | RBS::AST::Members::t]) -> void
11
+ # end
12
+
13
+ attr_reader :output #: String
14
+ attr_reader :writer #: RBS::Writer
6
15
 
7
- def initialize(buffer = +"")
16
+ # @rbs buffer: String
17
+ def initialize(buffer = +"") #: void
8
18
  @output = buffer
9
19
  @writer = RBS::Writer.new(out: StringIO.new(buffer))
10
20
  end
11
21
 
12
- def self.write(uses, decls)
22
+ # @rbs uses: Array[AST::Annotations::Use]
23
+ # @rbs decls: Array[AST::Declarations::t]
24
+ # @rbs rbs_decls: Array[RBS::AST::Declarations::t]
25
+ def self.write(uses, decls, rbs_decls) #: void
13
26
  writer = Writer.new()
14
- writer.write(uses, decls)
27
+ writer.write(uses, decls, rbs_decls)
15
28
  writer.output
16
29
  end
17
30
 
31
+ # @rbs *lines: String
32
+ # @rbs return: void
18
33
  def header(*lines)
19
34
  lines.each do |line|
20
35
  writer.out.puts("# " + line)
@@ -22,7 +37,12 @@ module RBS
22
37
  writer.out.puts
23
38
  end
24
39
 
25
- def write(uses, decls)
40
+ # @rbs uses: Array[AST::Annotations::Use]
41
+ # @rbs decls: Array[AST::Declarations::t]
42
+ # @rbs rbs_decls: Array[RBS::AST::Declarations::t] --
43
+ # Top level `rbs!` declarations
44
+ # @rbs return: void
45
+ def write(uses, decls, rbs_decls)
26
46
  use_dirs = uses.map do |use|
27
47
  RBS::AST::Directives::Use.new(
28
48
  clauses: use.clauses,
@@ -30,48 +50,60 @@ module RBS
30
50
  )
31
51
  end
32
52
 
33
- rbs = decls.filter_map do |decl|
34
- translate_decl(decl)
53
+ rbs = [] #: Array[RBS::AST::Declarations::t]
54
+
55
+ decls.each do |decl|
56
+ translate_decl(
57
+ decl,
58
+ rbs #: Array[RBS::AST::Declarations::t | RBS::AST::Members::t]
59
+ )
35
60
  end
36
61
 
37
- writer.write(use_dirs + rbs)
62
+ rbs.concat(rbs_decls)
63
+
64
+ writer.write(
65
+ use_dirs + rbs
66
+ )
38
67
  end
39
68
 
40
- def translate_decl(decl)
69
+ # @rbs decl: AST::Declarations::t
70
+ # @rbs rbs: _Content
71
+ # @rbs return: void
72
+ def translate_decl(decl, rbs)
41
73
  case decl
42
74
  when AST::Declarations::ClassDecl
43
- translate_class_decl(decl)
75
+ translate_class_decl(decl, rbs)
44
76
  when AST::Declarations::ModuleDecl
45
- translate_module_decl(decl)
77
+ translate_module_decl(decl, rbs)
46
78
  when AST::Declarations::ConstantDecl
47
- translate_constant_decl(decl)
79
+ translate_constant_decl(decl, rbs)
80
+ when AST::Declarations::BlockDecl
81
+ if decl.module_class_annotation
82
+ case decl.module_class_annotation
83
+ when AST::Annotations::ModuleDecl
84
+ translate_module_block_decl(decl, rbs)
85
+ when AST::Annotations::ClassDecl
86
+ translate_class_block_decl(decl, rbs)
87
+ end
88
+ end
48
89
  end
49
90
  end
50
91
 
51
- def translate_class_decl(decl)
92
+ # @rbs decl: AST::Declarations::ClassDecl
93
+ # @rbs rbs: _Content
94
+ # @rbs return: void
95
+ def translate_class_decl(decl, rbs)
52
96
  return unless decl.class_name
53
97
 
54
98
  if decl.comments
55
- comment = RBS::AST::Comment.new(string: decl.comments.content, location: nil)
99
+ comment = RBS::AST::Comment.new(string: decl.comments.content(trim: true), location: nil)
56
100
  end
57
101
 
58
102
  members = [] #: Array[RBS::AST::Members::t | RBS::AST::Declarations::t]
59
103
 
60
- decl.members.each do |member|
61
- if member.is_a?(AST::Members::Base)
62
- if rbs_member = translate_member(member)
63
- members.concat rbs_member
64
- end
65
- end
66
-
67
- if member.is_a?(AST::Declarations::Base)
68
- if rbs = translate_decl(member)
69
- members << rbs
70
- end
71
- end
72
- end
104
+ translate_members(decl.members, nil, members)
73
105
 
74
- RBS::AST::Declarations::Class.new(
106
+ rbs << RBS::AST::Declarations::Class.new(
75
107
  name: decl.class_name,
76
108
  type_params: decl.type_params,
77
109
  members: members,
@@ -82,32 +114,46 @@ module RBS
82
114
  )
83
115
  end
84
116
 
85
- def translate_module_decl(decl)
117
+ # @rbs members: Array[AST::Declarations::t | AST::Members::t]
118
+ # @rbs decl: AST::Declarations::SingletonClassDecl?
119
+ # @rbs rbs: _Content
120
+ # @rbs return: void
121
+ def translate_members(members, decl, rbs)
122
+ members.each do |member|
123
+ case member
124
+ when AST::Members::Base
125
+ translate_member(member, decl, rbs)
126
+ when AST::Declarations::SingletonClassDecl
127
+ translate_singleton_decl(member, rbs)
128
+ when AST::Declarations::BlockDecl
129
+ if member.module_class_annotation
130
+ translate_decl(member, rbs)
131
+ else
132
+ translate_members(member.members, decl, rbs)
133
+ end
134
+ when AST::Declarations::ClassDecl, AST::Declarations::ModuleDecl, AST::Declarations::ConstantDecl
135
+ translate_decl(member, rbs)
136
+ end
137
+ end
138
+ end
139
+
140
+ # @rbs decl: AST::Declarations::ModuleDecl
141
+ # @rbs rbs: _Content
142
+ # @rbs return: void
143
+ def translate_module_decl(decl, rbs)
86
144
  return unless decl.module_name
87
145
 
88
146
  if decl.comments
89
- comment = RBS::AST::Comment.new(string: decl.comments.content, location: nil)
147
+ comment = RBS::AST::Comment.new(string: decl.comments.content(trim: true), location: nil)
90
148
  end
91
149
 
92
150
  members = [] #: Array[RBS::AST::Members::t | RBS::AST::Declarations::t]
93
151
 
94
- decl.members.each do |member|
95
- if member.is_a?(AST::Members::Base)
96
- if rbs_member = translate_member(member)
97
- members.concat rbs_member
98
- end
99
- end
100
-
101
- if member.is_a?(AST::Declarations::Base)
102
- if rbs = translate_decl(member)
103
- members << rbs
104
- end
105
- end
106
- end
152
+ translate_members(decl.members, nil, members)
107
153
 
108
154
  self_types = decl.module_selfs.map { _1.constraint }.compact
109
155
 
110
- RBS::AST::Declarations::Module.new(
156
+ rbs << RBS::AST::Declarations::Module.new(
111
157
  name: decl.module_name,
112
158
  type_params: decl.type_params,
113
159
  members: members,
@@ -118,14 +164,17 @@ module RBS
118
164
  )
119
165
  end
120
166
 
121
- def translate_constant_decl(decl)
167
+ # @rbs decl: AST::Declarations::ConstantDecl
168
+ # @rbs rbs: _Content
169
+ # @rbs return: void
170
+ def translate_constant_decl(decl, rbs)
122
171
  return unless decl.constant_name
123
172
 
124
173
  if decl.comments
125
- comment = RBS::AST::Comment.new(string: decl.comments.content, location: nil)
174
+ comment = RBS::AST::Comment.new(string: decl.comments.content(trim: true), location: nil)
126
175
  end
127
176
 
128
- RBS::AST::Declarations::Constant.new(
177
+ rbs << RBS::AST::Declarations::Constant.new(
129
178
  name: decl.constant_name,
130
179
  type: decl.type,
131
180
  comment: comment,
@@ -133,78 +182,178 @@ module RBS
133
182
  )
134
183
  end
135
184
 
136
- def translate_member(member)
185
+ # @rbs decl: AST::Declarations::SingletonClassDecl
186
+ # @rbs rbs: _Content
187
+ # @rbs return: void
188
+ def translate_singleton_decl(decl, rbs)
189
+ decl.members.each do |member|
190
+ if member.is_a?(AST::Members::Base)
191
+ translate_member(member, decl, rbs)
192
+ end
193
+ end
194
+ end
195
+
196
+ # @rbs member: AST::Members::t
197
+ # @rbs decl: AST::Declarations::SingletonClassDecl? --
198
+ # The surrouding singleton class definition
199
+ # @rbs rbs: _Content
200
+ # @rbs return void
201
+ def translate_member(member, decl, rbs)
137
202
  case member
138
203
  when AST::Members::RubyDef
139
204
  if member.comments
140
- comment = RBS::AST::Comment.new(string: member.comments.content, location: nil)
205
+ comment = RBS::AST::Comment.new(string: member.comments.content(trim: true), location: nil)
141
206
  end
142
207
 
143
- if member.override_annotation
144
- return [
145
- RBS::AST::Members::MethodDefinition.new(
146
- name: member.method_name,
147
- kind: member.method_kind,
148
- overloads: [],
149
- annotations: [],
150
- location: nil,
151
- comment: comment,
152
- overloading: true,
153
- visibility: member.visibility
154
- )
155
- ]
156
- end
208
+ kind = method_kind(member, decl)
157
209
 
158
- [
159
- RBS::AST::Members::MethodDefinition.new(
210
+ if member.override_annotation
211
+ rbs << RBS::AST::Members::MethodDefinition.new(
160
212
  name: member.method_name,
161
- kind: member.method_kind,
162
- overloads: member.method_overloads,
163
- annotations: member.method_annotations,
213
+ kind: kind,
214
+ overloads: [],
215
+ annotations: [],
164
216
  location: nil,
165
217
  comment: comment,
166
- overloading: false,
218
+ overloading: true,
167
219
  visibility: member.visibility
168
220
  )
169
- ]
221
+ return
222
+ end
223
+
224
+ rbs << RBS::AST::Members::MethodDefinition.new(
225
+ name: member.method_name,
226
+ kind: kind,
227
+ overloads: member.method_overloads,
228
+ annotations: member.method_annotations,
229
+ location: nil,
230
+ comment: comment,
231
+ overloading: member.overloading?,
232
+ visibility: member.visibility
233
+ )
170
234
  when AST::Members::RubyAlias
171
235
  if member.comments
172
- comment = RBS::AST::Comment.new(string: member.comments.content, location: nil)
236
+ comment = RBS::AST::Comment.new(string: member.comments.content(trim: true), location: nil)
173
237
  end
174
238
 
175
- [
176
- RBS::AST::Members::Alias.new(
177
- new_name: member.new_name,
178
- old_name: member.old_name,
179
- kind: :instance,
180
- annotations: [],
181
- location: nil,
182
- comment: comment
183
- )
184
- ]
239
+ rbs << RBS::AST::Members::Alias.new(
240
+ new_name: member.new_name,
241
+ old_name: member.old_name,
242
+ kind: :instance,
243
+ annotations: [],
244
+ location: nil,
245
+ comment: comment
246
+ )
185
247
  when AST::Members::RubyMixin
186
- [member.rbs].compact
248
+ if m = member.rbs
249
+ rbs << m
250
+ end
187
251
  when AST::Members::RubyAttr
188
- member.rbs
252
+ if m = member.rbs
253
+ rbs.concat m
254
+ end
189
255
  when AST::Members::RubyPrivate
190
- [
191
- RBS::AST::Members::Private.new(location: nil)
192
- ]
256
+ rbs << RBS::AST::Members::Private.new(location: nil)
193
257
  when AST::Members::RubyPublic
194
- [
195
- RBS::AST::Members::Public.new(location: nil)
196
- ]
258
+ rbs << RBS::AST::Members::Public.new(location: nil)
197
259
  when AST::Members::RBSIvar
198
- [
199
- member.rbs
200
- ].compact
260
+ if m = member.rbs
261
+ rbs << m
262
+ end
201
263
  when AST::Members::RBSEmbedded
202
264
  case members = member.members
203
265
  when Array
204
- members
266
+ rbs.concat members
205
267
  end
206
268
  end
207
269
  end
270
+
271
+ private
272
+
273
+ # Returns the `kind` of the method definition
274
+ #
275
+ # ```rb
276
+ # def self.foo = () # :singleton
277
+ # class A
278
+ # class << self
279
+ # def bar = () # :singleton
280
+ # end
281
+ # end
282
+ #
283
+ # def object.foo = () # Not supported (returns :instance)
284
+ # ```
285
+ #
286
+ # @rbs member: AST::Members::RubyDef
287
+ # @rbs decl: AST::Declarations::SingletonClassDecl?
288
+ # @rbs return: RBS::AST::Members::MethodDefinition::kind
289
+ def method_kind(member, decl)
290
+ return :singleton if decl
291
+
292
+ case member.node.receiver
293
+ when Prism::SelfNode
294
+ :singleton
295
+ else
296
+ :instance
297
+ end
298
+ end
299
+
300
+ # @rbs block: AST::Declarations::BlockDecl
301
+ # @rbs rbs: _Content
302
+ # @rbs return: void
303
+ def translate_module_block_decl(block, rbs)
304
+ annotation = block.module_class_annotation
305
+ annotation.is_a?(AST::Annotations::ModuleDecl) or raise
306
+
307
+ return unless annotation.name
308
+
309
+ if block.comments
310
+ comment = RBS::AST::Comment.new(string: block.comments.content(trim: true), location: nil)
311
+ end
312
+
313
+ members = [] #: Array[RBS::AST::Members::t | RBS::AST::Declarations::t]
314
+
315
+ translate_members(block.members, nil, members)
316
+
317
+ self_types = annotation.self_types
318
+
319
+ rbs << RBS::AST::Declarations::Module.new(
320
+ name: annotation.name,
321
+ type_params: annotation.type_params,
322
+ members: members,
323
+ self_types: self_types,
324
+ annotations: [],
325
+ location: nil,
326
+ comment: comment
327
+ )
328
+ end
329
+
330
+ # @rbs block: AST::Declarations::BlockDecl
331
+ # @rbs rbs: _Content
332
+ # @rbs return: void
333
+ def translate_class_block_decl(block, rbs)
334
+ annotation = block.module_class_annotation
335
+ annotation.is_a?(AST::Annotations::ClassDecl) or raise
336
+
337
+ return unless annotation.name
338
+
339
+ if block.comments
340
+ comment = RBS::AST::Comment.new(string: block.comments.content(trim: true), location: nil)
341
+ end
342
+
343
+ members = [] #: Array[RBS::AST::Members::t | RBS::AST::Declarations::t]
344
+
345
+ translate_members(block.members, nil, members)
346
+
347
+ rbs << RBS::AST::Declarations::Class.new(
348
+ name: annotation.name,
349
+ type_params: annotation.type_params,
350
+ members: members,
351
+ super_class: annotation.super_class,
352
+ annotations: [],
353
+ location: nil,
354
+ comment: comment
355
+ )
356
+ end
208
357
  end
209
358
  end
210
359
  end
data/lib/rbs/inline.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # rbs_inline: enabled
2
3
 
3
4
  require_relative "inline/version"
4
5
 
@@ -7,6 +8,7 @@ require "strscan"
7
8
  require "rbs"
8
9
 
9
10
  require "rbs/inline/node_utils"
11
+ require "rbs/inline/annotation_parser/tokenizer"
10
12
  require "rbs/inline/annotation_parser"
11
13
  require "rbs/inline/ast/annotations"
12
14
  require "rbs/inline/ast/comment_lines"
@@ -18,5 +20,7 @@ require "rbs/inline/writer"
18
20
 
19
21
  module RBS
20
22
  module Inline
23
+ # @rbs!
24
+ # type token = [Symbol, String]
21
25
  end
22
26
  end
@@ -1,10 +1,6 @@
1
1
  ---
2
2
  path: ".gem_rbs_collection"
3
3
  gems:
4
- - name: abbrev
5
- version: '0'
6
- source:
7
- type: stdlib
8
4
  - name: fileutils
9
5
  version: '0'
10
6
  source:
@@ -38,7 +34,7 @@ gems:
38
34
  source:
39
35
  type: stdlib
40
36
  - name: prism
41
- version: 0.29.0
37
+ version: 0.30.0
42
38
  source:
43
39
  type: rubygems
44
40
  - name: rake
@@ -46,11 +42,11 @@ gems:
46
42
  source:
47
43
  type: git
48
44
  name: ruby/gem_rbs_collection
49
- revision: 7a105f52053ce1c708b605dfa9c1ab8473424036
45
+ revision: 4bf1c9687fc24cfbb30f4759653308c816f3a69f
50
46
  remote: https://github.com/ruby/gem_rbs_collection.git
51
47
  repo_dir: gems
52
48
  - name: rbs
53
- version: 3.5.0.pre.2
49
+ version: 3.5.1
54
50
  source:
55
51
  type: rubygems
56
52
  - name: rdoc
data/rbs_collection.yaml CHANGED
@@ -15,3 +15,5 @@ path: .gem_rbs_collection
15
15
 
16
16
  gems:
17
17
  - name: optparse
18
+ - name: ffi
19
+ ignore: true