rbs-inline 0.3.0 → 0.5.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.
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,17 +1,29 @@
1
+ # rbs_inline: enabled
2
+
1
3
  module RBS
2
4
  module Inline
3
5
  module AST
6
+ # @rbs!
7
+ # type token = [Symbol, String]
8
+ #
9
+ # type tree = token | Tree | Types::t | MethodType | nil
10
+
4
11
  class Tree
5
- attr_reader :trees
6
- attr_reader :type
7
- attr_reader :non_trivia_trees
12
+ attr_reader :trees #: Array[tree]
13
+ attr_reader :type #: Symbol
14
+
15
+ # Children but without `tWHITESPACE` tokens
16
+ attr_reader :non_trivia_trees #: Array[tree]
8
17
 
9
- def initialize(type)
18
+ # @rbs type: Symbol
19
+ def initialize(type) #: void
10
20
  @type = type
11
21
  @trees = []
12
22
  @non_trivia_trees = []
13
23
  end
14
24
 
25
+ # @rbs tok: tree
26
+ # @rbs return: self
15
27
  def <<(tok)
16
28
  trees << tok
17
29
  unless tok.is_a?(Array) && tok[0] == :tWHITESPACE
@@ -20,7 +32,8 @@ module RBS
20
32
  self
21
33
  end
22
34
 
23
- def to_s
35
+ # Returns the source code associated to the tree
36
+ def to_s #: String
24
37
  buf = +""
25
38
 
26
39
  trees.each do |tree|
@@ -39,6 +52,26 @@ module RBS
39
52
  buf
40
53
  end
41
54
 
55
+ # Returns `true` if token at the given index is of the given type
56
+ def token?(type, at:)
57
+ if tok = nth_token?(at)
58
+ tok[0] == type
59
+ end
60
+ end
61
+
62
+ # Returns `true` if tree at the given index is of the given type
63
+ def tree?(type, at:)
64
+ if tree = nth_tree?(at)
65
+ tree.type == type
66
+ end
67
+ end
68
+
69
+ # Returns n-th token from the children
70
+ #
71
+ # Raises if the value is not a token or nil.
72
+ #
73
+ # @rbs index: Integer
74
+ # @rbs return: token?
42
75
  def nth_token(index)
43
76
  tok = non_trivia_trees[index]
44
77
  case tok
@@ -49,6 +82,12 @@ module RBS
49
82
  end
50
83
  end
51
84
 
85
+ # Returns n-th token from the children
86
+ #
87
+ # Returns `nil` if the value is not a token.
88
+ #
89
+ # @rbs index: Integer
90
+ # @rbs return: token?
52
91
  def nth_token?(index)
53
92
  tok = non_trivia_trees[index]
54
93
  case tok
@@ -59,10 +98,22 @@ module RBS
59
98
  end
60
99
  end
61
100
 
101
+ # Returns n-th token from the children
102
+ #
103
+ # Raises if the value is not token.
104
+ #
105
+ # @rbs index: Integer
106
+ # @rbs return: token
62
107
  def nth_token!(index)
63
108
  nth_token(index) || raise
64
109
  end
65
110
 
111
+ # Returns n-th tree from the children
112
+ #
113
+ # Raises if the value is not a tree or nil.
114
+ #
115
+ # @rbs index: Integer
116
+ # @rbs return: Tree?
66
117
  def nth_tree(index)
67
118
  tok = non_trivia_trees[index]
68
119
  case tok
@@ -73,6 +124,12 @@ module RBS
73
124
  end
74
125
  end
75
126
 
127
+ # Returns n-th tree from the children
128
+ #
129
+ # Returns `nil` if the value is not a tree or nil.
130
+ #
131
+ # @rbs index: Integer
132
+ # @rbs return: Tree?
76
133
  def nth_tree?(index)
77
134
  tok = non_trivia_trees[index]
78
135
  case tok
@@ -83,11 +140,23 @@ module RBS
83
140
  end
84
141
  end
85
142
 
143
+ # Returns n-th tree from the children
144
+ #
145
+ # Raises if the value is not a tree.
146
+ #
147
+ # @rbs index: Integer
148
+ # @rbs return: Tree
86
149
  def nth_tree!(index)
87
150
  nth_tree(index) || raise
88
151
  end
89
152
 
90
153
 
