rbs-inline 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 44afba2006b4ffed3719716d5ac515d474dc7ba9ee18c4f9b3298c9925441284
4
- data.tar.gz: 2c008cd99c1de1a2cb80945eed727b74fe9450af07fd3d20299775d5ee728978
3
+ metadata.gz: 713b2bd4c5ddd833fa1bd1a71505924081013101a8a41200a88fed55751146ba
4
+ data.tar.gz: 44ffe7a91c0c0afeb7e163a02de508c50f96a609ae7c64593a6b739109ac11b0
5
5
  SHA512:
6
- metadata.gz: d7d02d3e07d295c00890ac95bad4c2f7f49700a0cdddb9885dcc3c194d272ef5b387c168f675a46f2b5dc3f49aaeb5230603dbe7ebcea20087952aebb7516b66
7
- data.tar.gz: 6e476d54d6db64e42cdc7a6bfe9dfb5b1454f290e2746836cc17fe77ca41db5dac2b1a01d311877af189ee7ad2bd06207f1db2aaac86e1e494dd5855e921254d
6
+ metadata.gz: 6507f727be79bcae3890c8b8c3c3865544022058e44d5bfd060efb5df0c91e50f2b5e43a11f284c7ea3de1066bdd0ed7e269a14c20b45cc05cafa7ad60ecffb7
7
+ data.tar.gz: aad87cafa19657b4d4b055527924146cb232cd96c56081bcbb24c1e1b0ff2fc24a449bdfe2132a43c2e5f5ac2affca8d615d0946b135676979556eaca4887aec
data/README.md CHANGED
@@ -80,12 +80,12 @@ The gem works as a transpiler from annotated Ruby code to RBS files. Run `rbs-in
80
80
  $ bundle exec rbs-inline lib
81
81
 
82
82
  # Save generated RBS files under sig/generated
83
- $ bundle exec rbs-inline --output=sig/generated lib
83
+ $ bundle exec rbs-inline --output lib
84
84
  ```
85
85
 
86
86
  You may want to use `fswatch` or likes to automatically generate RBS files when you edit the Ruby code.
87
87
 
88
- $ fswatch -0 lib | xargs -0 -n1 bundle exec rbs-inline --output=sig/generated
88
+ $ fswatch -0 lib | xargs -0 -n1 bundle exec rbs-inline --output
89
89
 
90
90
  ## More materials
91
91
 
@@ -4,6 +4,27 @@ module RBS
4
4
  module Inline
5
5
  module AST
6
6
  module Annotations
7
+ # @rbs!
8
+ # type t = VarType
9
+ # | ReturnType
10
+ # | Use
11
+ # | Inherits
12
+ # | Generic
13
+ # | ModuleSelf
14
+ # | Skip
15
+ # | Assertion
16
+ # | Application
17
+ # | RBSAnnotation
18
+ # | Override
19
+ # | IvarType
20
+ # | Yields
21
+ # | Embedded
22
+ # # | Def
23
+ # # | AttrReader | AttrWriter | AttrAccessor
24
+ # # | Include | Extend | Prepend
25
+ # # | Alias
26
+
27
+
7
28
  class Base
8
29
  attr_reader :source #:: CommentLines
9
30
  attr_reader :tree #:: Tree
@@ -1,9 +1,24 @@
1
+ # rbs_inline: enabled
2
+
1
3
  module RBS
2
4
  module Inline
3
5
  module AST
6
+ # CommentLines represents consecutive comments
7
+ #
8
+ # The comments construct one String.
9
+ #
10
+ # ```ruby
11
+ # # Hello <-- Comment1
12
+ # # World <-- Comment2
13
+ # ```
14
+ #
15
+ # We want to get a String of comment1 and comment2, `"Hello\nWorld".
16
+ # And want to translate a location in the string into the location in comment1 and comment2.
17
+ #
4
18
  class CommentLines
5
- attr_reader :comments
19
+ attr_reader :comments #:: Array[[Prism::Comment, Integer]]
6
20
 
21
+ # @rbs comments: Array[Prism::Comment]
7
22
  def initialize(comments)
8
23
  offsets = comments.map do |comment|
