Skip to content

Commit a3d9ca6

Browse files
authored
Merge pull request #187 from apiology/nil
Handle more 'nil' scenarios parsing YARD types
2 parents 7ce4a86 + a977665 commit a3d9ca6

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

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 "undefined" # solargraph convention
@@ -197,10 +199,11 @@ def self.yard_to_parlour(yard, item, config)
197199
relative_generic_type = generic_type.start_with?('::') \
198200
? generic_type[2..-1] : generic_type
199201

200-
parameters = split_type_parameters(type_parameters)
202+
yard_parameters = split_type_parameters(type_parameters)
203+
parameters = yard_parameters
201204
.map { |x| yard_to_parlour(x, item, config) }
202-
if SINGLE_ARG_GENERIC_TYPES.include?(relative_generic_type) && parameters.length > 1
203-
Parlour::Types.const_get(relative_generic_type).new(Parlour::Types::Union.new(parameters))
205+
if SINGLE_ARG_GENERIC_TYPES.include?(relative_generic_type) && yard_parameters.length > 1
206+
Parlour::Types.const_get(relative_generic_type).new(yard_to_parlour(yard_parameters, item, config))
204207
elsif relative_generic_type == 'Class'
205208
if parameters.length == 1
206209
Parlour::Types::Class.new(parameters.first)

spec/type_converter_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def yard_to_parlour_default(type)
8383

8484
context 'with literals' do
8585
it 'converts literals to their types' do
86+
expect(yard_to_parlour_default('nil')).to eq Types::Raw.new('NilClass')
8687
expect(yard_to_parlour_default([':up', ':down'])).to eq Types::Raw.new('Symbol')
8788
expect(yard_to_parlour_default(['String', ':up', ':down'])).to eq \
8889
Types::Union.new(['String', 'Symbol'])
@@ -138,6 +139,11 @@ def self.path
138139
Types::Array.new(Types::Union.new(['String', 'Integer']))
139140
end
140141

142+
it 'handles nil is one of multiple arguments in a one-argument type parameter' do
143+
expect(yard_to_parlour_default('Array<String, nil>')).to eq \
144+
Types::Array.new(Types::Nilable.new('String'))
145+
end
146+
141147
it 'handles whitespace' do
142148
expect(yard_to_parlour_default('Array < String >')).to eq Types::Array.new('String')
143149
end
@@ -154,6 +160,7 @@ def self.path
154160
it 'handles correctly-formed two-argument type parameters with hash rockets' do
155161
expect(yard_to_parlour_default('Hash<String=>Symbol>')).to eq Types::Hash.new('String', 'Symbol')
156162
expect(yard_to_parlour_default('Hash{String=>Symbol}')).to eq Types::Hash.new('String', 'Symbol')
163+
expect(yard_to_parlour_default('Hash{String=>String}')).to eq Types::Hash.new('String', 'String')
157164
expect(yard_to_parlour_default('Hash{String => Symbol}')).to eq Types::Hash.new('String', 'Symbol')
158165
expect(yard_to_parlour_default('Hash{String, Integer => Symbol, Float}')).to eq \
159166
Types::Hash.new(

0 commit comments

Comments
 (0)