154
+ # Returns n-th type from the children
155
+ #
156
+ # Raises if the value is not a type or nil.
157
+ #
158
+ # @rbs index: Integer
159
+ # @rbs return: Types::t?
91
160
  def nth_type(index)
92
161
  tok = non_trivia_trees[index]
93
162
  case tok
@@ -98,6 +167,12 @@ module RBS
98
167
  end
99
168
  end
100
169
 
170
+ # Returns n-th type from the children
171
+ #
172
+ # Returns `nil` if the value is not a type.
173
+ #
174
+ # @rbs index: Integer
175
+ # @rbs return: Types::t?
101
176
  def nth_type?(index)
102
177
  tok = non_trivia_trees[index]
103
178
  case tok
@@ -108,10 +183,22 @@ module RBS
108
183
  end
109
184
  end
110
185
 
186
+ # Returns n-th type from the children
187
+ #
188
+ # Raises if the value is not a type.
189
+ #
190
+ # @rbs index: Integer
191
+ # @rbs return: Types::t
111
192
  def nth_type!(index)
112
193
  nth_type(index) || raise
113
194
  end
114
195
 
196
+ # Returns n-th method type from the children
197
+ #
198
+ # Raises if the value is not a method type or `nil`.
199
+ #
200
+ # @rbs index: Integer
201
+ # @rbs return: MethodType?
115
202
  def nth_method_type(index)
116
203
  tok = non_trivia_trees[index]
117
204
  case tok
@@ -122,6 +209,12 @@ module RBS
122
209
  end
123
210
  end
124
211
 
212
+ # Returns n-th method type from the children
213
+ #
214
+ # Returns `nil` if the value is not a method type.
215
+ #
216
+ # @rbs index: Integer
217
+ # @rbs return: MethodType?
125
218
  def nth_method_type?(index)
126
219
  tok = non_trivia_trees[index]
127
220
  case tok
@@ -132,6 +225,12 @@ module RBS
132
225
  end
133
226
  end
134
227
 
228
+ # Returns n-th method tree from the children
229
+ #
230
+ # Raises if the value is not a method tree.
231
+ #
232
+ # @rbs index: Integer
233
+ # @rbs return: MethodType
135
234
  def nth_method_type!(index)
136
235
  nth_method_type(index) || raise
137
236
  end
@@ -17,22 +17,22 @@ module RBS
17
17
  #
18
18
  #
19
19
  class PathCalculator
20
- attr_reader :pwd #:: Pathname
20
+ attr_reader :pwd #: Pathname
21
21
 
22
- attr_reader :base_paths #:: Array[Pathname]
22
+ attr_reader :base_paths #: Array[Pathname]
23
23
 
24
- attr_reader :output_path #:: Pathname
24
+ attr_reader :output_path #: Pathname
25
25
 
26
26
  # @rbs pwd: Pathname
27
27
  # @rbs base_paths: Array[Pathname]
28
28
  # @rbs output_path: Pathname
29
- def initialize(pwd, base_paths, output_path) #:: void
29
+ def initialize(pwd, base_paths, output_path) #: void
30
30
  @pwd = pwd
31
31
  @base_paths = base_paths
32
32
  @output_path = output_path
33
33
  end
34
34
 
35
- #:: (Pathname) -> Pathname?
35
+ #: (Pathname) -> Pathname?
36
36
  def calculate(path)
37
37
  path = pwd + path if path.relative?
38
38
  path = path.cleanpath
@@ -49,18 +49,18 @@ module RBS
49
49
 
50
50
  # @rbs path: Pathname
51
51
  # @rbs prefix: Pathname
52
- # @rbs returns bool
52
+ # @rbs return: bool
53
53
  def has_prefix?(path, prefix:)
54
54
  path.descend.include?(prefix)
55
55
  end
56
56
  end
57
57
 
58
- attr_reader :stdout, :stderr #:: IO
59
- attr_reader :logger #:: Logger
58
+ attr_reader :stdout, :stderr #: IO
59
+ attr_reader :logger #: Logger
60
60
 
61
61
  # @rbs stdout: IO
62
62
  # @rbs stderr: IO
63
- def initialize(stdout: STDOUT, stderr: STDERR) #:: void
63
+ def initialize(stdout: STDOUT, stderr: STDERR) #: void
64
64
  @stdout = stdout
65
65
  @stderr = stderr