9
24
  comment.location.slice.index(/[^#\s]/) || 1
@@ -18,10 +33,12 @@ module RBS
18
33
  end
19
34
  end
20
35
 
21
- def string
36
+ def string #:: String
22
37
  comments.map {|comment, offset| comment.location.slice[offset..] }.join("\n")
23
38
  end
24
39
 
40
+ # @rbs index: Integer
41
+ # @rbs returns [Prism::Comment, Integer]?
25
42
  def comment_location(index)
26
43
  comments.each do |comment, offset|
27
44
  comment_length = comment.location.length
@@ -15,7 +15,7 @@ module RBS
15
15
  end
16
16
  end
17
17
 
18
- # @rbs! type t = ClassDecl | ModuleDecl | ConstantDecl
18
+ # @rbs! type t = ClassDecl | ModuleDecl | ConstantDecl | SingletonClassDecl
19
19
 
20
20
  # @rbs!
21
21
  # interface _WithComments
@@ -227,6 +227,9 @@ module RBS
227
227
  node.location.start_line
228
228
  end
229
229
  end
230
+
231
+ class SingletonClassDecl < ModuleOrClass #[Prism::SingletonClassNode]
232
+ end
230
233
  end
231
234
  end
232
235
  end
@@ -4,6 +4,11 @@ module RBS
4
4
  module Inline
5
5
  module AST
6
6
  module Members
7
+ # @rbs!
8
+ # type ruby = RubyDef | RubyAlias | RubyMixin | RubyAttr | RubyPublic | RubyPrivate
9
+ # type rbs = RBSIvar | RBSEmbedded
10
+ # type t = ruby | rbs
11
+
7
12
  class Base
8
13
  attr_reader :location #:: Prism::Location
9
14
 
@@ -64,27 +69,6 @@ module RBS
64
69
  end
65
70
  end
66
71
 
67
- # Returns the `kind` of the method definition
68
- #
69
- # [FIXME] It only supports `self` receiver.
70
- #
71
- # ```rb
72
- # def self.foo = () # :singleton
73
- # def object.foo = () # Not supported (returns :instance)
74
- # ```
75
- #
76
- def method_kind #:: RBS::AST::Members::MethodDefinition::kind
77
- # FIXME: really hacky implementation
78
- case node.receiver
79
- when Prism::SelfNode
80
- :singleton
81
- when nil
82
- :instance
83
- else
84
- :instance
85
- end
86
- end
87
-
88
72
  def return_type #:: Types::t?
89
73
  if assertion
90
74
  if assertion.type?
@@ -341,6 +325,8 @@ module RBS
341
325
  end
342
326
 
343
327
  class RubyMixin < RubyBase
328
+ include Declarations::ConstantUtil
329
+
344
330
  # CallNode that calls `include`, `prepend`, and `extend` method
345
331
  attr_reader :node #:: Prism::CallNode
346
332
 
@@ -371,11 +357,8 @@ module RBS
371
357
  return unless node.arguments.arguments.size == 1
372
358
 
373
359
  arg = node.arguments.arguments[0] || raise
374
- if arg.is_a?(Prism::ConstantReadNode)
375
- type_name = RBS::TypeName.new(name: arg.name, namespace: RBS::Namespace.empty)
376
- else
377
- raise
378
- end
360
+ type_name = type_name(arg)
361
+ return unless type_name
379
362
 
380
363
  args = [] #: Array[Types::t]
381
364
  if application
@@ -1,17 +1,28 @@
1
+ # rbs_inline: enabled
2
+
1
3
  module RBS
2
4
  module Inline
3
5
  module AST
4
6
  class Tree
5
- attr_reader :trees
6
- attr_reader :type
7
- attr_reader :non_trivia_trees
7
+ # @rbs!
8
+ # type token = [Symbol, String]
9
+ # type tree = token | Tree | Types::t | MethodType | nil
10
+
11
+ attr_reader :trees #:: Array[tree]
12
+ attr_reader :type #:: Symbol
13
+
14
+ # Children but without `tWHITESPACE` tokens
15
+ attr_reader :non_trivia_trees #:: Array[tree]
8
16
 
17
+ # @rbs type: Symbol
9
18
  def initialize(type)
10
19
  @type = type
11
20
  @trees = []
12
21
  @non_trivia_trees = []
13
22
  end
14
23
 
24
+ # @rbs tok: tree
25
+ # @rbs returns self
15
26
  def <<(tok)
16
27
  trees << tok
17
28
  unless tok.is_a?(Array) && tok[0] == :tWHITESPACE
@@ -20,7 +31,8 @@ module RBS
20
31
  self
21
32
  end
22
33
 
23
- def to_s
34
+ # Returns the source code associated to the tree
35
+ def to_s #:: String
24
36
  buf = +""
25
37
 
26
38
  trees.each do |tree|
@@ -39,6 +51,12 @@ module RBS
39
51
  buf
40
52
  end
41
53
 
54
+ # Returns n-th token from the children
55
+ #
56
+ # Raises if the value is not a token or nil.
57
+ #
58
+ # @rbs index: Integer
59
+ # @rbs returns token?
42
60
  def nth_token(index)
43
61
  tok = non_trivia_trees[index]
44
62
  case tok
@@ -49,6 +67,12 @@ module RBS
49
67
  end
50
68
  end
51
69
 
70
+ # Returns n-th token from the children
71
+ #
72
+ # Returns `nil` if the value is not a token.
73
+ #
74
+ # @rbs index: Integer
75
+ # @rbs returns token?
52
76
  def nth_token?(index)
53
77
  tok = non_trivia_trees[index]
54
78
  case tok
@@ -59,10 +83,22 @@ module RBS
59
83
  end
60
84
  end
61
85
 
86
+ # Returns n-th token from the children
87
+ #
88
+ # Raises if the value is not token.
89
+ #
90
+ # @rbs index: Integer
91
+ # @rbs returns token
62
92
  def nth_token!(index)
63
93
  nth_token(index) || raise
64
94
  end
65
95
 
96
+ # Returns n-th tree from the children
97
+ #
98
+ # Raises if the value is not a tree or nil.
99
+ #
100
+ # @rbs index: Integer
101
+ # @rbs returns Tree?
66
102
  def nth_tree(index)
67
103
  tok = non_trivia_trees[index]
68
104
  case tok
@@ -73,6 +109,12 @@ module RBS
73
109
  end
74
110
  end
75
111
 
112
+ # Returns n-th tree from the children
113
+ #
114
+ # Returns `nil` if the value is not a tree or nil.
115
+ #
116
+ # @rbs index: Integer
117
+ # @rbs returns Tree?
76
118
  def nth_tree?(index)
77
119
  tok = non_trivia_trees[index]
78
120
  case tok
@@ -83,11 +125,23 @@ module RBS
83
125
  end
84
126
  end
85
127
 
128
+ # Returns n-th tree from the children
129
+ #
130
+ # Raises if the value is not a tree.
131
+ #
132
+ # @rbs index: Integer
133
+ # @rbs returns Tree
86
134
  def nth_tree!(index)
87
135
  nth_tree(index) || raise
88
136
  end
89
137
 
90
138
 
139
+ # Returns n-th type from the children
140
+ #
141
+ # Raises if the value is not a type or nil.
142
+ #
143
+ # @rbs index: Integer
144
+ # @rbs returns Types::t?
91
145
  def nth_type(index)
92
146
  tok = non_trivia_trees[index]
93
147
  case tok
@@ -98,6 +152,12 @@ module RBS
98
152
  end
99
153
  end
100
154
 
155
+ # Returns n-th type from the children
156
+ #
157
+ # Returns `nil` if the value is not a type.
158
+ #
159
+ # @rbs index: Integer
160
+ # @rbs returns Types::t?
101
161
  def nth_type?(index)
102
162
  tok = non_trivia_trees[index]
103
163
  case tok
@@ -108,10 +168,22 @@ module RBS
108
168
  end
109
169
  end
110
170
 
171
+ # Returns n-th type from the children
172
+ #
173
+ # Raises if the value is not a type.
174
+ #
175
+ # @rbs index: Integer
176
+ # @rbs returns Types::t
111
177
  def nth_type!(index)
112
178
  nth_type(index) || raise
113
179
  end
114
180
 
181
+ # Returns n-th method type from the children
182
+ #
183
+ # Raises if the value is not a method type or `nil`.
184
+ #
185
+ # @rbs index: Integer
186
+ # @rbs returns MethodType?
115
187
  def nth_method_type(index)
116
188
  tok = non_trivia_trees[index]
117
189
  case tok
@@ -122,6 +194,12 @@ module RBS
122
194
  end
123
195
  end
124
196
 
197
+ # Returns n-th method type from the children
198
+ #
199
+ # Returns `nil` if the value is not a method type.
200
+ #
201
+ # @rbs index: Integer
202
+ # @rbs returns MethodType?
125
203
  def nth_method_type?(index)
126
204
  tok = non_trivia_trees[index]
127
205
  case tok
@@ -132,6 +210,12 @@ module RBS
132
210
  end
133
211
  end
134
212
 
213
+ # Returns n-th method tree from the children
214
+ #
215
+ # Raises if the value is not a method tree.
216
+ #
217
+ # @rbs index: Integer
218
+ # @rbs returns MethodType
135
219
  def nth_method_type!(index)
136
220
  nth_method_type(index) || raise
137
221
  end
@@ -1,6 +1,10 @@
1
+ # rbs_inline: enabled
2
+
1
3
  module RBS
2
4
  module Inline
3
5
  module NodeUtils
6
+ # @rbs node: Prism::Node
7
+ # @rbs returns TypeName?
4
8
  def type_name(node)
5
9
  case node
6
10
  when Prism::ConstantReadNode
@@ -86,7 +86,7 @@ module RBS
86
86
  current_class_module_decl or raise
87
87
  end
88
88
 
89
- #:: (AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl) { () -> void } -> void
89
+ #:: (AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl | AST::Declarations::SingletonClassDecl) { () -> void } -> void
90
90
  #:: (AST::Declarations::ConstantDecl) -> void
91
91
  def push_class_module_decl(decl)
92
92
  if current = current_class_module_decl
@@ -152,6 +152,18 @@ module RBS
152
152
  load_inner_annotations(node.location.start_line, node.location.end_line, class_decl.members)
153
153
  end
154
154
 
155
+ # @rbs override
156
+ def visit_singleton_class_node(node)
157
+ return if ignored_node?(node)
158
+
159
+ associated_comment = comments.delete(node.location.start_line - 1)
160
+ singleton_decl = AST::Declarations::SingletonClassDecl.new(node, associated_comment)
161
+
162
+ push_class_module_decl(singleton_decl) do
163
+ visit node.body
164
+ end
165
+ end
166
+
155
167
  # @rbs override
156
168
  def visit_module_node(node)
157
169
  return if ignored_node?(node)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RBS
4
4
  module Inline
5
- VERSION = "0.3.0"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
@@ -1,20 +1,27 @@
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
+ attr_reader :output #:: String
7
+ attr_reader :writer #:: RBS::Writer
6
8
 
9
+ # @rbs buffer: String
7
10
  def initialize(buffer = +"")
8
11
  @output = buffer
9
12
  @writer = RBS::Writer.new(out: StringIO.new(buffer))
10
13
  end
11
14
 
15
+ # @rbs uses: Array[AST::Annotations::Use]
16
+ # @rbs decls: Array[AST::Declarations::t]
12
17
  def self.write(uses, decls)
13
18
  writer = Writer.new()
14
19
  writer.write(uses, decls)
15
20
  writer.output
16
21
  end
17
22
 
23
+ # @rbs lines: Array[String]
24
+ # @rbs returns void
18
25
  def header(*lines)
19
26
  lines.each do |line|
20
27
  writer.out.puts("# " + line)
@@ -22,6 +29,9 @@ module RBS
22
29
  writer.out.puts
23
30
  end
24
31
 
32
+ # @rbs uses: Array[AST::Annotations::Use]
33
+ # @rbs decls: Array[AST::Declarations::t]
34
+ # @rbs returns void
25
35
  def write(uses, decls)
26
36
  use_dirs = uses.map do |use|
27
37
  RBS::AST::Directives::Use.new(
@@ -37,6 +47,8 @@ module RBS
37
47
  writer.write(use_dirs + rbs)
38
48
  end
39
49
 
50
+ # @rbs decl: AST::Declarations::t
51
+ # @rbs returns RBS::AST::Declarations::t?
40
52
  def translate_decl(decl)
41
53
  case decl
42
54
  when AST::Declarations::ClassDecl
@@ -48,6 +60,8 @@ module RBS
48
60
  end
49
61
  end
50
62
 
63
+ # @rbs decl: AST::Declarations::ClassDecl
64
+ # @rbs returns RBS::AST::Declarations::Class?
51
65
  def translate_class_decl(decl)
52
66
  return unless decl.class_name
53
67
 
@@ -59,12 +73,14 @@ module RBS
59
73
 
60
74
  decl.members.each do |member|
61
75
  if member.is_a?(AST::Members::Base)
62
- if rbs_member = translate_member(member)
76
+ if rbs_member = translate_member(member, decl)
63
77
  members.concat rbs_member
64
78
  end
65
79
  end
66
80
 
67
- if member.is_a?(AST::Declarations::Base)
81
+ if member.is_a?(AST::Declarations::SingletonClassDecl)
82
+ members.concat translate_singleton_decl(member)
83
+ elsif member.is_a?(AST::Declarations::Base)
68
84
  if rbs = translate_decl(member)
69
85
  members << rbs
70
86
  end
@@ -82,6 +98,8 @@ module RBS
82
98
  )
83
99
  end
84
100
 
101
+ # @rbs decl: AST::Declarations::ModuleDecl
102
+ # @rbs returns RBS::AST::Declarations::Module?
85
103
  def translate_module_decl(decl)
86
104
  return unless decl.module_name
87
105
 
@@ -93,12 +111,14 @@ module RBS
93
111
 
94
112
  decl.members.each do |member|
95
113
  if member.is_a?(AST::Members::Base)
96
- if rbs_member = translate_member(member)
114
+ if rbs_member = translate_member(member, decl)
97
115
  members.concat rbs_member
98
116
  end
99
117
  end
100
118
 
101
- if member.is_a?(AST::Declarations::Base)
119
+ if member.is_a?(AST::Declarations::SingletonClassDecl)
120
+ members.concat translate_singleton_decl(member)
121
+ elsif member.is_a?(AST::Declarations::Base)
102
122
  if rbs = translate_decl(member)
103
123
  members << rbs
104
124
  end
@@ -118,6 +138,8 @@ module RBS
118
138
  )
119
139
  end
120
140
 
141
+ # @rbs decl: AST::Declarations::ConstantDecl
142
+ # @rbs returns RBS::AST::Declarations::Constant?
121
143
  def translate_constant_decl(decl)
122
144
  return unless decl.constant_name
123
145
 
@@ -133,18 +155,39 @@ module RBS
133
155
  )
