Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ForbidTStruct handles nilable properties #180

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions lib/rubocop/cop/sorbet/forbid_t_struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,13 @@ def initialize_sig_param
def initialize_param
rb = String.new
rb << "#{name}:"
rb << " #{default}" if default
rb << " #{factory}" if factory
if default
rb << " #{default}"
elsif factory
rb << " #{factory}"
elsif nilable?
rb << " nil"
end
rb
end

Expand All @@ -142,6 +147,10 @@ def initialize_assign
rb << ".call" if factory
rb
end

def nilable?
type.start_with?("T.nilable(")
end
end

# @!method t_struct?(node)
Expand Down Expand Up @@ -203,11 +212,11 @@ def on_send(node)

def initialize_method(indent, props)
# We sort optional keyword arguments after required ones
props = props.sort_by { |prop| prop.default || prop.factory ? 1 : 0 }
sorted_props = props.sort_by { |prop| prop.default || prop.factory || prop.nilable? ? 1 : 0 }

string = +"\n"
string << "#{indent}sig { params(#{props.map(&:initialize_sig_param).join(", ")}).void }\n"
string << "#{indent}def initialize(#{props.map(&:initialize_param).join(", ")})\n"
string << "#{indent}sig { params(#{sorted_props.map(&:initialize_sig_param).join(", ")}).void }\n"
string << "#{indent}def initialize(#{sorted_props.map(&:initialize_param).join(", ")})\n"
props.each do |prop|
string << "#{indent} #{prop.initialize_assign}\n"
end
Expand Down
36 changes: 35 additions & 1 deletion spec/rubocop/cop/sorbet/forbid_t_struct_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ class Foo
sig { params(foo: Integer, baz: Symbol, bar: String).void }
def initialize(foo:, baz:, bar: "foo")
@foo = foo
@baz = baz
@bar = bar
@baz = baz
end
end
RUBY
Expand Down Expand Up @@ -374,5 +374,39 @@ def initialize(foo:, bar:)

expect(autocorrect_source(source)).to(eq(corrected))
end

it "handles optional nilable properties" do
source = <<~RUBY
class Foo < T::Struct
const :foo, T.nilable(Integer)
Morriar marked this conversation as resolved.
Show resolved Hide resolved
prop :bar, T.nilable(String)
prop :baz, Integer
end
RUBY

corrected = <<~RUBY
class Foo
extend T::Sig

sig { returns(T.nilable(Integer)) }
attr_reader :foo

sig { returns(T.nilable(String)) }
attr_accessor :bar

sig { returns(Integer) }
attr_accessor :baz

sig { params(baz: Integer, foo: T.nilable(Integer), bar: T.nilable(String)).void }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not clear why we need to change the argument order here if only dealing with keyword arguments?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Order is kept for keyword arguments foo and bar. Only baz is moved earlier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def initialize(baz:, foo: nil, bar: nil)
@foo = foo
@bar = bar
@baz = baz
end
end
RUBY

expect(autocorrect_source(source)).to(eq(corrected))
end
end
end