66
66
  @logger = Logger.new(stderr)
@@ -68,7 +68,7 @@ module RBS
68
68
  end
69
69
 
70
70
  # @rbs args: Array[String]
71
- # @rbs returns Integer
71
+ # @rbs return: Integer
72
72
  def run(args)
73
73
  base_paths = [Pathname("lib"), Pathname("app")]
74
74
  output_path = nil #: Pathname?
@@ -139,10 +139,10 @@ module RBS
139
139
 
140
140
  logger.debug { "Parsing ruby file #{target}..." }
141
141
 
142
- if (uses, decls = Parser.parse(Prism.parse_file(target.to_s), opt_in: opt_in))
142
+ if (uses, decls, rbs_decls = Parser.parse(Prism.parse_file(target.to_s), opt_in: opt_in))
143
143
  writer = Writer.new()
144
144
  writer.header("Generated from #{target.relative? ? target : target.relative_path_from(Pathname.pwd)} with RBS::Inline")
145
- writer.write(uses, decls)
145
+ writer.write(uses, decls, rbs_decls)
146
146
 
147
147
  if output
148
148
  unless output.parent.directory?
@@ -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 return: TypeName?
4
8
  def type_name(node)
5
9
  case node
6
10
  when Prism::ConstantReadNode
@@ -5,13 +5,18 @@
5
5
  module RBS
6
6
  module Inline
7
7
  class Parser < Prism::Visitor
8
+ # @rbs! type with_members = AST::Declarations::ModuleDecl
9
+ # | AST::Declarations::ClassDecl
10
+ # | AST::Declarations::SingletonClassDecl
11
+ # | AST::Declarations::BlockDecl
12
+
8
13
  # The top level declarations
9
14
  #
10
- attr_reader :decls #:: Array[AST::Declarations::t]
15
+ attr_reader :decls #: Array[AST::Declarations::t]
11
16
 
12
17
  # The surrounding declarations
13
18
  #
14
- attr_reader :surrounding_decls #:: Array[AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl]
19
+ attr_reader :surrounding_decls #: Array[with_members]
15
20
 
16
21
  # ParsingResult associated with the line number at the end
17
22
  #
@@ -23,24 +28,35 @@ module RBS
23
28
  # > [!IMPORTANT]
24
29
  # > The values will be removed during parsing.
25
30
  #
26
- attr_reader :comments #:: Hash[Integer, AnnotationParser::ParsingResult]
31
+ attr_reader :comments #: Hash[Integer, AnnotationParser::ParsingResult]
27
32
 
28
33
  # The current visibility applied to single `def` node
29
34
  #
30
35
  # Assuming it's directly inside `private` or `public` calls.
31
36
  # `nil` when the `def` node is not inside `private` or `public` calls.
32
37
  #
33
- attr_reader :current_visibility #:: RBS::AST::Members::visibility?
38
+ attr_reader :current_visibility #: RBS::AST::Members::visibility?
34
39
 
35
- def initialize() #:: void
40
+ def initialize() #: void
36
41
  @decls = []
37
42
  @surrounding_decls = []
38
43
  @comments = {}
39
44
  end
40
45
 
46
+ # Parses the given Prism result to a three tuple
47
+ #
48
+ # Returns a three tuple of:
49
+ #
50
+ # 1. An array of `use` directives
51
+ # 2. An array of declarations
52
+ # 3. An array of RBS declarations given as `@rbs!` annotation at top-level
53
+ #
54
+ # Note that only RBS declarations are allowed in the top-level `@rbs!` annotations.
55
+ # RBS *members* are ignored in the array.
56
+ #
41
57
  # @rbs result: ParseResult
42
58
  # @rbs opt_in: bool -- `true` for *opt-out* mode, `false` for *opt-in* mode.
43
- # @rbs returns [Array[AST::Annotations::Use], Array[AST::Declarations::t]]?
59
+ # @rbs return: [Array[AST::Annotations::Use], Array[AST::Declarations::t], Array[RBS::AST::Declarations::t]]?
44
60
  def self.parse(result, opt_in:)
45
61
  instance = Parser.new()
46
62
 
@@ -61,7 +77,7 @@ module RBS
61
77
 
62
78
  uses = [] #: Array[AST::Annotations::Use]
63
79
  annots.each do |annot|
