Class: RBS::AST::Ruby::Members::MethodTypeAnnotation::DocStyle

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/ast/ruby/members.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDocStyle

Returns a new instance of DocStyle.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rbs/ast/ruby/members.rb', line 29

def initialize
  @return_type_annotation = nil
  @required_positionals = []
  @optional_positionals = []
  @rest_positionals = nil
  @trailing_positionals = []
  @required_keywords = {}
  @optional_keywords = {}
  @rest_keywords = nil
  @block = nil
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



27
28
29
# File 'lib/rbs/ast/ruby/members.rb', line 27

def block
  @block
end

#optional_keywordsObject (readonly)

Returns the value of attribute optional_keywords.



25
26
27
# File 'lib/rbs/ast/ruby/members.rb', line 25

def optional_keywords
  @optional_keywords
end

#optional_positionalsObject (readonly)

Returns the value of attribute optional_positionals.



21
22
23
# File 'lib/rbs/ast/ruby/members.rb', line 21

def optional_positionals
  @optional_positionals
end

#required_keywordsObject (readonly)

Returns the value of attribute required_keywords.



24
25
26
# File 'lib/rbs/ast/ruby/members.rb', line 24

def required_keywords
  @required_keywords
end

#required_positionalsObject (readonly)

Returns the value of attribute required_positionals.



20
21
22
# File 'lib/rbs/ast/ruby/members.rb', line 20

def required_positionals
  @required_positionals
end

#rest_keywordsObject

Returns the value of attribute rest_keywords.



26
27
28
# File 'lib/rbs/ast/ruby/members.rb', line 26

def rest_keywords
  @rest_keywords
end

#rest_positionalsObject

Returns the value of attribute rest_positionals.



22
23
24
# File 'lib/rbs/ast/ruby/members.rb', line 22

def rest_positionals
  @rest_positionals
end

#return_type_annotationObject

Returns the value of attribute return_type_annotation.



19
20
21
# File 'lib/rbs/ast/ruby/members.rb', line 19

def return_type_annotation
  @return_type_annotation
end

#trailing_positionalsObject (readonly)

Returns the value of attribute trailing_positionals.



23
24
25
# File 'lib/rbs/ast/ruby/members.rb', line 23

def trailing_positionals
  @trailing_positionals
end

Class Method Details

.build(param_type_annotations, return_type_annotation, node) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/rbs/ast/ruby/members.rb', line 41

