Skip to content

Commit 00bf962

Browse files
committed
Add support for generic<T> syntax for type variables
This is the syntax used by Solargraph for generic type variables.
1 parent e15833a commit 00bf962

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

lib/sord/type_converter.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,17 @@ def self.yard_to_parlour(yard, item, config)
199199
.map { |x| yard_to_parlour(x, item, config) }
200200
if SINGLE_ARG_GENERIC_TYPES.include?(relative_generic_type) && parameters.length > 1
201201
Parlour::Types.const_get(relative_generic_type).new(Parlour::Types::Union.new(parameters))
202+
elsif relative_generic_type == 'generic'
203+
if parameters.length == 1
204+
# used by solargraph to indicate a type variable. Until
205+
# Parlour needs to support separate namespaces, convert to
206+
# the same namespaces as raw types:
207+
yard_to_parlour(parameters.first, nil, config)
208+
else
209+
handle_sord_error(parameters.map(&:describe).join,
210+
"Invalid generic<>, must have exactly one type variable: #{yard.inspect}.", item,
211+
config.replace_errors_with_untyped)
212+
end
202213
elsif relative_generic_type == 'Class'
203214
if parameters.length == 1
204215
Parlour::Types::Class.new(parameters.first)

spec/type_converter_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,16 @@ def self.path
250250
end
251251
end
252252

253+
context 'with a solargraph-style type variable' do
254+
it 'handles type variables by converting back to RBS-style unified namespace' do
255+
expect(yard_to_parlour_default('generic<Foo>')).to eq Types::Raw.new('Foo')
256+
end
257+
258+
it 'handles generic<> with more than one argument' do
259+
expect(yard_to_parlour_default('generic<F, T>')).to eq Types::Raw.new('SORD_ERROR_FT')
260+
end
261+
end
262+
253263
context 'when given an untyped generic' do
254264
it 'handles Hash correctly' do
255265
expect(yard_to_parlour_default('Hash')).to eq Types::Hash.new(Types::Untyped.new, Types::Untyped.new)
@@ -339,7 +349,7 @@ def self.path
339349
Sord::TypeConverter::Configuration.new(
340350
output_language: :rbs,
341351
replace_errors_with_untyped: false,
342-
replace_unresolved_with_untyped: false,
352+
replace_unresolved_with_untyped: false,
343353
)
344354
end
345355

0 commit comments

Comments
 (0)