64
- annot.annotations.each do |annotation|
80
+ annot.each_annotation do |annotation|
65
81
  if annotation.is_a?(AST::Annotations::Use)
66
82
  uses << annotation
67
83
  end
@@ -70,24 +86,44 @@ module RBS
70
86
 
71
87
  instance.visit(result.value)
72
88
 
89
+ rbs_embeddeds = [] #: Array[AST::Members::RBSEmbedded]
90
+
91
+ instance.comments.each_value do |comment|
92
+ comment.each_annotation do |annotation|
93
+ if annotation.is_a?(AST::Annotations::Embedded)
94
+ rbs_embeddeds << AST::Members::RBSEmbedded.new(comment, annotation)
95
+ end
96
+ end
97
+ end
98
+
99
+ rbs_decls = rbs_embeddeds.flat_map do |embedded|
100
+ if (members = embedded.members).is_a?(Array)
101
+ members.select do |member|
102
+ member.is_a?(RBS::AST::Declarations::Base)
103
+ end
104
+ else
105
+ []
106
+ end #: Array[RBS::AST::Declarations::t]
107
+ end
108
+
73
109
  [
74
110
  uses,
75
- instance.decls
111
+ instance.decls,
112
+ rbs_decls
76
113
  ]
77
114
  end
78
115
 
79
- # @rbs returns AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl | nil
116
+ # @rbs return: with_members?
80
117
  def current_class_module_decl
81
118
  surrounding_decls.last
82
119
  end
83
120
 
84
- # @rbs returns AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl
121
+ # @rbs return: with_members
85
122
  def current_class_module_decl!
86
123
  current_class_module_decl or raise
87
124
  end
88
125
 
89
- #:: (AST::Declarations::ModuleDecl | AST::Declarations::ClassDecl) { () -> void } -> void
90
- #:: (AST::Declarations::ConstantDecl) -> void
126
+ #: (with_members) { () -> void } -> void
91
127
  def push_class_module_decl(decl)
92
128
  if current = current_class_module_decl
93
129
  current.members << decl
@@ -96,7 +132,7 @@ module RBS
96
132
  end
97
133
 
98
134
  if block_given?
99
- surrounding_decls.push(_ = decl)
135
+ surrounding_decls.push(decl)
100
136
  begin
101
137
  yield
102
138
  ensure
@@ -114,11 +150,11 @@ module RBS
114
150
  # @rbs members: Array[AST::Members::t | AST::Declarations::t] --
115
151
  # The destination.
116
152
  # The method doesn't insert declarations, but have it to please type checker.
117
- def load_inner_annotations(start_line, end_line, members) #:: void
153
+ def load_inner_annotations(start_line, end_line, members) #: void
118
154
  comments = inner_annotations(start_line, end_line)
119
155
 
120
156
  comments.each do |comment|
121
- comment.annotations.each do |annotation|
157
+ comment.each_annotation do |annotation|
122
158
  case annotation
123
159
  when AST::Annotations::IvarType
124
160
  members << AST::Members::RBSIvar.new(comment, annotation)
@@ -133,39 +169,53 @@ module RBS
133
169
 
134
170
  # @rbs override
135
171
  def visit_class_node(node)
136
- return if ignored_node?(node)
172
+ process_nesting_node(node) do
173
+ visit node.constant_path
174
+ visit node.superclass
137
175
 
138
- visit node.constant_path
139
- visit node.superclass
176
+ associated_comment = comments.delete(node.location.start_line - 1)
177
+ if node.superclass
178
+ app_comment = application_annotation(node.superclass)
179
+ end
140
180
 
141
- associated_comment = comments.delete(node.location.start_line - 1)
142
- if node.superclass
143
- app_comment = application_annotation(node.superclass)
144
- end
181
+ class_decl = AST::Declarations::ClassDecl.new(node, associated_comment, app_comment)
145
182
 
146
- class_decl = AST::Declarations::ClassDecl.new(node, associated_comment, app_comment)
183
+ push_class_module_decl(class_decl) do
184
+ visit node.body
185
+ end
147
186
 
148
- push_class_module_decl(class_decl) do
149
- visit node.body
187
+ load_inner_annotations(node.location.start_line, node.location.end_line, class_decl.members)
150
188
  end
189
+ end
151
190
 