def self.build(param_type_annotations, return_type_annotation, node)
  doc = DocStyle.new
  doc.return_type_annotation = return_type_annotation

  splat_annotation = nil #: Annotations::SplatParamTypeAnnotation?
  double_splat_annotation = nil #: Annotations::DoubleSplatParamTypeAnnotation?
  block_annotation = nil #: Annotations::BlockParamTypeAnnotation?
  param_annotations = {} #: Hash[Symbol, Annotations::ParamTypeAnnotation]
  unused = [] #: Array[Annotations::ParamTypeAnnotation | Annotations::SplatParamTypeAnnotation | Annotations::DoubleSplatParamTypeAnnotation | Annotations::BlockParamTypeAnnotation]

  param_type_annotations.each do |annot|
    case annot
    when Annotations::SplatParamTypeAnnotation
      if splat_annotation
        unused << annot
      else
        splat_annotation = annot
      end
    when Annotations::DoubleSplatParamTypeAnnotation
      if double_splat_annotation
        unused << annot
      else
        double_splat_annotation = annot
      end
    when Annotations::BlockParamTypeAnnotation
      if block_annotation
        unused << annot
      else
        block_annotation = annot
      end
    when Annotations::ParamTypeAnnotation
      name = annot.name_location.source.to_sym
      if param_annotations.key?(name)
        unused << annot
      else
        param_annotations[name] = annot
      end
    end
  end

  if node.parameters
    params = node.parameters #: Prism::ParametersNode

    params.requireds.each do |param|
      if param.is_a?(Prism::RequiredParameterNode)
        annotation = param_annotations.delete(param.name)
        if annotation
          doc.required_positionals << annotation
        else
          doc.required_positionals << param.name
        end
      end
    end

    params.optionals.each do |param|
      if param.is_a?(Prism::OptionalParameterNode)
        annotation = param_annotations.delete(param.name)
        if annotation
          doc.optional_positionals << annotation
        else
          doc.optional_positionals << param.name
        end
      end
    end

    if (rest = params.rest) && rest.is_a?(Prism::RestParameterNode)
      if splat_annotation && (splat_annotation.name_location.nil? || rest.name.nil? || splat_annotation.name_location.source.to_sym == rest.name)
        doc.rest_positionals = splat_annotation
        splat_annotation = nil
      else
        doc.rest_positionals = rest.name || true
      end
    end

    params.posts.each do |param|
      if param.is_a?(Prism::RequiredParameterNode)
        annotation = param_annotations.delete(param.name)
        if annotation
          doc.trailing_positionals << annotation
        else
          doc.trailing_positionals << param.name
        end
      end
    end

    params.keywords.each do |param|
      case param
      when Prism::RequiredKeywordParameterNode
        annotation = param_annotations.delete(param.name)
        if annotation
          doc.required_keywords[param.name] = annotation
        else
          doc.required_keywords[param.name] = param.name
        end
      when Prism::OptionalKeywordParameterNode
        annotation = param_annotations.delete(param.name)
        if annotation
          doc.optional_keywords[param.name] = annotation
        else
          doc.optional_keywords[param.name] = param.name
        end
      end
    end

    if (kw_rest = params.keyword_rest) && kw_rest.is_a?(Prism::KeywordRestParameterNode)
      if double_splat_annotation && (double_splat_annotation.name_location.nil? || kw_rest.name.nil? || double_splat_annotation.name_location.source.to_sym == kw_rest.name)
        doc.rest_keywords = double_splat_annotation
        double_splat_annotation = nil
      else
        doc.rest_keywords = kw_rest.name || true
      end
    end

    if (blk = params.block) && blk.is_a?(Prism::BlockParameterNode)
      if block_annotation && (block_annotation.name_location.nil? || blk.name.nil? || block_annotation.name == blk.name)
        doc.block = block_annotation
        block_annotation = nil
      else
        doc.block = blk.name || true
      end
    end
  end

  if block_annotation
    if node.parameters&.block
      # Block parameter exists but name didn't match -- treat as unused
    else
      doc.block = block_annotation
      block_annotation = nil
    end
  end

  unused.concat(param_annotations.values)
  unused << splat_annotation if splat_annotation
  unused << double_splat_annotation if double_splat_annotation
  unused << block_annotation if block_annotation

  [doc, unused]
end

Instance Method Details

#all_param_annotationsObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/rbs/ast/ruby/members.rb', line 181

def all_param_annotations
  annotations = [] #: Array[param_type_annotation | Symbol | true | nil]

  required_positionals.each { |a| annotations << a }
  optional_positionals.each { |a| annotations << a }
  annotations << rest_positionals
  trailing_positionals.each { |a| annotations << a }
  required_keywords.each_value { |a| annotations << a }
  optional_keywords.each_value { |a| annotations << a }
  annotations << rest_keywords
  annotations << block

  annotations
end

#map_type_name(&block) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/rbs/ast/ruby/members.rb', line 196

def map_type_name(&block)
  DocStyle.new.tap do |new|
    new.return_type_annotation = return_type_annotation&.map_type_name(&block)
    new.required_positionals.replace(
      required_positionals.map do |a|
        case a
        when Annotations::ParamTypeAnnotation
          a.map_type_name(&block)
        else
          a
        end
      end
    )
    new.optional_positionals.replace(
      optional_positionals.map do |a|
        case a
        when Annotations::ParamTypeAnnotation
          a.map_type_name(&block)
        else
          a
        end
      end
    )
    new.rest_positionals =
      case rest_positionals
      when Annotations::SplatParamTypeAnnotation
        rest_positionals.map_type_name(&block)
      else
        rest_positionals
      end
    new.trailing_positionals.replace(
      trailing_positionals.map do |a|
        case a
        when Annotations::ParamTypeAnnotation
          a.map_type_name(&block)
        else
          a
        end
      end
    )
    new.required_keywords.replace(
      required_keywords.transform_values do |a|
        case a
        when Annotations::ParamTypeAnnotation
          a.map_type_name(&block)
        else
          a
        end
      end
    )
    new.optional_keywords.replace(
      optional_keywords.transform_values do |a|
        case a
        when Annotations::ParamTypeAnnotation
          a.map_type_name(&block)
        else
          a
        end
      end
    )
    new.rest_keywords =
      case rest_keywords
      when Annotations::DoubleSplatParamTypeAnnotation
        rest_keywords.map_type_name(&block)
      else
        rest_keywords
      end
    new.block =
      case self.block
      when Annotations::BlockParamTypeAnnotation
        self.block.map_type_name(&block)
      else
        self.block
      end
  end #: self
