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 cop's autocorrect should support new lines and adhere to max line length #203

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 34 additions & 3 deletions lib/rubocop/cop/sorbet/forbid_t_struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def on_send(node)
end

class Property
attr_reader :node, :kind, :name, :type, :default, :factory
attr_reader :node, :kind, :name, :default, :factory

def initialize(node, kind, name, type, default:, factory:)
@node = node
Expand Down Expand Up @@ -151,6 +151,10 @@ def initialize_assign
def nilable?
type.start_with?("T.nilable(")
end

def type
@type.gsub(/[[:space:]]+/, "").strip
end
end

# @!method t_struct?(node)
Expand Down Expand Up @@ -215,8 +219,35 @@ def initialize_method(indent, props)
sorted_props = props.sort_by { |prop| prop.default || prop.factory || prop.nilable? ? 1 : 0 }

string = +"\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"

line = "#{indent}sig { params(#{sorted_props.map(&:initialize_sig_param).join(", ")}).void }\n"
if line.length <= max_line_length
string << line
else
string << "#{indent}sig do\n"
string << "#{indent} params(\n"
sorted_props.each do |prop|
string << "#{indent} #{prop.initialize_sig_param}"
string << "," if prop != sorted_props.last
string << "\n"
end
string << "#{indent} ).void\n"
string << "#{indent}end\n"
end

line = "#{indent}def initialize(#{sorted_props.map(&:initialize_param).join(", ")})\n"
if line.length <= max_line_length
string << line
else
string << "#{indent}def initialize(\n"
sorted_props.each do |prop|
string << "#{indent} #{prop.initialize_param}"
string << "," if prop != sorted_props.last
string << "\n"
end
string << "#{indent})\n"
end

props.each do |prop|
string << "#{indent} #{prop.initialize_assign}\n"
end
Expand Down
77 changes: 77 additions & 0 deletions spec/rubocop/cop/sorbet/forbid_t_struct_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -408,5 +408,82 @@ def initialize(baz:, foo: nil, bar: nil)

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

it "strips new lines from type definitions" do
source = <<~RUBY
class Foo < T::Struct
const :foo, T.nilable(
Integer
)
end
RUBY

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

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

sig { params(foo: T.nilable(Integer)).void }
def initialize(foo: nil)
@foo = foo
end
end
RUBY

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

it "splits long lines" do
source = <<~RUBY
class Foo < T::Struct
const :foo, LONG_CONSTANT_NAME_WITH_MANY_CHARS, default: LONG_CONSTANT_NAME_WITH_MANY_CHARS
const :bar, LONG_CONSTANT_NAME_WITH_MANY_CHARS, default: LONG_CONSTANT_NAME_WITH_MANY_CHARS
const :baz, LONG_CONSTANT_NAME_WITH_MANY_CHARS, default: LONG_CONSTANT_NAME_WITH_MANY_CHARS
const :qux, LONG_CONSTANT_NAME_WITH_MANY_CHARS, default: LONG_CONSTANT_NAME_WITH_MANY_CHARS
end
RUBY

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

sig { returns(LONG_CONSTANT_NAME_WITH_MANY_CHARS) }
attr_reader :foo

sig { returns(LONG_CONSTANT_NAME_WITH_MANY_CHARS) }
attr_reader :bar

sig { returns(LONG_CONSTANT_NAME_WITH_MANY_CHARS) }
attr_reader :baz

sig { returns(LONG_CONSTANT_NAME_WITH_MANY_CHARS) }
attr_reader :qux

sig do
params(
foo: LONG_CONSTANT_NAME_WITH_MANY_CHARS,
bar: LONG_CONSTANT_NAME_WITH_MANY_CHARS,
baz: LONG_CONSTANT_NAME_WITH_MANY_CHARS,
qux: LONG_CONSTANT_NAME_WITH_MANY_CHARS
).void
end
def initialize(
foo: LONG_CONSTANT_NAME_WITH_MANY_CHARS,
bar: LONG_CONSTANT_NAME_WITH_MANY_CHARS,
baz: LONG_CONSTANT_NAME_WITH_MANY_CHARS,
qux: LONG_CONSTANT_NAME_WITH_MANY_CHARS
)
@foo = foo
@bar = bar
@baz = baz
@qux = qux
end
end
RUBY

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