Skip to content

Commit a977665

Browse files
committed
Handle more 'nil' scenarios parsing YARD types
1 parent e15833a commit a977665

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

lib/sord/type_converter.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ def self.yard_to_parlour(yard, item, config)
129129
case yard
130130
when nil # Type not specified
131131
Parlour::Types::Untyped.new
132+
when "nil"
133+
Parlour::Types::Raw.new('NilClass')
132134
when "bool", "Bool", "boolean", "Boolean", "true", "false"
133135
Parlour::Types::Boolean.new
134136
when 'self'
@@ -195,10 +197,11 @@ def self.yard_to_parlour(yard, item, config)
195197
relative_generic_type = generic_type.start_with?('::') \
196198
? generic_type[2..-1] : generic_type
197199

198-
parameters = split_type_parameters(type_parameters)
200+
yard_parameters = split_type_parameters(type_parameters)
201+
parameters = yard_parameters
199202
.map { |x| yard_to_parlour(x, item, config) }
200-
if SINGLE_ARG_GENERIC_TYPES.include?(relative_generic_type) && parameters.length > 1
201-
Parlour::Types.const_get(relative_generic_type).new(Parlour::Types::Union.new(parameters))
203+
if SINGLE_ARG_GENERIC_TYPES.include?(relative_generic_type) && yard_parameters.length > 1
204+
Parlour::Types.const_get(relative_generic_type).new(yard_to_parlour(yard_parameters, item, config))
202205
elsif relative_generic_type == 'Class'
203206
if parameters.length == 1
204207
Parlour::Types::Class.new(parameters.first)

spec/type_converter_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def yard_to_parlour_default(type)
7777

7878
context 'with literals' do
7979
it 'converts literals to their types' do
80+
expect(yard_to_parlour_default('nil')).to eq Types::Raw.new('NilClass')
8081
expect(yard_to_parlour_default([':up', ':down'])).to eq Types::Raw.new('Symbol')
8182
expect(yard_to_parlour_default(['String', ':up', ':down'])).to eq \
8283
Types::Union.new(['String', 'Symbol'])
@@ -132,6 +133,11 @@ def self.path
132133
Types::Array.new(Types::Union.new(['String', 'Integer']))
133134
end
134135

136+
it 'handles nil is one of multiple arguments in a one-argument type parameter' do
137+
expect(yard_to_parlour_default('Array<String, nil>')).to eq \
138+
Types::Array.new(Types::Nilable.new('String'))
139+
end
140+
135141
it 'handles whitespace' do
136142
expect(yard_to_parlour_default('Array < String >')).to eq Types::Array.new('String')
137143
end
@@ -148,6 +154,7 @@ def self.path
148154
it 'handles correctly-formed two-argument type parameters with hash rockets' do
149155
expect(yard_to_parlour_default('Hash<String=>Symbol>')).to eq Types::Hash.new('String', 'Symbol')
150156
expect(yard_to_parlour_default('Hash{String=>Symbol}')).to eq Types::Hash.new('String', 'Symbol')
157+
expect(yard_to_parlour_default('Hash{String=>String}')).to eq Types::Hash.new('String', 'String')
151158
expect(yard_to_parlour_default('Hash{String => Symbol}')).to eq Types::Hash.new('String', 'Symbol')
152159
expect(yard_to_parlour_default('Hash{String, Integer => Symbol, Float}')).to eq \
153160
Types::Hash.new(
@@ -339,7 +346,7 @@ def self.path
339346
Sord::TypeConverter::Configuration.new(
340347
output_language: :rbs,
341348
replace_errors_with_untyped: false,
342-
replace_unresolved_with_untyped: false,
349+
replace_unresolved_with_untyped: false,
343350
)
344351
end
345352

0 commit comments

Comments
 (0)