end

#method_typeObject



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/rbs/ast/ruby/members.rb', line 287

def method_type
  return_type =
    case return_type_annotation
    when Annotations::NodeTypeAssertion
      return_type_annotation.type
    when Annotations::ReturnTypeAnnotation
      return_type_annotation.return_type
    else
      Types::Bases::Any.new(location: nil)
    end

  any = -> { Types::Bases::Any.new(location: nil) }

  req_pos = required_positionals.map do |a|
    case a
    when Annotations::ParamTypeAnnotation
      Types::Function::Param.new(type: a.param_type, name: a.name_location.source.to_sym)
    else
      Types::Function::Param.new(type: any.call, name: a)
    end
  end
  opt_pos = optional_positionals.map do |a|
    case a
    when Annotations::ParamTypeAnnotation
      Types::Function::Param.new(type: a.param_type, name: a.name_location.source.to_sym)
    else
      Types::Function::Param.new(type: any.call, name: a)
    end
  end
  rest_pos =
    case rest_positionals
    when Annotations::SplatParamTypeAnnotation
      Types::Function::Param.new(type: rest_positionals.param_type, name: rest_positionals.name_location&.source&.to_sym)
    when Symbol
      Types::Function::Param.new(type: any.call, name: rest_positionals)
    when true
      Types::Function::Param.new(type: any.call, name: nil)
    else
      nil
    end
  trail_pos = trailing_positionals.map do |a|
    case a
    when Annotations::ParamTypeAnnotation
      Types::Function::Param.new(type: a.param_type, name: a.name_location.source.to_sym)
    else
      Types::Function::Param.new(type: any.call, name: a)
    end
  end

  req_kw = required_keywords.transform_values do |a|
    case a
    when Annotations::ParamTypeAnnotation
      Types::Function::Param.new(type: a.param_type, name: nil)
    else
      Types::Function::Param.new(type: any.call, name: nil)
    end
  end
  opt_kw = optional_keywords.transform_values do |a|
    case a
    when Annotations::ParamTypeAnnotation
      Types::Function::Param.new(type: a.param_type, name: nil)
    else
      Types::Function::Param.new(type: any.call, name: nil)
    end
  end

  rest_kw =
    case rest_keywords
    when Annotations::DoubleSplatParamTypeAnnotation
      Types::Function::Param.new(type: rest_keywords.param_type, name: rest_keywords.name_location&.source&.to_sym)
    when Symbol
      Types::Function::Param.new(type: any.call, name: rest_keywords)
    when true
      Types::Function::Param.new(type: any.call, name: nil)
    else
      nil
    end

  type = Types::Function.new(
    required_positionals: req_pos,
    optional_positionals: opt_pos,
    rest_positionals: rest_pos,
    trailing_positionals: trail_pos,
    required_keywords: req_kw,
    optional_keywords: opt_kw,
    rest_keywords: rest_kw,
    return_type: return_type
  )

  method_block =
    case self.block
    when Annotations::BlockParamTypeAnnotation
      Types::Block.new(
        type: self.block.type,
        required: self.block.required?
      )
    when Symbol, true
      Types::Block.new(
        type: Types::UntypedFunction.new(return_type: Types::Bases::Any.new(location: nil)),
        required: false
      )
    else
      nil
    end

  MethodType.new(
    type_params: [],
    type: type,
    block: method_block,
    location: nil
  )
end

#type_fingerprintObject



273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/rbs/ast/ruby/members.rb', line 273

def type_fingerprint
  [
    return_type_annotation&.type_fingerprint,
    all_param_annotations.map do |param|
      case param
      when Annotations::Base
        param.type_fingerprint
      else
        param
      end
    end
  ]
end