152
- load_inner_annotations(node.location.start_line, node.location.end_line, class_decl.members)
191
+ # @rbs override
192
+ def visit_singleton_class_node(node)
193
+ process_nesting_node(node) do
194
+ associated_comment = comments.delete(node.location.start_line - 1)
195
+ singleton_decl = AST::Declarations::SingletonClassDecl.new(node, associated_comment)
196
+
197
+ push_class_module_decl(singleton_decl) do
198
+ visit node.body
199
+ end
200
+
201
+ load_inner_annotations(node.location.start_line, node.location.end_line, singleton_decl.members)
202
+ end
153
203
  end
154
204
 
155
205
  # @rbs override
156
206
  def visit_module_node(node)
157
- return if ignored_node?(node)
207
+ process_nesting_node(node) do
208
+ visit node.constant_path
158
209
 
159
- visit node.constant_path
210
+ associated_comment = comments.delete(node.location.start_line - 1)
160
211
 
161
- associated_comment = comments.delete(node.location.start_line - 1)
212
+ module_decl = AST::Declarations::ModuleDecl.new(node, associated_comment)
213
+ push_class_module_decl(module_decl) do
214
+ visit node.body
215
+ end
162
216
 
163
- module_decl = AST::Declarations::ModuleDecl.new(node, associated_comment)
164
- push_class_module_decl(module_decl) do
165
- visit node.body
217
+ load_inner_annotations(node.location.start_line, node.location.end_line, module_decl.members)
166
218
  end
167
-
168
- load_inner_annotations(node.location.start_line, node.location.end_line, module_decl.members)
169
219
  end
170
220
 
171
221
  # Returns an array of annotations from comments that is located between start_line and end_line
@@ -179,7 +229,7 @@ module RBS
179
229
  #
180
230
  # @rbs start_line: Integer
181
231
  # @rbs end_line: Integer
182
- def inner_annotations(start_line, end_line) #:: Array[AnnotationParser::ParsingResult]
232
+ def inner_annotations(start_line, end_line) #: Array[AnnotationParser::ParsingResult]
183
233
  annotations = comments.each_value.select do |annotation|
184
234
  range = annotation.line_range
185
235
  start_line < range.begin && range.end < end_line
@@ -192,25 +242,27 @@ module RBS
192
242
 
193
243
  # @rbs override
194
244
  def visit_def_node(node)
195
- return if ignored_node?(node)
196
- return unless current_class_module_decl
245
+ process_nesting_node(node) do
246
+ return unless current_class_module_decl
197
247
 
198
- current_decl = current_class_module_decl!
248
+ current_decl = current_class_module_decl!
199
249
 
200
- if node.location
201
- associated_comment = comments.delete(node.location.start_line - 1)
202
- end
250
+ if node.location
251
+ associated_comment = comments.delete(node.location.start_line - 1)
252
+ end
203
253
 
204
- assertion = assertion_annotation(node.rparen_loc || node&.parameters&.location || node.name_loc)
254
+ assertion = assertion_annotation(node.rparen_loc || node&.parameters&.location || node.name_loc)
205
255
 
206
- current_decl.members << AST::Members::RubyDef.new(node, associated_comment, current_visibility, assertion)
256
+ current_decl.members << AST::Members::RubyDef.new(node, associated_comment, current_visibility, assertion)
207
257
 
208
- super
258
+ super
259
+ end
209
260
  end
210
261
 
211
262
  # @rbs override
212
263
  def visit_alias_method_node(node)
213
264
  return if ignored_node?(node)
265
+ return unless current_class_module_decl
214
266
 
215
267
  if node.location
216
268
  comment = comments.delete(node.location.start_line - 1)
@@ -244,9 +296,9 @@ module RBS
244
296
  end
245
297
  if assertion_comment && comment_line
246
298
  comments.delete(comment_line)
247
- assertion = assertion_comment.annotations.find do |annotation|
248
- annotation.is_a?(AST::Annotations::Assertion)
249
- end #: AST::Annotations::Assertion?
299
+ assertion = assertion_comment.each_annotation.find do |annotation|
300
+ annotation.is_a?(AST::Annotations::TypeAssertion)
301
+ end #: AST::Annotations::TypeAssertion?
250
302
  end
251
303
 
252
304
  current_class_module_decl!.members << AST::Members::RubyAttr.new(node, comment, assertion)
@@ -284,8 +336,8 @@ module RBS
284
336
  end