134
156
  end
135
157
 
136
- def translate_member(member)
158
+ # @rbs decl: AST::Declarations::SingletonClassDecl
159
+ # @rbs returns Array[RBS::AST::Members::t]
160
+ def translate_singleton_decl(decl)
161
+ members = []
162
+
163
+ decl.members.each do |member|
164
+ if member.is_a?(AST::Members::Base)
165
+ if rbs_member = translate_member(member, decl)
166
+ members.concat rbs_member
167
+ end
168
+ end
169
+ end
170
+
171
+ members
172
+ end
173
+
174
+ # @rbs member: AST::Members::t
175
+ # @rbs decl: AST::Declarations::ClassDecl | AST::Declarations::ModuleDecl | AST::Declarations::SingletonClassDecl
176
+ # @rbs returns Array[RBS::AST::Members::t | RBS::AST::Declarations::t]?
177
+ def translate_member(member, decl)
137
178
  case member
138
179
  when AST::Members::RubyDef
139
180
  if member.comments
140
181
  comment = RBS::AST::Comment.new(string: member.comments.content, location: nil)
141
182
  end
142
183
 
184
+ kind = method_kind(member, decl)
185
+
143
186
  if member.override_annotation
144
187
  return [
145
188
  RBS::AST::Members::MethodDefinition.new(
146
189
  name: member.method_name,
147
- kind: member.method_kind,
190
+ kind: kind,
148
191
  overloads: [],
149
192
  annotations: [],
150
193
  location: nil,
@@ -158,7 +201,7 @@ module RBS
158
201
  [
159
202
  RBS::AST::Members::MethodDefinition.new(
160
203
  name: member.method_name,
161
- kind: member.method_kind,
204
+ kind: kind,
162
205
  overloads: member.method_overloads,
163
206
  annotations: member.method_annotations,
164
207
  location: nil,
@@ -205,6 +248,35 @@ module RBS
205
248
  end
206
249
  end
207
250
  end
251
+
252
+ private
253
+
254
+ # Returns the `kind` of the method definition
255
+ #
256
+ # ```rb
257
+ # def self.foo = () # :singleton
258
+ # class A
259
+ # class << self
260
+ # def bar = () # :singleton
261
+ # end
262
+ # end
263
+ #
264
+ # def object.foo = () # Not supported (returns :instance)
265
+ # ```
266
+ #
267
+ # @rbs member: AST::Members::RubyDef
268
+ # @rbs decl: AST::Declarations::ClassDecl | AST::Declarations::ModuleDecl | AST::Declarations::SingletonClassDecl
269
+ # @rbs returns RBS::AST::Members::MethodDefinition::kind
270
+ def method_kind(member, decl)
271
+ return :singleton if decl.is_a?(AST::Declarations::SingletonClassDecl)
272
+
273
+ case member.node.receiver
274
+ when Prism::SelfNode
275
+ :singleton
276
+ else
277
+ :instance
278
+ end
279
+ end
208
280
  end
209
281
  end
210
282
  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
 
@@ -18,5 +19,7 @@ require "rbs/inline/writer"
18
19
 
19
20
  module RBS
20
21
  module Inline
22
+ # @rbs!
23
+ # type token = [Symbol, String]
21
24
  end
22
25
  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
@@ -4,6 +4,8 @@ module RBS
4
4
  module Inline
5
5
  module AST
6
6
  module Annotations
7
+ type t = VarType | ReturnType | Use | Inherits | Generic | ModuleSelf | Skip | Assertion | Application | RBSAnnotation | Override | IvarType | Yields | Embedded
8
+
7
9
  class Base
8
10
  attr_reader source: CommentLines
9
11
 
@@ -1,3 +1,5 @@
1
+ # Generated from lib/rbs/inline/ast/comment_lines.rb with RBS::Inline
2
+
1
3
  module RBS
2
4
  module Inline
3
5
  module AST
@@ -12,15 +14,17 @@ module RBS
12
14
  #
13
15
  # We want to get a String of comment1 and comment2, `"Hello\nWorld".
14
16
  # And want to translate a location in the string into the location in comment1 and comment2.
15
- #
16
17
  class CommentLines
17
- attr_reader comments: Array[[Prism::Comment, Integer]]
18
+ attr_reader comments: Array[[ Prism::Comment, Integer ]]
18
19
 
19
- def initialize: (Array[Prism::Comment]) -> void
20
+ # @rbs comments: Array[Prism::Comment]
21
+ def initialize: (Array[Prism::Comment] comments) -> untyped
20
22
 
21
23
  def string: () -> String
22
24
 
23
- def comment_location: (Integer index) -> [Prism::Comment, Integer]?
25
+ # @rbs index: Integer
26
+ # @rbs returns [Prism::Comment, Integer]?
27
+ def comment_location: (Integer index) -> [ Prism::Comment, Integer ]?
24
28
  end
25
29
  end
26
30
  end
@@ -10,7 +10,7 @@ module RBS
10
10
  def type_name: (Prism::Node node) -> TypeName?
11
11
  end
12
12
 
13
- type t = ClassDecl | ModuleDecl | ConstantDecl
13
+ type t = ClassDecl | ModuleDecl | ConstantDecl | SingletonClassDecl
14
14
 
15
15
  interface _WithComments
16
16
  def comments: () -> AnnotationParser::ParsingResult?
@@ -110,6 +110,9 @@ module RBS
110
110
 
111
111
  def start_line: () -> Integer
112
112
  end
113
+
114
+ class SingletonClassDecl < ModuleOrClass[Prism::SingletonClassNode]
115
+ end
113
116
  end
114
117
  end
115
118
  end
@@ -4,6 +4,12 @@ module RBS
4
4
  module Inline
5
5
  module AST
6
6
  module Members
7
+ type ruby = RubyDef | RubyAlias | RubyMixin | RubyAttr | RubyPublic | RubyPrivate
8
+
9
+ type rbs = RBSIvar | RBSEmbedded
10
+
11
+ type t = ruby | rbs
12
+
7
13
  class Base
8
14
  attr_reader location: Prism::Location
9
15
 
@@ -44,16 +50,6 @@ module RBS
44
50
 
45
51
  def method_type_annotations: () -> Array[Annotations::Assertion]
46
52
 
47
- # Returns the `kind` of the method definition
48
- #
49
- # [FIXME] It only supports `self` receiver.
50
- #
51
- # ```rb
52
- # def self.foo = () # :singleton
53
- # def object.foo = () # Not supported (returns :instance)
54
- # ```
55
- def method_kind: () -> RBS::AST::Members::MethodDefinition::kind
56
-
57
53
  def return_type: () -> Types::t?
58
54
 
59
55
  def var_type_hash: () -> Hash[Symbol, Types::t?]
@@ -84,6 +80,8 @@ module RBS
84
80
  end
85
81
 
86
82
  class RubyMixin < RubyBase
83
+ include Declarations::ConstantUtil
84
+
87
85
  # CallNode that calls `include`, `prepend`, and `extend` method
88
86
  attr_reader node: Prism::CallNode
89
87
 
@@ -1,97 +1,125 @@
1
+ # Generated from lib/rbs/inline/ast/tree.rb with RBS::Inline
2
+
1
3
  module RBS
2
4
  module Inline
3
5
  module AST
4
6
  class Tree
5
- type token = [Symbol, String]
7
+ type token = [ Symbol, String ]
6
8
 
7
9
  type tree = token | Tree | Types::t | MethodType | nil
8
10
 
9
11
  attr_reader trees: Array[tree]
10
12
 
13
+ attr_reader type: Symbol
14
+
11
15
  # Children but without `tWHITESPACE` tokens
12
- #
13
16
  attr_reader non_trivia_trees: Array[tree]
14
17
 
15
- attr_reader type: Symbol
18
+ # @rbs type: Symbol
19
+ def initialize: (Symbol type) -> untyped
16
20
 
17
- def initialize: (Symbol type) -> void
21
+ # @rbs tok: tree
22
+ # @rbs returns self
23
+ def <<: (tree tok) -> self
18
24
 
19
- def <<: (tree) -> self
25
+ # Returns the source code associated to the tree
26
+ def to_s: () -> String
20
27
 
21
28
  # Returns n-th token from the children
22
29
  #
23
30
  # Raises if the value is not a token or nil.
24
31
  #
25
- def nth_token: (Integer) -> token?
32
+ # @rbs index: Integer
33
+ # @rbs returns token?
34
+ def nth_token: (Integer index) -> token?
26
35
 
27
36
  # Returns n-th token from the children
28
37
  #
29
38
  # Returns `nil` if the value is not a token.
30
39
  #
31
- def nth_token?: (Integer) -> token?
40
+ # @rbs index: Integer
41
+ # @rbs returns token?
42
+ def nth_token?: (Integer index) -> token?
32
43
 
33
44
  # Returns n-th token from the children
34
45
  #
35
46
  # Raises if the value is not token.
36
47
  #
37
- def nth_token!: (Integer) -> token
48
+ # @rbs index: Integer
49
+ # @rbs returns token
50
+ def nth_token!: (Integer index) -> token
38
51
 
39
52
  # Returns n-th tree from the children
40
53
  #
41
54
  # Raises if the value is not a tree or nil.
42
55
  #
43
- def nth_tree: (Integer) -> Tree?
56
+ # @rbs index: Integer
57
+ # @rbs returns Tree?
58
+ def nth_tree: (Integer index) -> Tree?
44
59
 
45
60
  # Returns n-th tree from the children
46
61
  #
47
62
  # Returns `nil` if the value is not a tree or nil.
48
63
  #
49
- def nth_tree?: (Integer) -> Tree?
64
+ # @rbs index: Integer
65
+ # @rbs returns Tree?
66
+ def nth_tree?: (Integer index) -> Tree?
50
67
 
51
68
  # Returns n-th tree from the children
52
69
  #
53
70
  # Raises if the value is not a tree.
54
71
  #
55
- def nth_tree!: (Integer) -> Tree
72
+ # @rbs index: Integer
73
+ # @rbs returns Tree
74
+ def nth_tree!: (Integer index) -> Tree
56
75
 
57
76
  # Returns n-th type from the children
58
77
  #
59
- # Raises if the value is not a tree or nil.
78
+ # Raises if the value is not a type or nil.
60
79
  #
61
- def nth_type: (Integer) -> Types::t?
80
+ # @rbs index: Integer
81
+ # @rbs returns Types::t?
82
+ def nth_type: (Integer index) -> Types::t?
62
83
 
63
84
  # Returns n-th type from the children
64
85
  #
65
86
  # Returns `nil` if the value is not a type.
66
87
  #
67
- def nth_type?: (Integer) -> Types::t?
88
+ # @rbs index: Integer
89
+ # @rbs returns Types::t?
90
+ def nth_type?: (Integer index) -> Types::t?
68
91
 
69
92
  # Returns n-th type from the children
70
93
  #
71
94
  # Raises if the value is not a type.
72
95
  #
73
- def nth_type!: (Integer) -> Types::t
96
+ # @rbs index: Integer
97
+ # @rbs returns Types::t
98
+ def nth_type!: (Integer index) -> Types::t
74
99
 
75
100
  # Returns n-th method type from the children
76
101
  #
77
102
  # Raises if the value is not a method type or `nil`.
78
103
  #
79
- def nth_method_type: (Integer) -> MethodType?
104
+ # @rbs index: Integer
105
+ # @rbs returns MethodType?
106
+ def nth_method_type: (Integer index) -> MethodType?
80
107
 
81
108
  # Returns n-th method type from the children
82
109
  #
83
110
  # Returns `nil` if the value is not a method type.
84
111
  #
85
- def nth_method_type?: (Integer) -> MethodType?
112
+ # @rbs index: Integer
113
+ # @rbs returns MethodType?
114
+ def nth_method_type?: (Integer index) -> MethodType?
86
115
 
87
116
  # Returns n-th method tree from the children
88
117
  #
89
118
  # Raises if the value is not a method tree.
90
119
  #
91
- def nth_method_type!: (Integer) -> MethodType
92
-
93
- # Returns the source code associated to the tree
94
- def to_s: () -> String
120
+ # @rbs index: Integer
121
+ # @rbs returns MethodType
122
+ def nth_method_type!: (Integer index) -> MethodType
95
123
  end
96
124
  end
97
125
  end
@@ -0,0 +1,11 @@
1
+ # Generated from lib/rbs/inline/node_utils.rb with RBS::Inline
2
+
3
+ module RBS
4
+ module Inline
5
+ module NodeUtils
6
+ # @rbs node: Prism::Node
7
+ # @rbs returns TypeName?
8
+ def type_name: (Prism::Node node) -> TypeName?
9
+ end
10
+ end
11
+ end
@@ -41,9 +41,9 @@ module RBS
41
41
  # @rbs returns AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl
42
42
  def current_class_module_decl!: () -> (AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl)
43
43
 
44
- # :: (AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl) { () -> void } -> void
44
+ # :: (AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl | AST::Declarations::SingletonClassDecl) { () -> void } -> void
45
45
  # :: (AST::Declarations::ConstantDecl) -> void
46
- def push_class_module_decl: (AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl) { () -> void } -> void
46
+ def push_class_module_decl: (AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl | AST::Declarations::SingletonClassDecl) { () -> void } -> void
47
47
  | (AST::Declarations::ConstantDecl) -> void
48
48
 
49
49
  # Load inner declarations and delete them from `#comments` hash
@@ -60,6 +60,9 @@ module RBS
60
60
  # @rbs override
61
61
  def visit_class_node: ...
62
62
 
63
+ # @rbs override
64
+ def visit_singleton_class_node: ...
65
+
63
66
  # @rbs override
64
67
  def visit_module_node: ...
65
68
 
@@ -0,0 +1,7 @@
1
+ # Generated from lib/rbs/inline/version.rb with RBS::Inline
2
+
3
+ module RBS
4
+ module Inline
5
+ VERSION: ::String
6
+ end
7
+ end
@@ -0,0 +1,72 @@
1
+ # Generated from lib/rbs/inline/writer.rb with RBS::Inline
2
+
3
+ module RBS
4
+ module Inline
5
+ class Writer
6
+ attr_reader output: String
7
+
8
+ attr_reader writer: RBS::Writer
9
+
10
+ # @rbs buffer: String
11
+ def initialize: (?String buffer) -> untyped
12
+
13
+ # @rbs uses: Array[AST::Annotations::Use]
14
+ # @rbs decls: Array[AST::Declarations::t]
15
+ def self.write: (Array[AST::Annotations::Use] uses, Array[AST::Declarations::t] decls) -> untyped
16
+
17
+ # @rbs lines: Array[String]
18
+ # @rbs returns void
19
+ def header: (*String lines) -> void
20
+
21
+ # @rbs uses: Array[AST::Annotations::Use]
22
+ # @rbs decls: Array[AST::Declarations::t]
23
+ # @rbs returns void
24
+ def write: (Array[AST::Annotations::Use] uses, Array[AST::Declarations::t] decls) -> void
25
+
26
+ # @rbs decl: AST::Declarations::t
27
+ # @rbs returns RBS::AST::Declarations::t?
28
+ def translate_decl: (AST::Declarations::t decl) -> RBS::AST::Declarations::t?
29
+
30
+ # @rbs decl: AST::Declarations::ClassDecl
31
+ # @rbs returns RBS::AST::Declarations::Class?
32
+ def translate_class_decl: (AST::Declarations::ClassDecl decl) -> RBS::AST::Declarations::Class?
33
+
34
+ # @rbs decl: AST::Declarations::ModuleDecl
35
+ # @rbs returns RBS::AST::Declarations::Module?
36
+ def translate_module_decl: (AST::Declarations::ModuleDecl decl) -> RBS::AST::Declarations::Module?
37
+
38
+ # @rbs decl: AST::Declarations::ConstantDecl
39
+ # @rbs returns RBS::AST::Declarations::Constant?
40
+ def translate_constant_decl: (AST::Declarations::ConstantDecl decl) -> RBS::AST::Declarations::Constant?
41
+
42
+ # @rbs decl: AST::Declarations::SingletonClassDecl
43
+ # @rbs returns Array[RBS::AST::Members::t]
44
+ def translate_singleton_decl: (AST::Declarations::SingletonClassDecl decl) -> Array[RBS::AST::Members::t]
45
+
46
+ # @rbs member: AST::Members::t
47
+ # @rbs decl: AST::Declarations::ClassDecl | AST::Declarations::ModuleDecl | AST::Declarations::SingletonClassDecl
48
+ # @rbs returns Array[RBS::AST::Members::t | RBS::AST::Declarations::t]?
49
+ def translate_member: (AST::Members::t member, AST::Declarations::ClassDecl | AST::Declarations::ModuleDecl | AST::Declarations::SingletonClassDecl decl) -> Array[RBS::AST::Members::t | RBS::AST::Declarations::t]?
50
+
51
+ private
52
+
53
+ # Returns the `kind` of the method definition
54
+ #
55
+ # ```rb
56
+ # def self.foo = () # :singleton
57
+ # class A
58
+ # class << self
59
+ # def bar = () # :singleton
60
+ # end
61
+ # end
62
+ #
63
+ # def object.foo = () # Not supported (returns :instance)
64
+ # ```
65
+ #
66
+ # @rbs member: AST::Members::RubyDef
67
+ # @rbs decl: AST::Declarations::ClassDecl | AST::Declarations::ModuleDecl | AST::Declarations::SingletonClassDecl
68
+ # @rbs returns RBS::AST::Members::MethodDefinition::kind
69
+ def method_kind: (AST::Members::RubyDef member, AST::Declarations::ClassDecl | AST::Declarations::ModuleDecl | AST::Declarations::SingletonClassDecl decl) -> RBS::AST::Members::MethodDefinition::kind
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,7 @@
1
+ # Generated from lib/rbs/inline.rb with RBS::Inline
2
+
3
+ module RBS
4
+ module Inline
5
+ type token = [ Symbol, String ]
6
+ end
7
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbs-inline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Soutaro Matsumoto
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-11 00:00:00.000000000 Z
11
+ date: 2024-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prism
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '0.29'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '0.30'
22
+ version: '0.31'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,21 +29,21 @@ dependencies:
29
29
  version: '0.29'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '0.30'
32
+ version: '0.31'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: rbs
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: 3.5.0.pre
39
+ version: 3.5.0
40
40
  type: :runtime
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: 3.5.0.pre
46
+ version: 3.5.0
47
47
  description: Inline RBS type declaration.
48
48
  email:
49
49
  - matsumoto@soutaro.com
@@ -73,21 +73,19 @@ files:
73
73
  - lib/rbs/inline/writer.rb
74
74
  - rbs_collection.lock.yaml
75
75
  - rbs_collection.yaml
76
+ - sig/generated/rbs/inline.rbs
76
77
  - sig/generated/rbs/inline/annotation_parser.rbs
77
78
  - sig/generated/rbs/inline/ast/annotations.rbs
79
+ - sig/generated/rbs/inline/ast/comment_lines.rbs
78
80
  - sig/generated/rbs/inline/ast/declarations.rbs
79
81
  - sig/generated/rbs/inline/ast/members.rbs
82
+ - sig/generated/rbs/inline/ast/tree.rbs
80
83
  - sig/generated/rbs/inline/cli.rbs
84
+ - sig/generated/rbs/inline/node_utils.rbs
81
85
  - sig/generated/rbs/inline/parser.rbs
82
- - sig/rbs/inline.rbs
83
- - sig/rbs/inline/annotation_parser.rbs
84
- - sig/rbs/inline/ast/comment_lines.rbs
86
+ - sig/generated/rbs/inline/version.rbs
87
+ - sig/generated/rbs/inline/writer.rbs
85
88
  - sig/rbs/inline/ast/members.rbs
86
- - sig/rbs/inline/ast/tree.rbs
87
- - sig/rbs/inline/node_utils.rbs
88
- - sig/rbs/inline/writer.rbs
89
- - yard-samples/hello.rb
90
- - yard-samples/sample1.rb
91
89
  homepage: https://github.com/soutaro/rbs-inline
92
90
  licenses:
93
91
  - MIT
File without changes
@@ -1,7 +0,0 @@
1
- module RBS
2
- module Inline
3
- module NodeUtils
4
- def type_name: (Prism::Node) -> TypeName?
5
- end
6
- end
7
- end
@@ -1,27 +0,0 @@
1
- module RBS
2
- module Inline
3
- class Writer
4
- attr_reader output: String
5
-
6
- attr_reader writer: RBS::Writer
7
-
8
- def self.write: (Array[AST::Annotations::Use], Array[AST::Declarations::t]) -> String
9
-
10
- def initialize: (?String) -> void
11
-
12
- def write: (Array[AST::Annotations::Use], Array[AST::Declarations::t]) -> void
13
-
14
- def header: (*String) -> void
15
-
16
- def translate_decl: (AST::Declarations::t) -> RBS::AST::Declarations::t?
17
-
18
- def translate_class_decl: (AST::Declarations::ClassDecl) -> RBS::AST::Declarations::Class?
19
-
20
- def translate_module_decl: (AST::Declarations::ModuleDecl) -> RBS::AST::Declarations::Module?
21
-
22
- def translate_constant_decl: (AST::Declarations::ConstantDecl) -> RBS::AST::Declarations::Constant?
23
-
24
- def translate_member: (AST::Members::t) -> Array[RBS::AST::Members::t | RBS::AST::Declarations::t]?
25
- end
26
- end
27
- end
data/sig/rbs/inline.rbs DELETED
@@ -1,41 +0,0 @@
1
- module RBS
2
- module Inline
3
- type token = [Symbol, String]
4
-
5
- VERSION: String
6
-
7
- module AST
8
- module Declarations
9
- end
10
-
11
- module Annotations
12
- type t = VarType
13
- | ReturnType
14
- | Use
15
- | Inherits
16
- | Generic
17
- | ModuleSelf
18
- | Skip
19
- | Assertion
20
- | Application
21
- | RBSAnnotation
22
- | Override
23
- | IvarType
24
- | Yields
25
- | Embedded
26
- # | Def
27
- # | AttrReader | AttrWriter | AttrAccessor
28
- # | Include | Extend | Prepend
29
- # | Alias
30
- end
31
-
32
- module Members
33
- type ruby = RubyDef | RubyAlias | RubyMixin | RubyAttr | RubyPublic | RubyPrivate
34
-
35
- type rbs = RBSIvar | RBSEmbedded
36
-
37
- type t = ruby | rbs
38
- end
39
- end
40
- end
41
- end
@@ -1,6 +0,0 @@
1
- require "prism"
2
-
3
- ast = Prism.parse_file("yard-samples/sample1.rb")
4
-
5
- pp ast.value
6
- pp ast.comments
@@ -1,26 +0,0 @@
1
- module Foo
2
- # This is `Foo#foo` method
3
- #
4
- # @param i [Integer] Size of something
5
- # @param j [Symbol,Integer] Something doing meaningful
6
- # @return [String?] Returns a string or nil
7
- #
8
- #
9
- # @rbs.method (Integer, String) -> void
10
- # | [A] () { () [self: String] -> A } -> A?
11
- #
12
- def foo(i, j)
13
-
14
- end
15
-
16
- # @rbs.inline
17
- # attr_reader hoge: String
18
- # attr_reader name: String?
19
- def hoge
20
-
21
- end
22
-
23
- class Foo
24
- # @rbs.inline include Foo[String]
25
- end
26
- end