rbs-inline 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,7 +3,7 @@
3
3
  module RBS
4
4
  module Inline
5
5
  module AST
6
- # CommentLines represents consecutive comments
6
+ # CommentLines represents consecutive comments, providing a mapping from locations in `#string` to a pair of a comment and its offset
7
7
  #
8
8
  # The comments construct one String.
9
9
  #
@@ -16,37 +16,34 @@ module RBS
16
16
  # And want to translate a location in the string into the location in comment1 and comment2.
17
17
  #
18
18
  class CommentLines
19
- attr_reader :comments #:: Array[[Prism::Comment, Integer]]
19
+ attr_reader :comments #: Array[Prism::Comment]
20
20
 
21
21
  # @rbs comments: Array[Prism::Comment]
22
- def initialize(comments)
23
- offsets = comments.map do |comment|
24
- comment.location.slice.index(/[^#\s]/) || 1
25
- end
26
- first_offset = offsets[0]
27
-
28
- @comments = comments.map.with_index do |comment, index|
29
- offset = offsets[index]
30
- offset = first_offset if offset > first_offset
22
+ def initialize(comments) #: void
23
+ @comments = comments
24
+ end
31
25
 
32
- [comment, offset]
33
- end
26
+ def lines #: Array[String]
27
+ comments.map {|comment| comment.location.slice }
34
28
  end
35
29
 
36
- def string #:: String
37
- comments.map {|comment, offset| comment.location.slice[offset..] }.join("\n")
30
+ def string #: String
31
+ comments.map {|comment| comment.location.slice[1..] || "" }.join("\n")
38
32
  end
39
33
 
34
+ # Translates the cursor index of `#string` into the cursor index of a specific comment object
35
+ #
40
36
  # @rbs index: Integer
41
- # @rbs returns [Prism::Comment, Integer]?
37
+ # @rbs return: [Prism::Comment, Integer]?
42
38
  def comment_location(index)
43
- comments.each do |comment, offset|
39
+ comments.each do |comment|
44
40
  comment_length = comment.location.length
45
41
 
46
- if index + offset <= comment_length
47
- return [comment, index + offset]
42
+ if index + 1 <= comment_length
43
+ return [comment, index + 1]
48
44
  else
49
- index = index - comment_length + offset - 1
45
+ index -= comment_length - 1
46
+ index -= 1 # newline
50
47
  return if index < 0
51
48
  end
52
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 | SingletonClassDecl
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,18 +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
230
 
231
231
  class SingletonClassDecl < ModuleOrClass #[Prism::SingletonClassNode]
232
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
233
269
  end
234
270
  end
235
271
  end