syntax_tree-rbs 0.4.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.
@@ -1,226 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RBS
4
- module AST
5
- module Declarations
6
- class Alias
7
- # Prints out a type alias, which is a declaration that looks like:
8
- # type foo = String
9
- def format(q)
10
- SyntaxTree::RBS::Comment.maybe_format(q, comment)
11
- SyntaxTree::RBS::Annotations.maybe_format(q, annotations)
12
-
13
- q.group do
14
- q.text("type ")
15
- name.format(q)
16
- q.text(" =")
17
- q.group do
18
- q.indent do
19
- q.breakable
20
- type.format(q)
21
- end
22
- end
23
- end
24
- end
25
-
26
- def pretty_print(q)
27
- q.group(2, "(constant", ")") do
28
- SyntaxTree::RBS::Comment.maybe_pretty_print(q, comment)
29
- SyntaxTree::RBS::Annotations.maybe_pretty_print(q, annotations)
30
-
31
- q.breakable
32
- q.text("name=")
33
- q.pp(name)
34
-
35
- q.breakable
36
- q.text("type=")
37
- q.pp(type)
38
- end
39
- end
40
- end
41
-
42
- class Class
43
- # Prints out a class declarations, which looks like:
44
- # class Foo end
45
- def format(q)
46
- SyntaxTree::RBS::Comment.maybe_format(q, comment)
47
- SyntaxTree::RBS::Annotations.maybe_format(q, annotations)
48
-
49
- q.group do
50
- q.text("class ")
51
- SyntaxTree::RBS::NameAndTypeParams.new(self).format(q)
52
-
53
- if super_class
54
- q.text(" < ")
55
- SyntaxTree::RBS::NameAndArgs.new(super_class).format(q)
56
- end
57
-
58
- q.indent do
59
- SyntaxTree::RBS::Members.new(self).format(q)
60
- end
61
-
62
- q.breakable(force: true)
63
- q.text("end")
64
- end
65
- end
66
-
67
- def pretty_print(q)
68
- q.group(2, "(class", ")") do
69
- SyntaxTree::RBS::Comment.maybe_pretty_print(q, comment)
70
- SyntaxTree::RBS::Annotations.maybe_pretty_print(q, annotations)
71
-
72
- q.pp(SyntaxTree::RBS::NameAndTypeParams.new(self))
73
-
74
- if super_class
75
- q.breakable
76
- q.text("super_class=")
77
- q.group(2, "(class", ")") do
78
- q.pp(SyntaxTree::RBS::NameAndArgs.new(super_class))
79
- end
80
- end
81
-
82
- q.breakable
83
- q.text("members=")
84
- q.pp(members)
85
- end
86
- end
87
- end
88
-
89
- class Constant
90
- # Prints out a constant declaration, which looks like:
91
- # Foo: String
92
- def format(q)
93
- SyntaxTree::RBS::Comment.maybe_format(q, comment)
94
-
95
- q.group do
96
- name.format(q)
97
- q.text(": ")
98
- type.format(q)
99
- end
100
- end
101
-
102
- def pretty_print(q)
103
- q.group(2, "(constant", ")") do
104
- SyntaxTree::RBS::Comment.maybe_pretty_print(q, comment)
105
-
106
- q.breakable
107
- q.text("name=")
108
- q.pp(name)
109
-
110
- q.breakable
111
- q.text("type=")
112
- q.pp(type)
113
- end
114
- end
115
- end
116
-
117
- class Global
118
- # Prints out a global declaration, which looks like:
119
- # $foo: String
120
- def format(q)
121
- SyntaxTree::RBS::Comment.maybe_format(q, comment)
122
-
123
- q.group do
124
- q.text(name)
125
- q.text(": ")
126
- type.format(q)
127
- end
128
- end
129
-
130
- def pretty_print(q)
131
- q.group(2, "(global", ")") do
132
- SyntaxTree::RBS::Comment.maybe_pretty_print(q, comment)
133
-
134
- q.breakable
135
- q.text("name=")
136
- q.pp(name)
137
-
138
- q.breakable
139
- q.text("type=")
140
- q.pp(type)
141
- end
142
- end
143
- end
144
-
145
- class Interface
146
- # Prints out an interface declaration, which looks like:
147
- # interface _Foo end
148
- def format(q)
149
- SyntaxTree::RBS::Comment.maybe_format(q, comment)
150
- SyntaxTree::RBS::Annotations.maybe_format(q, annotations)
151
-
152
- q.group do
153
- q.text("interface ")
154
- SyntaxTree::RBS::NameAndTypeParams.new(self).format(q)
155
- q.indent { SyntaxTree::RBS::Members.new(self).format(q) }
156
- q.breakable(force: true)
157
- q.text("end")
158
- end
159
- end
160
-
161
- def pretty_print(q)
162
- q.group(2, "(interface", ")") do
163
- SyntaxTree::RBS::Comment.maybe_pretty_print(q, comment)
164
- SyntaxTree::RBS::Annotations.maybe_pretty_print(q, annotations)
165
-
166
- q.pp(SyntaxTree::RBS::NameAndTypeParams.new(self))
167
-
168
- q.breakable
169
- q.text("members=")
170
- q.pp(members)
171
- end
172
- end
173
- end
174
-
175
- class Module
176
- # Prints out a module declaration, which looks like:
177
- # module Foo end
178
- def format(q)
179
- SyntaxTree::RBS::Comment.maybe_format(q, comment)
180
- SyntaxTree::RBS::Annotations.maybe_format(q, annotations)
181
-
182
- q.group do
183
- q.text("module ")
184
- SyntaxTree::RBS::NameAndTypeParams.new(self).format(q)
185
-
186
- if self_types.any?
187
- q.text(" : ")
188
- q.seplist(self_types, -> { q.text(", ") }) do |self_type|
189
- SyntaxTree::RBS::NameAndArgs.new(self_type).format(q)
190
- end
191
- end
192
-
193
- q.indent { SyntaxTree::RBS::Members.new(self).format(q) }
194
- q.breakable(force: true)
195
- q.text("end")
196
- end
197
- end
198
-
199
- def pretty_print(q)
200
- q.group(2, "(module", ")") do
201
- SyntaxTree::RBS::Comment.maybe_pretty_print(q, comment)
202
- SyntaxTree::RBS::Annotations.maybe_pretty_print(q, annotations)
203
-
204
- q.pp(SyntaxTree::RBS::NameAndTypeParams.new(self))
205
-
206
- if self_types.any?
207
- q.breakable
208
- q.text("self_types=")
209
- q.group(2, "[", "]") do
210
- q.seplist(self_types) do |self_type|
211
- q.group(2, "(self-type", ")") do
212
- q.pp(SyntaxTree::RBS::NameAndArgs.new(self_type))
213
- end
214
- end
215
- end
216
- end
217
-
218
- q.breakable
219
- q.text("members=")
220
- q.pp(members)
221
- end
222
- end
223
- end
224
- end
225
- end
226
- end
@@ -1,414 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SyntaxTree
4
- module RBS
5
- # Prints out an attr_* meta method, which looks like:
6
- # attr_accessor foo: Foo
7
- # attr_reader foo: Foo
8
- # attr_writer foo: Foo
9
- class Attribute
10
- attr_reader :type, :node
11
-
12
- def initialize(type, node)
13
- @type = type
14
- @node = node
15
- end
16
-
17
- def format(q)
18
- q.group do
19
- q.text("#{node.visibility} ") if node.visibility
20
- q.text("attr_#{type} ")
21
- q.text("self.") if node.kind == :singleton
22
- q.text(node.name)
23
-
24
- if node.ivar_name == false
25
- q.text("()")
26
- elsif node.ivar_name
27
- q.text("(")
28
- q.text(node.ivar_name)
29
- q.text(")")
30
- end
31
-
32
- q.text(": ")
33
- node.type.format(q)
34
- end
35
- end
36
-
37
- def pretty_print(q)
38
- if node.kind == :singleton
39
- q.breakable
40
- q.text("singleton")
41
- end
42
-
43
- q.breakable
44
- q.text("name=")
45
- q.pp(node.name)
46
-
47
- if node.visibility
48
- q.breakable
49
- q.text("visibility=")
50
- q.pp(node.visibility)
51
- end
52
-
53
- unless node.ivar_name.nil?
54
- q.breakable
55
- q.text("ivar_name=")
56
- q.pp(node.ivar_name)
57
- end
58
-
59
- q.breakable
60
- q.text("type=")
61
- q.pp(node.type)
62
- end
63
- end
64
- end
65
- end
66
-
67
- module RBS
68
- module AST
69
- module Members
70
- class Alias
71
- # Prints out an alias within a declaration, which looks like:
72
- # alias foo bar
73
- # alias self.foo self.bar
74
- def format(q)
75
- SyntaxTree::RBS::Comment.maybe_format(q, comment)
76
- SyntaxTree::RBS::Annotations.maybe_format(q, annotations)
77
-
78
- if kind == :singleton
79
- q.text("alias self.")
80
- q.text(new_name)
81
- q.text(" self.")
82
- q.text(old_name)
83
- else
84
- q.text("alias ")
85
- q.text(new_name)
86
- q.text(" ")
87
- q.text(old_name)
88
- end
89
- end
90
-
91
- def pretty_print(q)
92
- q.group(2, "(alias", ")") do
93
- SyntaxTree::RBS::Comment.maybe_pretty_print(q, comment)
94
- SyntaxTree::RBS::Annotations.maybe_pretty_print(q, annotations)
95
-
96
- if kind == :singleton
97
- q.breakable
98
- q.text("singleton")
99
- end
100
-
101
- q.breakable
102
- q.text("new_name=")
103
- q.pp(new_name)
104
-
105
- q.breakable
106
- q.text("old_name=")
107
- q.pp(old_name)
108
- end
109
- end
110
- end
111
-
112
- class AttrAccessor
113
- # Prints out an attr_accessor meta method, which looks like:
114
- # attr_accessor foo: Foo
115
- def format(q)
116
- SyntaxTree::RBS::Comment.maybe_format(q, comment)
117
- SyntaxTree::RBS::Annotations.maybe_format(q, annotations)
118
- SyntaxTree::RBS::Attribute.new(:accessor, self).format(q)
119
- end
120
-
121
- def pretty_print(q)
122
- q.group(2, "(attr-accessor", ")") do
123
- SyntaxTree::RBS::Comment.maybe_pretty_print(q, comment)
124
- SyntaxTree::RBS::Annotations.maybe_pretty_print(q, annotations)
125
- q.pp(SyntaxTree::RBS::Attribute.new(:accessor, self))
126
- end
127
- end
128
- end
129
-
130
- class AttrReader
131
- # Prints out an attr_reader meta method, which looks like:
132
- # attr_reader foo: Foo
133
- def format(q)
134
- SyntaxTree::RBS::Comment.maybe_format(q, comment)
135
- SyntaxTree::RBS::Annotations.maybe_format(q, annotations)
136
- SyntaxTree::RBS::Attribute.new(:reader, self).format(q)
137
- end
138
-
139
- def pretty_print(q)
140
- q.group(2, "(attr-reader", ")") do
141
- SyntaxTree::RBS::Comment.maybe_pretty_print(q, comment)
142
- SyntaxTree::RBS::Annotations.maybe_pretty_print(q, annotations)
143
- q.pp(SyntaxTree::RBS::Attribute.new(:reader, self))
144
- end
145
- end
146
- end
147
-
148
- class AttrWriter
149
- # Prints out an attr_writer meta method, which looks like:
150
- # attr_writer foo: Foo
151
- def format(q)
152
- SyntaxTree::RBS::Comment.maybe_format(q, comment)
153
- SyntaxTree::RBS::Annotations.maybe_format(q, annotations)
154
- SyntaxTree::RBS::Attribute.new(:writer, self).format(q)
155
- end
156
-
157
- def pretty_print(q)
158
- q.group(2, "(attr-writer", ")") do
159
- SyntaxTree::RBS::Comment.maybe_pretty_print(q, comment)
160
- SyntaxTree::RBS::Annotations.maybe_pretty_print(q, annotations)
161
- q.pp(SyntaxTree::RBS::Attribute.new(:writer, self))
162
- end
163
- end
164
- end
165
-
166
- class ClassInstanceVariable
167
- # Prints out a class instance variable member, which looks like:
168
- # self.@foo: String
169
- def format(q)
170
- SyntaxTree::RBS::Comment.maybe_format(q, comment)
171
-
172
- q.group do
173
- q.text("self.")
174
- q.text(name)
175
- q.text(": ")
176
- type.format(q)
177
- end
178
- end
179
-
180
- def pretty_print(q)
181
- q.group(2, "(class-instance-variable", ")") do
182
- SyntaxTree::RBS::Comment.maybe_pretty_print(q, comment)
183
-
184
- q.breakable
185
- q.text("name=")
186
- q.pp(name)
187
- end
188
- end
189
- end
190
-
191
- class ClassVariable
192
- # Prints out a class variable member, which looks like:
193
- # @@foo: String
194
- def format(q)
195
- SyntaxTree::RBS::Comment.maybe_format(q, comment)
196
-
197
- q.group do
198
- q.text(name)
199
- q.text(": ")
200
- type.format(q)
201
- end
202
- end
203
-
204
- def pretty_print(q)
205
- q.group(2, "(class-variable", ")") do
206
- SyntaxTree::RBS::Comment.maybe_pretty_print(q, comment)
207
-
208
- q.breakable
209
- q.text("name=")
210
- q.pp(name)
211
- end
212
- end
213
- end
214
-
215
- class Extend
216
- # Prints out an extend, which looks like:
217
- # extend Foo
218
- def format(q)
219
- SyntaxTree::RBS::Comment.maybe_format(q, comment)
220
- SyntaxTree::RBS::Annotations.maybe_format(q, annotations)
221
-
222
- q.group do
223
- q.text("extend ")
224
- SyntaxTree::RBS::NameAndArgs.new(self).format(q)
225
- end
226
- end
227
-
228
- def pretty_print(q)
229
- q.group(2, "(extend", ")") do
230
- SyntaxTree::RBS::Comment.maybe_pretty_print(q, comment)
231
- SyntaxTree::RBS::Annotations.maybe_pretty_print(q, annotations)
232
- q.pp(SyntaxTree::RBS::NameAndArgs.new(self))
233
- end
234
- end
235
- end
236
-
237
- class Include
238
- # Prints out an include, which looks like:
239
- # include Foo
240
- def format(q)
241
- SyntaxTree::RBS::Comment.maybe_format(q, comment)
242
- SyntaxTree::RBS::Annotations.maybe_format(q, annotations)
243
-
244
- q.group do
245
- q.text("include ")
246
- SyntaxTree::RBS::NameAndArgs.new(self).format(q)
247
- end
248
- end
249
-
250
- def pretty_print(q)
251
- q.group(2, "(include", ")") do
252
- SyntaxTree::RBS::Comment.maybe_pretty_print(q, comment)
253
- SyntaxTree::RBS::Annotations.maybe_pretty_print(q, annotations)
254
- q.pp(SyntaxTree::RBS::NameAndArgs.new(self))
255
- end
256
- end
257
- end
258
-
259
- class InstanceVariable
260
- # Prints out an instance variable member, which looks like:
261
- # @foo: String
262
- def format(q)
263
- SyntaxTree::RBS::Comment.maybe_format(q, comment)
264
-
265
- q.group do
266
- q.text(name)
267
- q.text(": ")
268
- type.format(q)
269
- end
270
- end
271
-
272
- def pretty_print(q)
273
- q.group(2, "(instance-variable", ")") do
274
- SyntaxTree::RBS::Comment.maybe_pretty_print(q, comment)
275
-
276
- q.breakable
277
- q.text("name=")
278
- q.pp(name)
279
- end
280
- end
281
- end
282
-
283
- class MethodDefinition
284
- # Prints out a method definition, which looks like:
285
- # def t: (T t) -> void
286
- def format(q)
287
- SyntaxTree::RBS::Comment.maybe_format(q, comment)
288
- SyntaxTree::RBS::Annotations.maybe_format(q, annotations)
289
-
290
- q.group do
291
- q.text("#{visibility} ") if visibility
292
- q.text("def ")
293
-
294
- if kind == :singleton
295
- q.text("self.")
296
- elsif kind == :singleton_instance
297
- q.text("self?.")
298
- end
299
-
300
- q.text(Parser::KEYWORDS.include?(name.to_s) ? "`#{name}`" : name)
301
- q.text(":")
302
-
303
- if types.length == 1 && !overload?
304
- q.text(" ")
305
- SyntaxTree::RBS::MethodSignature.new(types.first).format(q)
306
- else
307
- separator =
308
- lambda do
309
- q.breakable
310
- q.text("| ")
311
- end
312
-
313
- q.group do
314
- q.indent do
315
- q.breakable
316
- q.seplist(types, separator) do |type|
317
- SyntaxTree::RBS::MethodSignature.new(type).format(q)
318
- end
319
-
320
- if overload?
321
- separator.call
322
- q.text("...")
323
- end
324
- end
325
- end
326
- end
327
- end
328
- end
329
-
330
- def pretty_print(q)
331
- q.group(2, "(method-definition", ")") do
332
- SyntaxTree::RBS::Comment.maybe_pretty_print(q, comment)
333
- SyntaxTree::RBS::Annotations.maybe_pretty_print(q, annotations)
334
-
335
- q.breakable
336
- q.text("kind=")
337
- q.pp(kind)
338
-
339
- q.breakable
340
- q.text("name=")
341
- q.pp(name)
342
-
343
- if visibility
344
- q.breakable
345
- q.text("visibility=")
346
- q.pp(visibility)
347
- end
348
-
349
- if overload?
350
- q.breakable
351
- q.text("overload")
352
- end
353
-
354
- q.breakable
355
- q.text("types=")
356
- q.group(2, "[", "]") do
357
- q.breakable("")
358
- q.seplist(types) do |type|
359
- q.pp(SyntaxTree::RBS::MethodSignature.new(type))
360
- end
361
- q.breakable("")
362
- end
363
- end
364
- end
365
- end
366
-
367
- class Prepend
368
- # Prints out a prepend, which looks like:
369
- # prepend Foo
370
- def format(q)
371
- SyntaxTree::RBS::Comment.maybe_format(q, comment)
372
- SyntaxTree::RBS::Annotations.maybe_format(q, annotations)
373
-
374
- q.group do
375
- q.text("prepend ")
376
- SyntaxTree::RBS::NameAndArgs.new(self).format(q)
377
- end
378
- end
379
-
380
- def pretty_print(q)
381
- q.group(2, "(prepend", ")") do
382
- SyntaxTree::RBS::Comment.maybe_pretty_print(q, comment)
383
- SyntaxTree::RBS::Annotations.maybe_pretty_print(q, annotations)
384
- q.pp(SyntaxTree::RBS::NameAndArgs.new(self))
385
- end
386
- end
387
- end
388
-
389
- class Private
390
- # Prints out a private declaration, which looks like:
391
- # private
392
- def format(q)
393
- q.text("private")
394
- end
395
-
396
- def pretty_print(q)
397
- q.text("(private)")
398
- end
399
- end
400
-
401
- class Public
402
- # Prints out a public declaration, which looks like:
403
- # public
404
- def format(q)
405
- q.text("public")
406
- end
407
-
408
- def pretty_print(q)
409
- q.text("(public)")
410
- end
411
- end
412
- end
413
- end
414
- end