Skip to content

Commit

Permalink
Split line if it's longer than max line length
Browse files Browse the repository at this point in the history
  • Loading branch information
Morriar authored and KaanOzkan committed Mar 5, 2024
1 parent cdfa443 commit 0ef3f07
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
31 changes: 29 additions & 2 deletions lib/rubocop/cop/sorbet/forbid_t_struct.rb
Expand Up @@ -219,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
51 changes: 51 additions & 0 deletions spec/rubocop/cop/sorbet/forbid_t_struct_spec.rb
Expand Up @@ -434,5 +434,56 @@ def initialize(foo: nil)

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

0 comments on commit 0ef3f07

Please sign in to comment.