Skip to content

Commit

Permalink
Handle nil attributes by making them untyped
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronC81 committed Sep 15, 2020
1 parent 5e2b5cd commit dbda472
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/sord/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,9 @@ def add_attributes(item)
if yard_types.empty?
Logging.omit("no YARD type given for #{name.inspect}, using untyped", reader || writer)
parlour_type = Parlour::Types::Untyped.new
elsif yard_types.all? { |x| x == 'nil' }
# Nil attributes are extremely unusual, so just use untyped
parlour_type = Parlour::Types::Untyped.new
else
parlour_type = TypeConverter.yard_to_parlour(
yard_types, reader || writer, @replace_errors_with_untyped, @replace_unresolved_with_untyped)
Expand Down
23 changes: 23 additions & 0 deletions spec/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1629,4 +1629,27 @@ def foo: () -> void
end
RUBY
end

it 'handles nil attributes' do
YARD.parse_string(<<-RUBY)
class A
# @return [nil]
attr_accessor :x
end
RUBY

expect(rbi_gen.generate.strip).to eq fix_heredoc(<<-RUBY)
# typed: strong
class A
sig { returns(T.untyped) }
attr_accessor :x
end
RUBY

expect(rbs_gen.generate.strip).to eq fix_heredoc(<<-RUBY)
class A
attr_accessor x: untyped
end
RUBY
end
end

0 comments on commit dbda472

Please sign in to comment.