285
337
 
286
338
  # @rbs new_visibility: RBS::AST::Members::visibility?
287
- # @rbs block: ^() -> void
288
- # @rbs returns void
339
+ # @rbs &block: () -> void
340
+ # @rbs return: void
289
341
  def push_visibility(new_visibility, &block)
290
342
  old_visibility = current_visibility
291
343
 
@@ -297,11 +349,21 @@ module RBS
297
349
  end
298
350
  end
299
351
 
352
+ # @rbs [A] (Node) { () -> A } -> A?
353
+ def process_nesting_node(node)
354
+ yield unless ignored_node?(node)
355
+ ensure
356
+ # Delete all inner annotations
357
+ inner_annotations(node.location.start_line, node.location.end_line)
358
+ comments.delete(node.location.start_line)
359
+ comments.delete(node.location.end_line)
360
+ end
361
+
300
362
  # @rbs node: Node
301
- # @rbs returns bool
363
+ # @rbs return: bool
302
364
  def ignored_node?(node)
303
365
  if comment = comments.fetch(node.location.start_line - 1, nil)
304
- comment.annotations.any? { _1.is_a?(AST::Annotations::Skip) }
366
+ comment.each_annotation.any? { _1.is_a?(AST::Annotations::Skip) }
305
367
  else
306
368
  false
307
369
  end
@@ -312,7 +374,7 @@ module RBS
312
374
  # The application annotation is removed from `comments`.
313
375
  #
314
376
  # @rbs node: Node
315
- # @rbs returns AST::Annotations::Application?
377
+ # @rbs return: AST::Annotations::Application?
316
378
  def application_annotation(node)
317
379
  comment_line, app_comment = comments.find do |_, comment|
318
380
  comment.line_range.begin == node.location.end_line
@@ -320,18 +382,18 @@ module RBS
320
382
 
321
383
  if app_comment && comment_line
322
384
  comments.delete(comment_line)
323
- app_comment.annotations.find do |annotation|
385
+ app_comment.each_annotation.find do |annotation|
324
386
  annotation.is_a?(AST::Annotations::Application)
325
387
  end #: AST::Annotations::Application?
326
388
  end
327
389
  end
328
390
 
329
- # Fetch Assertion annotation which is associated to `node`
391
+ # Fetch TypeAssertion annotation which is associated to `node`
330
392
  #
331
393
  # The assertion annotation is removed from `comments`.
332
394
  #
333
395
  # @rbs node: Node | Location
334
- # @rbs returns AST::Annotations::Assertion?
396
+ # @rbs return: AST::Annotations::TypeAssertion?
335
397
  def assertion_annotation(node)
336
398
  if node.is_a?(Prism::Location)
337
399
  location = node
@@ -344,9 +406,9 @@ module RBS
344
406
 
345
407
  if app_comment && comment_line
346
408
  comments.delete(comment_line)
347
- app_comment.annotations.find do |annotation|
348
- annotation.is_a?(AST::Annotations::Assertion)
349
- end #: AST::Annotations::Assertion?
409
+ app_comment.each_annotation.find do |annotation|
410
+ annotation.is_a?(AST::Annotations::TypeAssertion)
411
+ end #: AST::Annotations::TypeAssertion?
350
412
  end
351
413
  end
352
414
 
@@ -358,7 +420,26 @@ module RBS
358
420
  assertion = assertion_annotation(node)
359
421
 
360
422
  decl = AST::Declarations::ConstantDecl.new(node, comment, assertion)
361
- push_class_module_decl(decl)
423
+
424
+ if current = current_class_module_decl
425
+ current.members << decl
426
+ else
427
+ decls << decl
428
+ end
429
+ end
430
+
431
+ # @rbs override
432
+ def visit_block_node(node)
433
+ process_nesting_node(node) do
434
+ comment = comments.delete(node.location.start_line - 1)
435
+ block = AST::Declarations::BlockDecl.new(node, comment)
436
+
437
+ push_class_module_decl(block) do
438
+ super
439
+ end
440
+
441
+ load_inner_annotations(node.location.start_line, node.location.end_line, block.members)
442
+ end
362
443
  end
363
444
  end
364
445
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RBS
4
4
  module Inline
5
- VERSION = "0.3.0"
5
+ VERSION = "0.5.0"
6
6
  end
7
7
  end