parlour 4.0.0 → 5.0.0.beta.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/ISSUE_TEMPLATE/bug-report.md +0 -0
- data/.github/ISSUE_TEMPLATE/feature-request.md +0 -0
- data/.gitignore +1 -1
- data/.parlour +0 -0
- data/.rspec +0 -0
- data/.travis.yml +0 -0
- data/CHANGELOG.md +52 -0
- data/CODE_OF_CONDUCT.md +0 -0
- data/Gemfile +0 -0
- data/LICENSE.txt +0 -0
- data/README.md +208 -20
- data/Rakefile +0 -0
- data/exe/parlour +45 -6
- data/lib/parlour.rb +27 -1
- data/lib/parlour/conflict_resolver.rb +31 -9
- data/lib/parlour/conversion/converter.rb +34 -0
- data/lib/parlour/conversion/rbi_to_rbs.rb +229 -0
- data/lib/parlour/debugging.rb +0 -0
- data/lib/parlour/detached_rbi_generator.rb +0 -0
- data/lib/parlour/detached_rbs_generator.rb +25 -0
- data/lib/parlour/generator.rb +34 -0
- data/lib/parlour/kernel_hack.rb +0 -0
- data/lib/parlour/options.rb +71 -0
- data/lib/parlour/parse_error.rb +0 -0
- data/lib/parlour/plugin.rb +0 -0
- data/lib/parlour/rbi_generator.rb +24 -37
- data/lib/parlour/rbi_generator/arbitrary.rb +5 -2
- data/lib/parlour/rbi_generator/attribute.rb +14 -5
- data/lib/parlour/rbi_generator/class_namespace.rb +8 -3
- data/lib/parlour/rbi_generator/constant.rb +17 -6
- data/lib/parlour/rbi_generator/enum_class_namespace.rb +8 -3
- data/lib/parlour/rbi_generator/extend.rb +5 -2
- data/lib/parlour/rbi_generator/include.rb +5 -2
- data/lib/parlour/rbi_generator/method.rb +15 -10
- data/lib/parlour/rbi_generator/module_namespace.rb +7 -2
- data/lib/parlour/rbi_generator/namespace.rb +40 -13
- data/lib/parlour/rbi_generator/parameter.rb +11 -5
- data/lib/parlour/rbi_generator/rbi_object.rb +19 -78
- data/lib/parlour/rbi_generator/struct_class_namespace.rb +9 -2
- data/lib/parlour/rbi_generator/struct_prop.rb +12 -9
- data/lib/parlour/rbi_generator/type_alias.rb +101 -0
- data/lib/parlour/rbs_generator.rb +24 -0
- data/lib/parlour/rbs_generator/arbitrary.rb +92 -0
- data/lib/parlour/rbs_generator/attribute.rb +82 -0
- data/lib/parlour/rbs_generator/block.rb +49 -0
- data/lib/parlour/rbs_generator/class_namespace.rb +106 -0
- data/lib/parlour/rbs_generator/constant.rb +95 -0
- data/lib/parlour/rbs_generator/extend.rb +92 -0
- data/lib/parlour/rbs_generator/include.rb +92 -0
- data/lib/parlour/rbs_generator/interface_namespace.rb +34 -0
- data/lib/parlour/rbs_generator/method.rb +146 -0
- data/lib/parlour/rbs_generator/method_signature.rb +104 -0
- data/lib/parlour/rbs_generator/module_namespace.rb +35 -0
- data/lib/parlour/rbs_generator/namespace.rb +627 -0
- data/lib/parlour/rbs_generator/parameter.rb +146 -0
- data/lib/parlour/rbs_generator/rbs_object.rb +78 -0
- data/lib/parlour/rbs_generator/type_alias.rb +96 -0
- data/lib/parlour/type_loader.rb +0 -0
- data/lib/parlour/type_parser.rb +174 -5
- data/lib/parlour/typed_object.rb +87 -0
- data/lib/parlour/types.rb +539 -0
- data/lib/parlour/version.rb +1 -1
- data/parlour.gemspec +1 -1
- data/plugin_examples/foobar_plugin.rb +0 -0
- data/rbi/parlour.rbi +1404 -441
- metadata +33 -10
- data/lib/parlour/rbi_generator/options.rb +0 -74
@@ -1,13 +1,13 @@
|
|
1
1
|
# typed: true
|
2
2
|
module Parlour
|
3
|
-
class RbiGenerator
|
3
|
+
class RbiGenerator < Generator
|
4
4
|
# Represents a module definition.
|
5
5
|
class ModuleNamespace < Namespace
|
6
6
|
extend T::Sig
|
7
7
|
|
8
8
|
sig do
|
9
9
|
params(
|
10
|
-
generator:
|
10
|
+
generator: Generator,
|
11
11
|
name: String,
|
12
12
|
final: T::Boolean,
|
13
13
|
interface: T::Boolean,
|
@@ -96,6 +96,11 @@ module Parlour
|
|
96
96
|
"Module #{name} - #{"interface, " if interface}#{children.length} " +
|
97
97
|
"children, #{includes.length} includes, #{extends.length} extends"
|
98
98
|
end
|
99
|
+
|
100
|
+
sig { override.void }
|
101
|
+
def generalize_from_rbi!
|
102
|
+
super
|
103
|
+
end
|
99
104
|
end
|
100
105
|
end
|
101
106
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# typed: true
|
2
2
|
module Parlour
|
3
|
-
class RbiGenerator
|
3
|
+
class RbiGenerator < Generator
|
4
4
|
# A generic namespace. This shouldn't be used, except as the type of
|
5
5
|
# {RbiGenerator#root}.
|
6
6
|
class Namespace < RbiObject
|
@@ -24,7 +24,7 @@ module Parlour
|
|
24
24
|
|
25
25
|
sig do
|
26
26
|
params(
|
27
|
-
generator:
|
27
|
+
generator: Generator,
|
28
28
|
name: T.nilable(String),
|
29
29
|
final: T::Boolean,
|
30
30
|
block: T.nilable(T.proc.params(x: Namespace).void)
|
@@ -77,6 +77,17 @@ module Parlour
|
|
77
77
|
)
|
78
78
|
end
|
79
79
|
|
80
|
+
sig { returns(T::Array[RbiGenerator::TypeAlias]) }
|
81
|
+
# The {RbiGenerator::TypeAlias} objects from {children}.
|
82
|
+
# @return [Array<RbiGenerator::TypeAlias>]
|
83
|
+
def aliases
|
84
|
+
T.cast(
|
85
|
+
children.select { |c| c.is_a?(RbiGenerator::TypeAlias) },
|
86
|
+
T::Array[RbiGenerator::TypeAlias]
|
87
|
+
)
|
88
|
+
end
|
89
|
+
alias type_aliases aliases
|
90
|
+
|
80
91
|
sig { returns(T::Array[RbiGenerator::Constant]) }
|
81
92
|
# The {RbiGenerator::Constant} objects from {children}.
|
82
93
|
# @return [Array<RbiGenerator::Constant>]
|
@@ -260,8 +271,8 @@ module Parlour
|
|
260
271
|
params(
|
261
272
|
name: String,
|
262
273
|
parameters: T.nilable(T::Array[Parameter]),
|
263
|
-
return_type: T.nilable(
|
264
|
-
returns: T.nilable(
|
274
|
+
return_type: T.nilable(Types::TypeLike),
|
275
|
+
returns: T.nilable(Types::TypeLike),
|
265
276
|
abstract: T::Boolean,
|
266
277
|
implementation: T::Boolean,
|
267
278
|
override: T::Boolean,
|
@@ -320,7 +331,7 @@ module Parlour
|
|
320
331
|
params(
|
321
332
|
name: String,
|
322
333
|
kind: Symbol,
|
323
|
-
type:
|
334
|
+
type: Types::TypeLike,
|
324
335
|
class_attribute: T::Boolean,
|
325
336
|
block: T.nilable(T.proc.params(x: Attribute).void)
|
326
337
|
).returns(Attribute)
|
@@ -376,7 +387,7 @@ module Parlour
|
|
376
387
|
sig do
|
377
388
|
params(
|
378
389
|
name: String,
|
379
|
-
type:
|
390
|
+
type: Types::TypeLike,
|
380
391
|
class_attribute: T::Boolean,
|
381
392
|
block: T.nilable(T.proc.params(x: Attribute).void)
|
382
393
|
).returns(Attribute)
|
@@ -397,7 +408,7 @@ module Parlour
|
|
397
408
|
sig do
|
398
409
|
params(
|
399
410
|
name: String,
|
400
|
-
type:
|
411
|
+
type: Types::TypeLike,
|
401
412
|
class_attribute: T::Boolean,
|
402
413
|
block: T.nilable(T.proc.params(x: Attribute).void)
|
403
414
|
).returns(Attribute)
|
@@ -418,7 +429,7 @@ module Parlour
|
|
418
429
|
sig do
|
419
430
|
params(
|
420
431
|
name: String,
|
421
|
-
type:
|
432
|
+
type: Types::TypeLike,
|
422
433
|
class_attribute: T::Boolean,
|
423
434
|
block: T.nilable(T.proc.params(x: Attribute).void)
|
424
435
|
).returns(Attribute)
|
@@ -552,7 +563,7 @@ module Parlour
|
|
552
563
|
new_constant
|
553
564
|
end
|
554
565
|
|
555
|
-
sig { params(name: String, type:
|
566
|
+
sig { params(name: String, type: Types::TypeLike, block: T.nilable(T.proc.params(x: TypeAlias).void)).returns(TypeAlias) }
|
556
567
|
# Adds a new type alias, in the form of a constant, to this namespace.
|
557
568
|
#
|
558
569
|
# @example Add a +MyType+ type alias, to +Integer+, to the class.
|
@@ -563,7 +574,15 @@ module Parlour
|
|
563
574
|
# @param block A block which the new instance yields itself to.
|
564
575
|
# @return [RbiGenerator::Constant]
|
565
576
|
def create_type_alias(name, type:, &block)
|
566
|
-
|
577
|
+
new_type_alias = RbiGenerator::TypeAlias.new(
|
578
|
+
generator,
|
579
|
+
name: name,
|
580
|
+
type: type,
|
581
|
+
&block
|
582
|
+
)
|
583
|
+
move_next_comments(new_type_alias)
|
584
|
+
children << new_type_alias
|
585
|
+
new_type_alias
|
567
586
|
end
|
568
587
|
|
569
588
|
sig do
|
@@ -615,12 +634,17 @@ module Parlour
|
|
615
634
|
"includes, #{extends.length} extends, #{constants.length} constants"
|
616
635
|
end
|
617
636
|
|
637
|
+
sig { override.void }
|
638
|
+
def generalize_from_rbi!
|
639
|
+
children.each(&:generalize_from_rbi!)
|
640
|
+
end
|
641
|
+
|
618
642
|
private
|
619
643
|
|
620
644
|
sig do
|
621
645
|
overridable.params(
|
622
646
|
indent_level: Integer,
|
623
|
-
options: Options
|
647
|
+
options: Options,
|
624
648
|
).returns(T::Array[String])
|
625
649
|
end
|
626
650
|
# Generates the RBI lines for the body of this namespace. This consists of
|
@@ -639,13 +663,16 @@ module Parlour
|
|
639
663
|
eigen_constants, non_eigen_constants = constants.partition(&:eigen_constant)
|
640
664
|
eigen_constants.sort_by!(&:name) if options.sort_namespaces
|
641
665
|
|
642
|
-
if includes.any? || extends.any? || non_eigen_constants.any?
|
666
|
+
if includes.any? || extends.any? || aliases.any? || non_eigen_constants.any?
|
643
667
|
result += (options.sort_namespaces ? includes.sort_by(&:name) : includes)
|
644
668
|
.flat_map { |x| x.generate_rbi(indent_level, options) }
|
645
669
|
.reject { |x| x.strip == '' }
|
646
670
|
result += (options.sort_namespaces ? extends.sort_by(&:name) : extends)
|
647
671
|
.flat_map { |x| x.generate_rbi(indent_level, options) }
|
648
672
|
.reject { |x| x.strip == '' }
|
673
|
+
result += (options.sort_namespaces ? aliases.sort_by(&:name) : aliases)
|
674
|
+
.flat_map { |x| x.generate_rbi(indent_level, options) }
|
675
|
+
.reject { |x| x.strip == '' }
|
649
676
|
result += (options.sort_namespaces ? non_eigen_constants.sort_by(&:name) : non_eigen_constants)
|
650
677
|
.flat_map { |x| x.generate_rbi(indent_level, options) }
|
651
678
|
.reject { |x| x.strip == '' }
|
@@ -695,7 +722,7 @@ module Parlour
|
|
695
722
|
|
696
723
|
first, *rest = remaining_children.reject do |child|
|
697
724
|
# We already processed these kinds of children
|
698
|
-
child.is_a?(Include) || child.is_a?(Extend) || child.is_a?(Constant)
|
725
|
+
child.is_a?(Include) || child.is_a?(Extend) || child.is_a?(Constant) || child.is_a?(TypeAlias)
|
699
726
|
end
|
700
727
|
unless first
|
701
728
|
# Remove any trailing whitespace due to includes or class attributes
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# typed: true
|
2
|
+
require 'rainbow'
|
2
3
|
module Parlour
|
3
|
-
class RbiGenerator
|
4
|
+
class RbiGenerator < Generator
|
4
5
|
# Represents a method parameter with a Sorbet type signature.
|
5
6
|
class Parameter
|
6
7
|
extend T::Sig
|
@@ -8,7 +9,7 @@ module Parlour
|
|
8
9
|
sig do
|
9
10
|
params(
|
10
11
|
name: String,
|
11
|
-
type: T.nilable(
|
12
|
+
type: T.nilable(Types::TypeLike),
|
12
13
|
default: T.nilable(String)
|
13
14
|
).void
|
14
15
|
end
|
@@ -80,7 +81,7 @@ module Parlour
|
|
80
81
|
T.must(name[prefix.length..-1])
|
81
82
|
end
|
82
83
|
|
83
|
-
sig { returns(
|
84
|
+
sig { returns(Types::TypeLike) }
|
84
85
|
# A Sorbet string of this parameter's type, such as +"String"+ or
|
85
86
|
# +"T.untyped"+.
|
86
87
|
# @return [String]
|
@@ -118,8 +119,8 @@ module Parlour
|
|
118
119
|
#
|
119
120
|
# @return [String]
|
120
121
|
def to_sig_param
|
121
|
-
"#{name_without_kind}: #{type}"
|
122
|
-
end
|
122
|
+
"#{name_without_kind}: #{String === @type ? @type : @type.generate_rbi}"
|
123
|
+
end
|
123
124
|
|
124
125
|
# A mapping of {kind} values to the characteristic prefixes each kind has.
|
125
126
|
PREFIXES = {
|
@@ -128,6 +129,11 @@ module Parlour
|
|
128
129
|
double_splat: '**',
|
129
130
|
block: '&'
|
130
131
|
}.freeze
|
132
|
+
|
133
|
+
sig { void }
|
134
|
+
def generalize_from_rbi!
|
135
|
+
@type = TypeParser.parse_single_type(@type) if String === @type
|
136
|
+
end
|
131
137
|
end
|
132
138
|
end
|
133
139
|
end
|
@@ -1,17 +1,15 @@
|
|
1
1
|
# typed: true
|
2
2
|
module Parlour
|
3
|
-
class RbiGenerator
|
3
|
+
class RbiGenerator < Generator
|
4
4
|
# An abstract class which is subclassed by any classes which can generate
|
5
5
|
# entire lines of an RBI, such as {Namespace} and {Method}. (As an example,
|
6
6
|
# {Parameter} is _not_ a subclass because it does not generate lines, only
|
7
7
|
# segments of definition and signature lines.)
|
8
8
|
# @abstract
|
9
|
-
class RbiObject
|
10
|
-
extend T::Helpers
|
11
|
-
extend T::Sig
|
9
|
+
class RbiObject < TypedObject
|
12
10
|
abstract!
|
13
|
-
|
14
|
-
sig { params(generator:
|
11
|
+
|
12
|
+
sig { params(generator: Generator, name: String).void }
|
15
13
|
# Creates a new RBI object.
|
16
14
|
# @note Don't call this directly.
|
17
15
|
#
|
@@ -19,60 +17,16 @@ module Parlour
|
|
19
17
|
# @param name [String] The name of this module.
|
20
18
|
# @return [void]
|
21
19
|
def initialize(generator, name)
|
20
|
+
super(name)
|
22
21
|
@generator = generator
|
23
|
-
@generated_by = generator.current_plugin
|
24
|
-
@name = name
|
25
|
-
@comments = []
|
22
|
+
@generated_by = RbiGenerator === generator ? generator.current_plugin : nil
|
26
23
|
end
|
27
24
|
|
28
|
-
sig { returns(
|
25
|
+
sig { returns(Generator) }
|
29
26
|
# The generator which this object belongs to.
|
30
|
-
# @return [
|
27
|
+
# @return [Generator]
|
31
28
|
attr_reader :generator
|
32
29
|
|
33
|
-
sig { returns(T.nilable(Plugin)) }
|
34
|
-
# The {Plugin} which was controlling the {generator} when this object was
|
35
|
-
# created.
|
36
|
-
# @return [Plugin, nil]
|
37
|
-
attr_reader :generated_by
|
38
|
-
|
39
|
-
sig { returns(String) }
|
40
|
-
# The name of this object.
|
41
|
-
# @return [String]
|
42
|
-
attr_reader :name
|
43
|
-
|
44
|
-
sig { returns(T::Array[String]) }
|
45
|
-
# An array of comments which will be placed above the object in the RBI
|
46
|
-
# file.
|
47
|
-
# @return [Array<String>]
|
48
|
-
attr_reader :comments
|
49
|
-
|
50
|
-
sig { params(comment: T.any(String, T::Array[String])).void }
|
51
|
-
# Adds one or more comments to this RBI object. Comments always go above
|
52
|
-
# the definition for this object, not in the definition's body.
|
53
|
-
#
|
54
|
-
# @example Creating a module with a comment.
|
55
|
-
# namespace.create_module('M') do |m|
|
56
|
-
# m.add_comment('This is a module')
|
57
|
-
# end
|
58
|
-
#
|
59
|
-
# @example Creating a class with a multi-line comment.
|
60
|
-
# namespace.create_class('C') do |c|
|
61
|
-
# c.add_comment(['This is a multi-line comment!', 'It can be as long as you want!'])
|
62
|
-
# end
|
63
|
-
#
|
64
|
-
# @param comment [String, Array<String>] The new comment(s).
|
65
|
-
# @return [void]
|
66
|
-
def add_comment(comment)
|
67
|
-
if comment.is_a?(String)
|
68
|
-
comments << comment
|
69
|
-
elsif comment.is_a?(Array)
|
70
|
-
comments.concat(comment)
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
alias_method :add_comments, :add_comment
|
75
|
-
|
76
30
|
sig do
|
77
31
|
abstract.params(
|
78
32
|
indent_level: Integer,
|
@@ -115,32 +69,19 @@ module Parlour
|
|
115
69
|
# @return [void]
|
116
70
|
def merge_into_self(others); end
|
117
71
|
|
118
|
-
sig {
|
119
|
-
|
120
|
-
|
121
|
-
#
|
122
|
-
# @abstract
|
123
|
-
# @return [String]
|
124
|
-
def describe; end
|
125
|
-
|
126
|
-
private
|
127
|
-
|
128
|
-
sig do
|
129
|
-
params(
|
130
|
-
indent_level: Integer,
|
131
|
-
options: Options
|
132
|
-
).returns(T::Array[String])
|
72
|
+
sig { override.overridable.returns(String) }
|
73
|
+
def describe
|
74
|
+
'RBI object'
|
133
75
|
end
|
134
|
-
|
76
|
+
|
77
|
+
sig { abstract.void }
|
78
|
+
# Assuming that the types throughout this object and its children have
|
79
|
+
# been specified as RBI-style types, generalises them into type instances
|
80
|
+
# from the {Parlour::Types} module.
|
135
81
|
#
|
136
|
-
# @
|
137
|
-
# @
|
138
|
-
|
139
|
-
def generate_comments(indent_level, options)
|
140
|
-
comments.any? \
|
141
|
-
? comments.map { |c| options.indented(indent_level, "# #{c}") }
|
142
|
-
: []
|
143
|
-
end
|
82
|
+
# @abstract
|
83
|
+
# @return [void]
|
84
|
+
def generalize_from_rbi!; end
|
144
85
|
end
|
145
86
|
end
|
146
87
|
end
|
@@ -8,7 +8,7 @@ module Parlour
|
|
8
8
|
|
9
9
|
sig do
|
10
10
|
params(
|
11
|
-
generator:
|
11
|
+
generator: Generator,
|
12
12
|
name: String,
|
13
13
|
final: T::Boolean,
|
14
14
|
props: T::Array[StructProp],
|
@@ -39,7 +39,7 @@ module Parlour
|
|
39
39
|
sig do
|
40
40
|
override.params(
|
41
41
|
indent_level: Integer,
|
42
|
-
options: Options
|
42
|
+
options: Options,
|
43
43
|
).returns(T::Array[String])
|
44
44
|
end
|
45
45
|
# Generates the RBI lines for the body of this struct. This consists of
|
@@ -98,6 +98,13 @@ module Parlour
|
|
98
98
|
@props = other.props if props.empty?
|
99
99
|
end
|
100
100
|
end
|
101
|
+
|
102
|
+
sig { override.void }
|
103
|
+
def generalize_from_rbi!
|
104
|
+
super
|
105
|
+
|
106
|
+
props.each(&:generalize_from_rbi!)
|
107
|
+
end
|
101
108
|
end
|
102
109
|
end
|
103
110
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# typed: true
|
2
2
|
module Parlour
|
3
|
-
class RbiGenerator
|
3
|
+
class RbiGenerator < Generator
|
4
4
|
# Represents a +T::Struct+ property.
|
5
5
|
class StructProp
|
6
6
|
extend T::Sig
|
@@ -8,7 +8,7 @@ module Parlour
|
|
8
8
|
sig do
|
9
9
|
params(
|
10
10
|
name: String,
|
11
|
-
type:
|
11
|
+
type: Types::TypeLike,
|
12
12
|
optional: T.nilable(T.any(T::Boolean, Symbol)),
|
13
13
|
enum: T.nilable(String),
|
14
14
|
dont_store: T.nilable(T::Boolean),
|
@@ -28,8 +28,7 @@ module Parlour
|
|
28
28
|
# https://github.com/sorbet/sorbet/blob/master/gems/sorbet-runtime/lib/types/props/_props.rb#L31-L106
|
29
29
|
#
|
30
30
|
# @param name [String] The name of this property.
|
31
|
-
# @param type [String]
|
32
|
-
# +"String"+.
|
31
|
+
# @param type [String] This property's type.
|
33
32
|
# @return [void]
|
34
33
|
def initialize(name, type, optional: nil, enum: nil, dont_store: nil,
|
35
34
|
foreign: nil, default: nil, factory: nil, immutable: nil, array: nil,
|
@@ -77,10 +76,9 @@ module Parlour
|
|
77
76
|
# @return [String]
|
78
77
|
attr_reader :name
|
79
78
|
|
80
|
-
sig { returns(
|
81
|
-
#
|
82
|
-
#
|
83
|
-
# @return [String, nil]
|
79
|
+
sig { returns(Types::TypeLike) }
|
80
|
+
# This parameter's type.
|
81
|
+
# @return [Types::TypeLike, nil]
|
84
82
|
attr_reader :type
|
85
83
|
|
86
84
|
sig { returns(T.nilable(T.any(T::Boolean, Symbol))) }
|
@@ -122,7 +120,7 @@ module Parlour
|
|
122
120
|
# Returns the +prop+ call required to create this property.
|
123
121
|
# @return [String]
|
124
122
|
def to_prop_call
|
125
|
-
call = "prop :#{name}, #{type}"
|
123
|
+
call = "prop :#{name}, #{String === @type ? @type : @type.generate_rbi}"
|
126
124
|
|
127
125
|
EXTRA_PROPERTIES.each do |extra_property|
|
128
126
|
value = send extra_property
|
@@ -131,6 +129,11 @@ module Parlour
|
|
131
129
|
|
132
130
|
call
|
133
131
|
end
|
132
|
+
|
133
|
+
sig { void }
|
134
|
+
def generalize_from_rbi!
|
135
|
+
@type = TypeParser.parse_single_type(@type) if String === @type
|
136
|
+
end
|
134
137
|
end
|
135
138
|
end
|
136
139
|
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
# typed: true
|
2
|
+
module Parlour
|
3
|
+
class RbiGenerator < Generator
|
4
|
+
# Represents a type alias.
|
5
|
+
class TypeAlias < RbiObject
|
6
|
+
sig do
|
7
|
+
params(
|
8
|
+
generator: Generator,
|
9
|
+
name: String,
|
10
|
+
type: Types::TypeLike,
|
11
|
+
block: T.nilable(T.proc.params(x: TypeAlias).void)
|
12
|
+
).void
|
13
|
+
end
|
14
|
+
# Creates a new type alias.
|
15
|
+
#
|
16
|
+
# @param name [String] The name of the alias.
|
17
|
+
# @param value [String] The type to alias to.
|
18
|
+
def initialize(generator, name:, type:, &block)
|
19
|
+
super(generator, name)
|
20
|
+
@type = type
|
21
|
+
yield_self(&block) if block
|
22
|
+
end
|
23
|
+
|
24
|
+
# @return [Types::TypeLike] The type to alias to.
|
25
|
+
sig { returns(Types::TypeLike) }
|
26
|
+
attr_reader :type
|
27
|
+
|
28
|
+
sig { params(other: Object).returns(T::Boolean) }
|
29
|
+
# Returns true if this instance is equal to another type alias.
|
30
|
+
#
|
31
|
+
# @param other [Object] The other instance. If this is not a {TypeAlias} (or a
|
32
|
+
# subclass of it), this will always return false.
|
33
|
+
# @return [Boolean]
|
34
|
+
def ==(other)
|
35
|
+
TypeAlias === other && name == other.name && type == other.type
|
36
|
+
end
|
37
|
+
|
38
|
+
sig do
|
39
|
+
override.params(
|
40
|
+
indent_level: Integer,
|
41
|
+
options: Options
|
42
|
+
).returns(T::Array[String])
|
43
|
+
end
|
44
|
+
# Generates the RBI lines for this type alias.
|
45
|
+
#
|
46
|
+
# @param indent_level [Integer] The indentation level to generate the lines at.
|
47
|
+
# @param options [Options] The formatting options to use.
|
48
|
+
# @return [Array<String>] The RBI lines, formatted as specified.
|
49
|
+
def generate_rbi(indent_level, options)
|
50
|
+
[options.indented(indent_level,
|
51
|
+
"#{name} = T.type_alias { #{String === @type ? @type : @type.generate_rbi} }"
|
52
|
+
)]
|
53
|
+
end
|
54
|
+
|
55
|
+
sig do
|
56
|
+
override.params(
|
57
|
+
others: T::Array[RbiGenerator::RbiObject]
|
58
|
+
).returns(T::Boolean)
|
59
|
+
end
|
60
|
+
# Given an array of {TypeAlias} instances, returns true if they may be
|
61
|
+
# merged into this instance using {merge_into_self}. This is always false.
|
62
|
+
#
|
63
|
+
# @param others [Array<RbiGenerator::RbiObject>] An array of other
|
64
|
+
# {TypeAlias} instances.
|
65
|
+
# @return [Boolean] Whether this instance may be merged with them.
|
66
|
+
def mergeable?(others)
|
67
|
+
others.all? { |other| self == other }
|
68
|
+
end
|
69
|
+
|
70
|
+
sig do
|
71
|
+
override.params(
|
72
|
+
others: T::Array[RbiGenerator::RbiObject]
|
73
|
+
).void
|
74
|
+
end
|
75
|
+
# Given an array of {TypeAlias} instances, merges them into this one.
|
76
|
+
# This particular implementation will simply do nothing, as instances
|
77
|
+
# are only mergeable if they are indentical.
|
78
|
+
# You MUST ensure that {mergeable?} is true for those instances.
|
79
|
+
#
|
80
|
+
# @param others [Array<RbiGenerator::RbiObject>] An array of other
|
81
|
+
# {TypeAlias} instances.
|
82
|
+
# @return [void]
|
83
|
+
def merge_into_self(others)
|
84
|
+
# We don't need to change anything! We only merge identical type alias
|
85
|
+
end
|
86
|
+
|
87
|
+
sig { override.returns(String) }
|
88
|
+
# Returns a human-readable brief string description of this code.
|
89
|
+
#
|
90
|
+
# @return [String]
|
91
|
+
def describe
|
92
|
+
"Type Alias (#{name} = #{type})"
|
93
|
+
end
|
94
|
+
|
95
|
+
sig { override.void }
|
96
|
+
def generalize_from_rbi!
|
97
|
+
@type = TypeParser.parse_single_type(@type) if String === @type
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|