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,35 +1,49 @@
1
+ # rbs_inline: enabled
2
+
1
3
  module RBS
2
4
  module Inline
3
5
  module AST
6
+ # CommentLines represents consecutive comments, providing a mapping from locations in `#string` to a pair of a comment and its offset
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
6
-
7
- def initialize(comments)
8
- offsets = comments.map do |comment|
9
- comment.location.slice.index(/[^#\s]/) || 1
10
- end
11
- first_offset = offsets[0]
19
+ attr_reader :comments #: Array[Prism::Comment]
12
20
 
13
- @comments = comments.map.with_index do |comment, index|
14
- offset = offsets[index]
15
- offset = first_offset if offset > first_offset
21
+ # @rbs comments: Array[Prism::Comment]
22
+ def initialize(comments) #: void
23
+ @comments = comments
24
+ end
16
25
 
17
- [comment, offset]
18
- end
26
+ def lines #: Array[String]
27
+ comments.map {|comment| comment.location.slice }
19
28
  end
20
29
 
21
- def string
22
- comments.map {|comment, offset| comment.location.slice[offset..] }.join("\n")
30
+ def string #: String
31
+ comments.map {|comment| comment.location.slice[1..] || "" }.join("\n")
23
32
  end
24
33
 
34
+ # Translates the cursor index of `#string` into the cursor index of a specific comment object
35
+ #
36
+ # @rbs index: Integer
37
+ # @rbs return: [Prism::Comment, Integer]?
25
38
  def comment_location(index)
26
- comments.each do |comment, offset|
39
+ comments.each do |comment|
27
40
  comment_length = comment.location.length
28
41
 
29
- if index + offset <= comment_length
30
- return [comment, index + offset]
42
+ if index + 1 <= comment_length
43
+ return [comment, index + 1]
31
44
  else
32
- index = index - comment_length + offset - 1
45
+ index -= comment_length - 1
46
+ index -= 1 # newline
33
47
  return if index < 0
34
48
  end
35
49
  end
@@ -6,7 +6,7 @@ module RBS
6
6
  module Declarations
7
7
  module ConstantUtil
8
8
  # @rbs node: Prism::Node
9
- # @rbs returns TypeName?
9
+ # @rbs return: TypeName?
10
10
  def type_name(node)
11
11
  case node
12
12
  when Prism::ConstantReadNode, Prism::ConstantPathNode
@@ -15,19 +15,19 @@ module RBS
15
15
  end
16
16
  end
17
17
 
18
- # @rbs! type t = ClassDecl | ModuleDecl | ConstantDecl
19
-
20
18
  # @rbs!
19
+ # type t = ClassDecl | ModuleDecl | ConstantDecl | SingletonClassDecl | BlockDecl
20
+ #
21
21
  # interface _WithComments
22
22
  # def comments: () -> AnnotationParser::ParsingResult?
23
23
  # end
24
24
 
25
25
  # @rbs module-self _WithComments
26
26
  module Generics
27
- # @rbs returns Array[RBS::AST::TypeParam]
27
+ # @rbs return: Array[RBS::AST::TypeParam]
28
28
  def type_params
29
29
  if comments = comments()
30
- comments.annotations.filter_map do |annotation|
30
+ comments.each_annotation.filter_map do |annotation|
31
31
  if annotation.is_a?(Annotations::Generic)
32
32
  annotation.type_param
33
33
  end
@@ -44,26 +44,26 @@ module RBS
44
44
  # @rbs generic NODE < Prism::Node
45
45
  class ModuleOrClass < Base
46
46
  # The node that represents the declaration
47
- attr_reader :node #:: NODE
47
+ attr_reader :node #: NODE
48
48
 
49
49
  # Leading comment
50
- attr_reader :comments #:: AnnotationParser::ParsingResult?
50
+ attr_reader :comments #: AnnotationParser::ParsingResult?
51
51
 
52
52
  # Members included in the declaration
53
- attr_reader :members #:: Array[Members::t | t]
53
+ attr_reader :members #: Array[Members::t | t]
54
54
 
55
55
  # @rbs node: NODE
56
56
  # @rbs comments: AnnotationParser::ParsingResult?
57
- def initialize(node, comments) #:: void
57
+ def initialize(node, comments) #: void
58
58
  @node = node
59
59
  @comments = comments
60
60
  @members = []
61
61
  end
62
62
 
63
63
  # Type parameters for the declaration
64
- def type_params #:: Array[RBS::AST::TypeParam]
64
+ def type_params #: Array[RBS::AST::TypeParam]
65
65
  if comments = comments()
66
- comments.annotations.filter_map do |annotation|
66
+ comments.each_annotation.filter_map do |annotation|
67
67
  if annotation.is_a?(Annotations::Generic)
68
68
  annotation.type_param
69
69
  end
@@ -73,7 +73,7 @@ module RBS
73
73
  end
74
74
  end
75
75
 
76
- def start_line #:: Integer
76
+ def start_line #: Integer
77
77
  node.location.start_line
78
78
  end
79
79
  end
@@ -82,12 +82,12 @@ module RBS
82
82
  include ConstantUtil
83
83
 
84
84
  # Type application for super class
85
- attr_reader :super_app #:: Annotations::Application?
85
+ attr_reader :super_app #: Annotations::Application?
86
86
 
87
87
  # @rbs node: Prism::ClassNode
88
88
  # @rbs comments: AnnotationParser::ParsingResult?
89
89
  # @rbs super_app: Annotations::Application?
90
- # @rbs returns void
90
+ # @rbs return: void
91
91
  def initialize(node, comments, super_app)
92
92
  super(node, comments)
93
93
 
@@ -95,14 +95,14 @@ module RBS
95
95
  end
96
96
 
97
97
  # @rbs %a{pure}
98
- def class_name #:: TypeName?
98
+ def class_name #: TypeName?
99
99
  type_name(node.constant_path)
100
100
  end
101
101
 
102
102
  # @rbs %a{pure}
103
- def super_class #:: RBS::AST::Declarations::Class::Super?
103
+ def super_class #: RBS::AST::Declarations::Class::Super?
104
104
  if comments
105
- if inherits = comments.annotations.find {|a| a.is_a?(Annotations::Inherits) } #: Annotations::Inherits?
105
+ if inherits = comments.each_annotation.find {|a| a.is_a?(Annotations::Inherits) } #: Annotations::Inherits?
106
106
  super_name = inherits.super_name
107
107
  super_args = inherits.args
108
108
 
@@ -141,14 +141,14 @@ module RBS
141
141
  include ConstantUtil
142
142
 
143
143
  # @rbs %a{pure}
144
- def module_name #:: TypeName?
144
+ def module_name #: TypeName?
145
145
  type_name(node.constant_path)
146
146
  end
147
147
 
148
148
  # @rbs %a{pure}
149
- def module_selfs #:: Array[Annotations::ModuleSelf]
149
+ def module_selfs #: Array[Annotations::ModuleSelf]
150
150
  if comments
151
- comments.annotations.filter_map do |ann|
151
+ comments.each_annotation.filter_map do |ann|
152
152
  if ann.is_a?(AST::Annotations::ModuleSelf)
153
153
  ann
154
154
  end
@@ -162,21 +162,21 @@ module RBS
162
162
  class ConstantDecl < Base
163
163
  include ConstantUtil
164
164
 
165
- attr_reader :node #:: Prism::ConstantWriteNode
166
- attr_reader :comments #:: AnnotationParser::ParsingResult?
167
- attr_reader :assertion #:: Annotations::Assertion?
165
+ attr_reader :node #: Prism::ConstantWriteNode
166
+ attr_reader :comments #: AnnotationParser::ParsingResult?
167
+ attr_reader :assertion #: Annotations::TypeAssertion?
168
168
 
169
169
  # @rbs node: Prism::ConstantWriteNode
170
170
  # @rbs comments: AnnotationParser::ParsingResult?
171
- # @rbs assertion: Annotations::Assertion?
172
- def initialize(node, comments, assertion)
171
+ # @rbs assertion: Annotations::TypeAssertion?
172
+ def initialize(node, comments, assertion) #: void
173
173
  @node = node
174
174
  @comments = comments
175
175
  @assertion = assertion
176
176
  end
177
177
 
178
178
  # @rbs %a{pure}
179
- # @rbs returns Types::t
179
+ # @rbs return: Types::t
180
180
  def type
181
181
  if assertion
182
182
  case assertion.type
@@ -218,15 +218,54 @@ module RBS
218
218
  end
219
219
 
220
220
  # @rbs %a{pure}
221
- # @rbs returns TypeName?
221
+ # @rbs return: TypeName?
222
222
  def constant_name
223
223
  TypeName.new(name: node.name, namespace: Namespace.empty)
224
224
  end
225
225
 
226
- def start_line #:: Integer
226
+ def start_line #: Integer
227
227
  node.location.start_line
228
228
  end
229
229
  end
230
+
231
+ class SingletonClassDecl < ModuleOrClass #[Prism::SingletonClassNode]
232
+ end
233
+
234
+ class BlockDecl < Base
235
+ attr_reader :node #: Prism::BlockNode
236
+
237
+ attr_reader :comments #: AnnotationParser::ParsingResult?
238
+
239
+ # Members included in the declaration
240
+ attr_reader :members #: Array[Members::t | t]
241
+
242
+ # @rbs (Prism::BlockNode, AnnotationParser::ParsingResult?) -> void
243
+ def initialize(node, comments)
244
+ @node = node
245
+ @members = []
246
+ @comments = comments
247
+ end
248
+
249
+ def start_line #: Integer
250
+ node.location.start_line
251
+ end
252
+
253
+ def module_class_annotation #: Annotations::ModuleDecl | Annotations::ClassDecl | nil
254
+ if comments
255
+ comments.each_annotation.each do |annotation|
256
+ if annotation.is_a?(Annotations::ModuleDecl)
257
+ return annotation
258
+ end
259
+
260
+ if annotation.is_a?(Annotations::ClassDecl)
261
+ return annotation
262
+ end
263
+ end
264
+
265
+ nil
266
+ end
267
+ end
268
+ end
230
269
  end
231
270
  end
232
271
  end